我有一个家庭作业问题,我必须为银行账户中的每笔交易扣除超过分配的免费交易数量的费用。我的问题是我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());
}
}