【问题标题】:Bank Account Transfer Project Java银行账户转账项目 Java
【发布时间】: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。并显示错误的堆栈跟踪。
  • 什么错误?您是否希望我们围绕这两个类编写上下文并自己找到它?

标签: java class bank


【解决方案1】:

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

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

我在SavingsAccount 类中没有看到deposit()withdraw() 方法,希望BankAccount 类中存在。

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

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

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

【讨论】:

  • 这就是我需要帮助的地方。如何“找到”用户选择的转移目的地。我意识到现在它只是一个字符串,但我真的不知道从那时起该怎么做。我在java方面的经验非常有限......
  • 不确定,我可以在这里提供多少帮助,但您需要遵循引擎课程中的一些基本步骤。 1. 将地图定义为Map&lt;String, SavingsAccount&gt; maintainedAccounts = new HashMap&lt;String, SavingsAccount&gt;();。 2. 实例化账户并在地图中输入maintainedAccounts.put("acountNumber123",savings1);。 3.在withdrawdepost之前,获取银行账户SavingsAccount accountToMaintain = maintainedAccounts.get("accountNumber123");。 4. 在检索到的银行对象上调用所需的函数,例如accountToMaintain.deposit(1000);accountToMaintain.withdraw(500);.
猜你喜欢
  • 2014-08-27
  • 2015-09-08
  • 1970-01-01
  • 2019-07-22
  • 2020-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多