gpt4 book ai didi

java - 将 Math.max 用于 BankAccount 类

转载 作者:行者123 更新时间:2023-11-29 08:01:36 25 4
gpt4 key购买 nike

我有一个家庭作业问题,我必须为银行账户中超过分配的免费交易次数的每笔交易扣除费用。我的问题是我的 Math.max 不适用于 deductMonthlyCharge 类,程序不是仅在交易超过分配金额时才收取费用,而是收取每笔交易的费用。我不知道如何解决这个问题。另外,我应该在每个月后重置交易计数。我不知道该怎么做。如果有人可以将我推向正确的方向,将不胜感激。

这是我的银行账户代码:

public class BankAccount
{
private double balance;
private double fee;
private double freeTransactions;
private double transactionCount;

public BankAccount()
{
balance = 0;
fee = 5;
freeTransactions = 5;
transactionCount = 0;
}

public BankAccount(double initialBalance)
{
balance = initialBalance;
transactionCount = 0;
}

public void deposit(double amount)
{
double newBalance = balance + amount;
balance = newBalance;
transactionCount++;
}

public void withdraw(double amount)
{
double newBalance = balance - amount;
balance = newBalance;
transactionCount++;
}

public double getBalance()
{
return balance;
}

public void setTransFee(double amount)
{
balance = amount+(balance-fee);
balance = balance;
}

public void setNumFreeTrans(double amount)
{
amount = freeTransactions;
}

public double deductMonthlyCharge()
{
double transCount = Math.max(transactionCount, freeTransactions);
double fee = 2.00 * (transCount - freeTransactions);
return fee;
}
}

这是我的 BankAccountTester 代码:

public class BankAccountTester
{
private BankAccount rockdown;
public static void main(String[] args) {
BankAccount rockdown = new BankAccount(1000.0);
rockdown.deposit(1000);
rockdown.withdraw(500);
rockdown.withdraw(400);
rockdown.deposit(200);
System.out.println(rockdown.getBalance()- rockdown.deductMonthlyCharge());

rockdown.deposit(1000);
rockdown.withdraw(500);
rockdown.withdraw(400);
rockdown.deposit(200);
rockdown.deposit(500);
System.out.println(rockdown.getBalance()- rockdown.deductMonthlyCharge());

rockdown.deposit(1000);
rockdown.withdraw(500);
rockdown.withdraw(400);
rockdown.deposit(200);
rockdown.deposit(500);
rockdown.withdraw(1000);
System.out.println(rockdown.getBalance()- rockdown.deductMonthlyCharge());
}
}

最佳答案

您永远不会在非默认 构造函数中设置freeTransactions,因此它默认为0:

public BankAccount(double initialBalance) 

您可以像这样从重载的构造函数中调用您的默认构造函数:

public BankAccount(double initialBalance) {   
super();
balance = initialBalance;
}

以便调用语句 freeTransactions = 5;

关于java - 将 Math.max 用于 BankAccount 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14044316/

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