【发布时间】:2012-10-06 18:55:12
【问题描述】:
我正在用 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 () + "!");
}
【问题讨论】:
-
我不明白你的问题。
-
无论你在做什么,你都不应该在涉及金钱的地方使用 double。请改用 BigDecimal。
-
我无法将钱从一个帐户转移到另一个帐户,我在引擎类上遇到错误。我认为我向其汇款的帐户有问题...
-
听 Vash。并显示错误的堆栈跟踪。
-
什么错误?您是否希望我们围绕这两个类编写上下文并自己找到它?