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

这是我的BankAccount代码:

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/

10-14 09:05