简体   繁体   中英

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. 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:

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:

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 :

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; 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. 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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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