Skip to main content
added 51 characters in body; edited tags; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Interest calculator design

I have written a program that, given an amount, returns the interest you would receive on that figure. There are 3 interest rate bands as follows:

  • 1% - £0 to £1000
  • 2% - £1000 to £5000
  • 3% - £5000+

Can anyone tell me if the following is a good solution or are the 'if'if statements too restrictive?

 public BigDecimal calc(BigDecimal amt) { if(amt.compareTo(new BigDecimal(1001))==-1){ interest = amt.divide(new BigDecimal("100"));   }else if(amt.compareTo(new BigDecimal(5001))==-1){ interest = amt.multiply(0.02);   }else{ interest = amt.multiply(0.03); }  return interest; } 

Interest calculator design

I have written a program that, given an amount, returns the interest you would receive on that figure. There are 3 interest rate bands as follows:

  • 1% - £0 to £1000
  • 2% - £1000 to £5000
  • 3% - £5000+

Can anyone tell me if the following is a good solution or are the 'if' statements too restrictive?

 public BigDecimal calc(BigDecimal amt) { if(amt.compareTo(new BigDecimal(1001))==-1){ interest = amt.divide(new BigDecimal("100"));   }else if(amt.compareTo(new BigDecimal(5001))==-1){ interest = amt.multiply(0.02);   }else{ interest = amt.multiply(0.03); }  return interest; } 

Interest calculator

I have written a program that, given an amount, returns the interest you would receive on that figure. There are 3 interest rate bands as follows:

  • 1% - £0 to £1000
  • 2% - £1000 to £5000
  • 3% - £5000+

Can anyone tell me if the following is a good solution or are the if statements too restrictive?

public BigDecimal calc(BigDecimal amt) { if(amt.compareTo(new BigDecimal(1001))==-1){ interest = amt.divide(new BigDecimal("100")); }else if(amt.compareTo(new BigDecimal(5001))==-1){ interest = amt.multiply(0.02); }else{ interest = amt.multiply(0.03); } return interest; } 
Source Link
TheCoder
  • 223
  • 2
  • 7

Interest calculator design

I have written a program that, given an amount, returns the interest you would receive on that figure. There are 3 interest rate bands as follows:

  • 1% - £0 to £1000
  • 2% - £1000 to £5000
  • 3% - £5000+

Can anyone tell me if the following is a good solution or are the 'if' statements too restrictive?

 public BigDecimal calc(BigDecimal amt) { if(amt.compareTo(new BigDecimal(1001))==-1){ interest = amt.divide(new BigDecimal("100")); }else if(amt.compareTo(new BigDecimal(5001))==-1){ interest = amt.multiply(0.02); }else{ interest = amt.multiply(0.03); } return interest; }