gpt4 book ai didi

java - 银行账户转账项目 Java

转载 作者:行者123 更新时间:2023-12-02 00:11:23 25 4
gpt4 key购买 nike

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

}

最佳答案

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

您的转账帐号 thatAccount是一个字符串String thatAccount = in.next(); 。怎么打电话deposit ()方法在那?

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

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

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

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

关于java - 银行账户转账项目 Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12762839/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com