0

我正在用 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 () + "!");

      }
4

1 回答 1

3

我有一些观察/建议如下:

您的 transferAccountthatAccount是一个 String String thatAccount = in.next();。你怎么能调用deposit ()方法呢?

我在课堂上没有看到deposit()withdraw()方法SavingsAccount,希望BankAccount课堂上有。

现在确定您如何将余额初始化为saving1.balance=0;. 它应该通过一些类方法来完成,例如setBalanceas saving1.setBalance(0);

当您调用savings1.withdraw()方法时,余额为0.

希望这些将帮助您确定问题并纠正程序。

于 2012-10-06T19:19:59.020 回答