我正在尝试学习没有任何编程经验的Java,所以我正在学习一个教程,但是我被困在一个我无法理解的部分。在本教程中,我制作了一个模拟BankAccount函数并尝试对其进行测试。但是,我陷入了" setOverdraftAccount"功能。
这是我的测试代码:
/**
Class TestBank
**/
//MAKE NO CHANGES TO THIS CLASS OTHER THAN UNCOMMENTING LINES ONE BY ONE
//MODIFY THE BankAccount CLASS TO CORRECT ERRORS REVEALED AS YOU UNCOMMENT THE LINES
public class TestBank {
private static int counter = 0;
public static void main(String [] args) {
BankAccount b1 = new BankAccount(254.36);
printBalance(b1, 254.36);//must produce "b1 balance = 254.36..."
//
double amt = 100.00;
//
b1.deposit(amt);
printBalance(b1, 354.36);//must produce "b1 balance = 354.36..."
//
b1.withdraw(100.00);
printBalance(b1, 254.36);
//
BankAccount b2 = new BankAccount(123.45);//must produce "b2 balance = 123.45..."
printBalance(b2, 123.45);
//
b2.deposit(amt*2);
printBalance(b2, 323.45);
//
b2.withdraw(10.56);
printBalance(b2, 312.89);
//
b1.transfer(154.36, b2); //transfer $154.36 from account b1 to account b2
printBalance(b1, 100.00);
printBalance(b2, 467.25);
//
BankAccount b3 = new BankAccount(83.80);
printBalance(b3, 83.80); //must produce "b3 balance = 83.80..."
//
b1.setOverdraftAccount(b2); // set b2 as the overdraft account for b1
// //if account balance goes below zero, funds are transferred from overdraft account in $50 increments to cover shortfall
//
b2.setOverdraftAccount(b3); //set b3 as the overdraft account for b2
//
b1.withdraw(554.50);
printBalance(b1, 45.50);
printBalance(b2, 17.25);
printBalance(b3, 33.80);
private static void printBalance(BankAccount b, double expectedBalance)
{
counter++;
System.out.println(counter + ". " + b.getAccountNumber()+ " balance = "+ b.getBalance() + " --> must be "+ expectedBalance);
}
}
public class BankAccount {
private double balance;
private String accountNumber;
private static int counter = 1;
private BankAccount overdraftAccount = null;
public BankAccount(double balance) {
this.balance = balance;
this.accountNumber = "b"+counter;
counter++;
}
public String getAccountNumber() {
return accountNumber;
}
public double getBalance() {
return balance;
}
public void deposit(double amount) {
balance += amount;
}
public void withdraw(double amount) {
balance -= amount;
}
public void transfer(double amount, BankAccount other) {
this.withdraw(amount);
other.deposit(amount);
}
public void setOverdraftAccount(BankAccount b) {
overdraftAccount = b;
}
}
在我的Testbank课程中,它说" b1.setOverdraftAccount(b2)"它描述了当资金低于0时程序应该做什么,在这种情况下增加50美元。有人能指出我应该做些什么来解决这个问题,而不向TestBank类添加任何内容吗?
我有一种感觉,我应该在我的" setOverdraftAccount"中添加一个while循环。功能是为了从我的透支帐户中提取50,但我不知道如何将50添加到b1的余额中。