我正在处理的项目是带有单个PatronList
对象的银行接口,该对象本身包含包含BankPatron
对象的BankAccount
对象。我面临的问题是我似乎无法创建任何新对象。 main方法仅使用单个PatronList
对象,但是我尝试创建的任何BankPatron
或BankAccount
对象都没有,但是由于某种原因它们仍会输出。在我的程序中,所有对象都已经预制,并设置为等于null。这有很多代码,我不确定可能需要什么,所以如果在这里发布太多内容,我感到抱歉。
因此,界面菜单以此开头:
要添加列表用户,主要方法中的代码如下:
if(option.equals("a"))
{
if(((pList.getBankPatron(0)==null)&&(pList.getBankPatron(1)==null)&&(pList.getBankPatron(2)==null)&&(pList.getBankPatron(3)==null)&&(pList.getBankPatron(4)==null)))
{
System.out.println("None.");
}
else
{
for(int i =0; i<=4;i++)
{
if( pList.getBankPatron(i)!=null)
{
BankPatron u = pList.getBankPatron(i);
pList.patronInfo(u);}
}
}
}
要添加顾客,它是:
if(option.equals("b"))
{
System.out.println("What is the patron's first name?");
String fname = scan.next();
System.out.println("Last name?");
String lname = scan.next();
System.out.println("Their salary?");
double salary = scan.nextDouble();
System.out.println("Their cash on hand?");
double cash = scan.nextDouble();
if(pList.addPatron(fname, lname, salary, cash))
{
System.out.println("Welcome to the bank.");
}
else
{
System.out.println("Bank line is full, sorry.");
}
}
这是BankPatron
类的代码:
public class PatronList {
private BankPatron patronOne;
private BankPatron patronTwo;
private BankPatron patronThree;
private BankPatron patronFour;
private BankPatron patronFive;
public PatronList()
{
patronOne =null;
patronTwo =null;
patronThree =null;
patronFour =null;
patronFive =null;
}
public boolean addPatron(BankPatron e)
{
boolean success =false;
if(patronOne==null)
{
patronOne = e;
success =true;
}
else if(patronTwo==null)
{
patronTwo = e;
success =true;
}
else if(patronThree==null)
{
patronThree = e;
success =true;
}
else if(patronFour==null)
{
patronFour = e;
success =true;
}
else if(patronFive==null)
{
patronFive = e;
success =true;
}
return success;
}
public boolean addPatron(String fname, String lname, double pay, double cash)
{
BankPatron b = new BankPatron(fname, lname,pay,cash);
return this.addPatron(b);
}
public BankPatron getBankPatron(int i)
{
switch(i)
{
case 0: return patronOne;
case 1: return patronTwo;
case 2: return patronThree;
case 3: return patronFour;
case 4: return patronFive;
default: return null;
}
}
public BankPatron getBankPatron(String name)
{
if(patronOne!=null)
{
if(name.equals(patronOne.getName()))
{
return patronOne;
}
else
return null;
}
if(patronTwo!=null)
{
if(name.equals(patronTwo.getName()))
{
return patronTwo;
}
else
return null;
}
if(patronThree!=null)
{
if(name.equals(patronThree.getName()))
{
return patronThree;
}
else
return null;
}
if(patronFour!=null)
{
if(name.equals(patronFour.getName()))
{
return patronFour;
}
else
return null;
}
if(patronFive!=null)
{
if(name.equals(patronFive.getName()))
{
return patronFive;
}
else
return null;
}
else
return null;
}
public boolean removePatron(BankPatron e)
{
boolean success=true;
if(e==patronOne)
{
patronOne=null;
}
else if(e==patronTwo)
{
patronTwo=null;
}
else if(e==patronThree)
{
patronThree=null;
}
else if(e==patronFour)
{
patronFour=null;
}
else if(e==patronFive)
{
patronFive=null;
}
else
{
success=false;
}
return success;
}
public String patronInfo(BankPatron a)
{
String s = a.toString();
return s;
}
}
银行赞助人类别的代码:
public class BankPatron {
private String firstName;
private String lastName;
private double salary;
private double cashOnHand;
private BankAccount accountOne;
private BankAccount accountTwo;
public BankPatron()
{
firstName = "John";
lastName = "Doe";
salary = 50000.00;
cashOnHand = 2300.00;
accountOne = null;
accountTwo=null;
}
public BankPatron(String fname, String lname, double pay, double cash)
{
firstName= fname;
lastName=lname;
salary=pay;
cashOnHand= cash;
accountOne = null;
accountTwo=null;
}
public String getName()
{
return firstName + " " + lastName;
}
public double getSalary()
{
return salary;
}
public double getCashOnHand()
{
return cashOnHand;
}
public void payCheck()
{
double paycheck = salary / 26;
cashOnHand+= paycheck;
}
public BankAccount getBankAccount(int i)
{
if(i !=1 ||i!=2)
{
return null;
}
else if(i == 1)
{
return accountOne;
}
else
return accountTwo;
}
public boolean deposit(int account, double depo)
{
if(account==1 && accountOne != null)
{
if(cashOnHand>=depo)
{
cashOnHand-=depo;
accountOne.deposit(depo);
return true;
}
else
return false;
}
if(account==2 && accountTwo != null)
{
if(cashOnHand>=depo)
{
cashOnHand-=depo;
accountTwo.deposit(depo);
return true;
}
else
return false;
}
else
return false;
}
public boolean withdraw(int account, double wdraw)
{
if(account==1 && accountOne != null)
{
if(accountOne.withdraw(wdraw)== true)
{
cashOnHand+=wdraw;
return true;
}
else
{
return false;
}
}
if(account==2 && accountTwo != null)
{
if(accountTwo.withdraw(wdraw)== true)
{
cashOnHand+=wdraw;
return true;
}
else
{
return false;
}
}
else
return false;
}
public boolean addAccount(double rate, AccountType type)
{
boolean success=true;
if(accountOne==null)
{
accountOne = new BankAccount(firstName, lastName, rate, type);
}
else if(accountTwo==null)
{
accountTwo = new BankAccount(firstName, lastName, rate, type);
}
else
{
success=false;
}
return success;
}
public boolean removeAccount(int i)
{
if(i==1 && accountOne!=null)
{
accountOne=null;
return true;
}
if(i==2 && accountTwo!=null)
{
accountTwo=null;
return true;
}
else
return false;
}
public String toString()
{
String output=this.getName();
if(accountOne != null)
{
output+= accountOne.getType()+ " " + accountOne.getAccountNum() + " Balance: " + accountOne.checkBalance()+" Interest Rate: "+ accountOne.getRate();
}
if(accountTwo != null)
{
output+= " "+ accountTwo.getType()+ " " + accountTwo.getAccountNum() + " Balance: " + accountTwo.checkBalance()+" Interest Rate: "+ accountTwo.getRate();
}
return output;
}
}
BankAccount
类的代码:
public class BankAccount
{
private int accountNumber;
private double balance;
private double interestRate;
private String holderFirstName;
private String holderLastName;
private AccountType type;
private static int nextAccountNum = 1001;
public BankAccount()
{
accountNumber = getNextAccountNum();
balance = 0.0;
interestRate = 0.0;
holderFirstName = "";
holderLastName = "";
type = AccountType.Checking;
}
public BankAccount(String first, String last, double rate, AccountType at)
{
accountNumber = getNextAccountNum();
balance = 0.0;
interestRate = rate;
holderFirstName = first;
holderLastName = last;
type = at;
}
private int getNextAccountNum()
{
return nextAccountNum++;
}
public int getAccountNum()
{
return accountNumber;
}
public double checkBalance()
{
return balance;
}
public void deposit(double amount)
{
balance += amount;
}
public boolean withdraw(double amount)
{
boolean success = true;
if((balance - amount) >= 0.0) // Only able to withdraw if there is actually enough money.
{
balance -= amount;
}
else
{
success = false;
}
return success;
}
public double getRate()
{
return interestRate;
}
public void setRate(double rate)
{
interestRate = rate;
}
public void applyInterest()
{
balance += (balance * (interestRate / 100));
}
public String getHolderFullName()
{
return holderFirstName + " " + holderLastName;
}
public AccountType getType()
{
return type;
}
}
谢谢。
答案 0 :(得分:0)
不太确定这对其他人有什么用,但是我弄清楚了为什么我无法检索任何创建的BankAccount对象。问题是下面的方法,它是一个简单的逻辑错误。我在第一个if语句中有OR而不是AND。凯文(Kevin)的评论表明了为什么我也难以创建BankPatron对象。
public BankAccount getBankAccount(int i)
{
if(i !=1 || i!=2)
{
return null;
}
else if(i == 1)
{
return accountOne;
}
else
return accountTwo;