我正在用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 () + "!");
}
答案 0 :(得分:3)
我有一些obervation /建议如下:
您的transferAccount thatAccount
是字符串String thatAccount = in.next();
。你怎么能在那上面调用deposit ()
方法?
我在deposit()
类中没有看到withdraw()
和SavingsAccount
方法,希望BankAccount
类中存在这些方法。
现在确定如何将余额初始化为saving1.balance=0;
。它应该通过一些类方法完成,例如setBalance
为saving1.setBalance(0);
。
当您调用savings1.withdraw()
方法时,余额为0
。
希望这些能帮助您识别问题并纠正程序。