将Math.max用于BankAccount类

时间:2012-12-26 18:03:46

标签: java

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

4 个答案:

答案 0 :(得分:3)

您从未在非默认构造函数中设置freeTransactions,因此默认为0

public BankAccount(double initialBalance) 

你可以像这样调用你的默认构造函数:

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

以便调用语句freeTransactions = 5;

答案 1 :(得分:2)

你错过了一个构造函数的几行。

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

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

答案 2 :(得分:0)

Math.max(a,b)是正确的,并返回更大的值。 如果有计算费用,您可能希望将方法更改为仅计算费用,因此没有剩余的免费交易。

顺便说一下。 freeTransactionsfee应通过声明或构造来设置。

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

或者我错了?

答案 3 :(得分:0)

我会这样写,而不是尝试使用数学。

if (transactionCount <= freeTransactions)
   return 0;
return 2 * (transactionCount - freeTransactions);