简体   繁体   English

将Math.max用于BankAccount类

[英]using Math.max for a BankAccount class

I have a homework problem in which I have to deduct a fee for each transaction in a bank account which goes over the allotted number of free transactions. 我有一个家庭作业问题,我必须扣除银行帐户中每笔交易的费用,该笔费用超过了免费交易的分配数量。 My problem is that my Math.max is not working for the deductMonthlyCharge class, instead of applying the charge only when the transaction go over the allotted amount, the program is charging the fee for EVERY transaction. 我的问题是我的Math.max不适用于deductMonthlyCharge类,而不是仅在交易超过分配的金额时才应用费用,该程序正在为每笔交易收取费用。 I have no idea how to fix this. 我不知道该如何解决。 Also, I am supposed to reset the transaction count after every month. 另外,我应该每个月后重置交易计数。 I have no idea how to do this. 我不知道该怎么做。 If someone could nudge me in the right direction, it would be greatly appreciated thanks. 如果有人能向正确的方向推动我,将不胜感激。

Here is my BankAccount code: 这是我的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;
   }
}

Here's my BankAccountTester code: 这是我的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());
        }
    } 

You never set the freeTransactions in your non-default constructor so it defaults to 0 : 您永远不会在非默认构造函数中设置freeTransactions ,因此它默认为0

public BankAccount(double initialBalance) 

You could call your default constructor from the overloaded one like so: 您可以像这样从重载的调用默认构造函数:

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

so that the statement freeTransactions = 5; 这样语句freeTransactions = 5; is called. 叫做。

You're missing a couple of lines from one of your constructors. 您缺少其中一个构造函数的几行内容。

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

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

Math.max(a,b) is correct and returns the greater value. Math.max(a,b)是正确的,并返回较大的值。 You may want to change your method to only calc a fee, if there is a fee to calc, thus there are not free transactions left. 您可能希望将方法更改为仅计算费用(如果要计算费用),因此就没有免费交易了。

Btw. 顺便说一句。 freeTransactions and fee should be set by declaration or in construction. freeTransactionsfee应该通过声明或在构建中设置。

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

Or am I wrong? 还是我错了?

I would write it this way instead of trying to use Math. 我会这样写,而不是尝试使用数学。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM