0

I am trying to place interest on bank transactions. The main goal is to try and have 0.05 or 5% on transactions over 2000. 0.02 or 2% as a flat rate for Savings accounts and 0% for a checking account.

So I have a check interest method that is placed before any deposit so the correct interest is given. I cant get the 0% to work without it overriding the other interests. My code looks like this as a default.

public void checkInterest(double amount){

 if(balance + amount <= 2000){

     interest = 0.02;
   }
    else{
        interest = 0.05; 
    }

My last attempt was this.

 public void checkInterest(double amount){

 if(balance + amount <= 2000){

     interest = 0.02;
   }
    else{
        interest = 0.05; 
    }
  if(Checking.accountType == "checking"){

     interest = 0.00;
 }
}

If I do this, whilst running it overrides and gives out a flat 0%. I also tried using Checking.accountType != null, but this also didn't work.

Any pointers? This is just for academic work. (also, I'm just a beginner so go easy)

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
  • 1
    Possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Henry Mar 27 '16 at 07:18
  • @Henry just switched it out for an .equals() and no luck. Perhaps something has gone wrong elsewhere in my code but it is hard to say because going through each method, it should be correct. – danielwishart Mar 27 '16 at 07:28
  • Step through the code with the debugger to see where it goes wrong. – Henry Mar 27 '16 at 08:11

1 Answers1

0

If the account type is a String, you should compare them like this:

if(Checking.accountType.equals("checking")){

Also, you might want to change your checkInterest method to return the value of interest.

 public double checkInterest(double amount){
   double interest;
   // Calculate interest rate here
   return interest;
 }
Adam Rice
  • 880
  • 8
  • 20