# Interest Calculator Design

• 04-09-2013, 09:19 PM
heyya99
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.

Any help is welcome.

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;         }```
• 04-09-2013, 11:49 PM
Ronin
Re: Interest Calculator Design
Hi heyya99, welcome to the forums.

A quick and easy solution would be be to pass another argument into the method which says how long the customer has been with the bank or the date they joined. You could the perform calcations based on the value which has been passed in.

Another option would be to create a Customer object which contains the date the customer joined the bank. You could the perform the calcualtion based on the difference between when the customer joined and the current date. The benefit of creating a Customer class is that you could also add additonal values and properties such as name, marital status, date of birth, etc.

Regards.
• 04-10-2013, 11:51 AM
heyya99
Re: Interest Calculator Design
Quote:

Originally Posted by Ronin
Hi heyya99, welcome to the forums.

A quick and easy solution would be be to pass another argument into the method which says how long the customer has been with the bank or the date they joined. You could the perform calcations based on the value which has been passed in.

Another option would be to create a Customer object which contains the date the customer joined the bank. You could the perform the calcualtion based on the difference between when the customer joined and the current date. The benefit of creating a Customer class is that you could also add additonal values and properties such as name, marital status, date of birth, etc.

Regards.

Thanks!

I'm trying to find a method of storing the years with bank, rate and money threshold. If a bank manager decided to add a new rate, I'd like to allow them to do it with minimal changes. I've been thinking of a map (years as key and an array as value). The array could hold the threshold and the rate. What do you think of that? The solution has to be simple.
• 04-10-2013, 12:02 PM
Ronin
Re: Interest Calculator Design
That solution could work but there are a couple of things to bear in mind.
You want the array to store both the rate and thresholds which would mean that for each new entry you would be adding two elements in the array. As such using the indexes to represent the number of years means you would have to halve this value and add one to get the number of years.
Instead you could use an array of Rate objects which contains all of the relevant information.

When you said you want the solution to be simple, is there a specific reason for this?

Regards.
• 04-10-2013, 12:13 PM
heyya99
Re: Interest Calculator Design
Quote:

Originally Posted by Ronin
That solution could work but there are a couple of things to bear in mind.
You want the array to store both the rate and thresholds which would mean that for each new entry you would be adding two elements in the array. As such using the indexes to represent the number of years means you would have to halve this value and add one to get the number of years.
Instead you could use an array of Rate objects which contains all of the relevant information.

When you said you want the solution to be simple, is there a specific reason for this?

Regards.

I wasn't suggesting using an array index for the year, I meant having a map with the year for it's key, then each value would be a 2 element array (threshold and rate). Does that make sense?

The app is for a job interview and I've been told overly complicated solutions are being rejected.

Do you think reading rates from a text file is a good solution?
• 04-10-2013, 12:43 PM
Ronin
Re: Interest Calculator Design
Ah, I see. I would not consider using simple objects as complicated, by that definition anything beyond a simple "Hello World" program is complicated.

As for the text file, if the application needs a way to store the information when it is not running then you would need a file to store the information in. Using objects in a similar way may mean they need to be serializable to store them in files but is this considered complicated?

An interview is an opportunity to demonstrate your abilities. Can you get the job done without the need to "show off" but create something which achieves the requirements.
Your overall solution depends on your initial brief and no two solutions are likely to be the same. At a guess the interviewers would be looking for your ideas and the way you approached the solution as much as your final implementation.

Regards.
• 04-10-2013, 01:00 PM
heyya99
Re: Interest Calculator Design
Quote:

Originally Posted by Ronin
Ah, I see. I would not consider using simple objects as complicated, by that definition anything beyond a simple "Hello World" program is complicated.

As for the text file, if the application needs a way to store the information when it is not running then you would need a file to store the information in. Using objects in a similar way may mean they need to be serializable to store them in files but is this considered complicated?

An interview is an opportunity to demonstrate your abilities. Can you get the job done without the need to "show off" but create something which achieves the requirements.
Your overall solution depends on your initial brief and no two solutions are likely to be the same. At a guess the interviewers would be looking for your ideas and the way you approached the solution as much as your final implementation.

Regards.

I hear you.

Here's my suggestion. I create an class called Rate with 2 variables: rate and threshold. I then create an Array that holds each rate. To find the correct interest rate, I use the array's index to find the correct Rate object. When I've found the object, I use the balance threshold to find the rate.

If a new rate is to be added, a create a new Rate object and add it to the ArrayList.
• 04-10-2013, 02:19 PM
Ronin
Re: Interest Calculator Design
Just thinking about it, you may also be able to use enums instead of objects.

Regards.
• 04-10-2013, 02:22 PM
heyya99
Re: Interest Calculator Design
Quote:

Originally Posted by Ronin
Just thinking about it, you may also be able to use enums instead of objects.

Regards.

But how would I store those objects or enums? Map?
• 04-10-2013, 03:24 PM
Ronin
Re: Interest Calculator Design
A Map is certainly one way to do it, you could also use a simple array.
Whatever solution you use you need to bear in mind how you are going to reference the correct element. I'm not going say use one method over another as this is your project for your interview. When you're asked why you chose to use a Map or array over another method, you will need to validate your decision and I don't think "Because someone on the internet told me to" would suffice.

Regards.
• 04-10-2013, 03:32 PM
heyya99
Re: Interest Calculator Design
Quote:

Originally Posted by Ronin
A Map is certainly one way to do it, you could also use a simple array.
Whatever solution you use you need to bear in mind how you are going to reference the correct element. I'm not going say use one method over another as this is your project for your interview. When you're asked why you chose to use a Map or array over another method, you will need to validate your decision and I don't think "Because someone on the internet told me to" would suffice.

Regards.

Thanks. I get you.

Do you think a Strategy pattern would be a good solution?
• 04-10-2013, 10:31 PM
Ronin
Re: Interest Calculator Design
I would say it might be slightly above their requirements for the assignment but then I'm not assessing it.

Regards.
• 04-10-2013, 10:45 PM
heyya99
Re: Interest Calculator Design
Quote:

Originally Posted by Ronin
I would say it might be slightly above their requirements for the assignment but then I'm not assessing it.

Regards.

Agreed. I have a working version finished where I have a a map of (year:arrayList) and each array list has a rate class. I find the right array list by passing in the year. I then loop over the rate objects and find the rate that my balance corresponds to.

To add a new year and subsequent set of bands, I need to create a new rate arrayList and add it to the map, e.g. (3:new ArrayList). To me that's still a bit off effort to have to add a new band. Or am I expecting too much?
• 04-12-2013, 01:41 PM
Ronin
Re: Interest Calculator Design
I don't think you are as long as you can validate your design decision.

Oh, and good luck.

Regards.