我正在用 Java 做一个项目,我被困在其中的一部分上。我在 SavingsAccount 类中有存款功能,但我似乎无法弄清楚如何在引擎类中调用它。对于我们的项目,我们必须允许用户使用 BlueJ 虚拟机创建多个银行账户并在它们之间转移资金。我将发布我的引擎类和储蓄账户类的相关代码......谢谢,任何帮助将不胜感激!
问题:我无法将钱从一个帐户转移到另一个帐户,我在引擎类上收到一条错误消息。我想我汇款到的账户有问题...
储蓄账户代码
public class SavingsAccount extends BankAccount
public void transfer (BankAccount that, double amount)
{
if
(balance-amount < -80)
balance = balance ;
else
{
if
(amount <= balance)
{
this.balance = this.balance - amount;
that.balance = that.balance + amount;
}
else
{
this.balance = this.balance - amount-20;
that.balance = that.balance + amount;
}
}
}
引擎类
public class engine
{
SavingsAccount savings1 = new SavingsAccount();
savings1.balance = 0;
//code for other choices, such as deposit and withdraw...
if (selection2 == 3)
{
System.out.println ("How much would you like to transfer?");
int transferAmount = in.nextInt ( );
System.out.println ("Which account would you like to transfer the money to?");
String thatAccount = in.next();
savings1.withdraw (transferAmount);
thatAccount.deposit (transferAmount);
System.out.println ("You account balance is " + savings1.getBalance () + "!");
}