1

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

4 回答 4

3

你从来没有freeTransactions在你的非默认构造函数中设置,所以它默认为0

public BankAccount(double initialBalance) 

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

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

以便freeTransactions = 5;调用该语句。

于 2012-12-26T18:10:24.987 回答
2

您的一个构造函数中缺少几行代码。

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

   public BankAccount(double initialBalance)
   {   
      balance = initialBalance;
      fee = 5;
      freeTransactions = 5;
      transactionCount = 0;
   }
于 2012-12-26T18:12:44.077 回答
0

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

if (transactionCount <= freeTransactions)
   return 0;
return 2 * (transactionCount - freeTransactions);
于 2012-12-26T18:37:54.860 回答
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;
   }

还是我错了?

于 2012-12-26T18:16:04.460 回答