Interest Calculator Design

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

1% - £0 to £1000

2% - £1000 to £5000

3% - £5000+

I would like to amend the program to cater for different bands for different customers. If a customer is with the bank for a period of time, she gets a better rate. Here are the new rates:

After one year:

1% - £0 to £1000

2.5% - £1000 to £5000

4% - £5000+

After two years:

2% - £0 to £1000

3% - £1000 to £5000

4% - £5000 to £10000

5% - £10000+

My initial approach is below. Can anyone suggest how I'd go about implementing the additional part? I want the program to be as simple as possible with minimal complexity. I don't want an all-compassing solution, just something that works well and is extensible.

Code :

private static BigDecimal ONE_PERCENT = new BigDecimal("0.01");
private static BigDecimal TWO_PERCENT = new BigDecimal("0.02");
private static BigDecimal THREE_PERCENT = new BigDecimal("0.03");
private static BigDecimal ONE_THOUSAND = new BigDecimal("1000");
private static BigDecimal FIVE_THOUSAND = new BigDecimal("5000");
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;
}

Re: Interest Calculator Design

Re: Interest Calculator Design

If you want it really really basic, have your first function determine how long they have been part of the bank then have two separate functions for each set of bands.

If you really wanted to do it properly and have it be dynamic, you should have a map of amounts to rates. You could then iterate through the map and when you find the range your value is in, get the rate. You could then add as many rates as you would like and it would always work. Then just have two separate maps for each set of rates. I would be interested in helping you work through this if you are interested.

Re: Interest Calculator Design

Quote:

Originally Posted by

**Chris.Brown.SPE**
If you want it really really basic, have your first function determine how long they have been part of the bank then have two separate functions for each set of bands.

If you really wanted to do it properly and have it be dynamic, you should have a map of amounts to rates. You could then iterate through the map and when you find the range your value is in, get the rate. You could then add as many rates as you would like and it would always work. Then just have two separate maps for each set of rates. I would be interested in helping you work through this if you are interested.

Thanks. Your solution is what I have been thinking of too. I was planning on adding each map to an ArrayList so that the year would be the index of the ArrayList(year-1).

When you say 2 seperate maps, do you mean having one map per year so if I had a rate for each year up to 10, I'd have 10 rate maps?

Re: Interest Calculator Design

Correct one map per year, good idea putting them in a list so you can find each one easily.