Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Interest Calculator Design

Options
  • 03-04-2013 5:09pm
    #1
    Registered Users Posts: 22


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


Comments

  • Registered Users Posts: 22 AbuseMePlz


    How would I extend the program to cater for different interest rates depending on the age of a customer account, e.g.

    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+


  • Registered Users Posts: 22 AbuseMePlz


    Anyone?


  • Registered Users Posts: 1,931 ✭✭✭PrzemoF


    [I'm not Java programmer]

    - do not hardcode bands/ interest rates - use variables
    - why new BigDecimal ("100") and new BigDecimal(1000)?
    - do you really have to use multiply and divide for applying interest? It breaks the Keep It Simple and Stupid rule of programming
    - I don't know if java allows to use switch-case statements on ranges, but that would be much cleaner solution IMHO [just checked: it doesn't, so I have another reason to avoid java ;-) ]
    - consider using some lookup table: account age / bands / rates


  • Registered Users Posts: 22 AbuseMePlz


    PrzemoF wrote: »
    - why new BigDecimal ("100") and new BigDecimal(1000)?

    You're right. That's a typo.
    PrzemoF wrote: »
    - do you really have to use multiply and divide for applying interest? It breaks the Keep It Simple and Stupid rule of programming

    Can you elaborate on this? I cannot think of another way to calculate the interest..


  • Registered Users Posts: 1,931 ✭✭✭PrzemoF


    I mean you're using "divide by 100" to calculate 1% and "multiply by 0.02" to calculate 2%. Use one or the other way, but not both in the same piece of code.


  • Advertisement
  • Registered Users Posts: 22 AbuseMePlz


    I am thinking about using an array of mapping tables (ArrayList with each element a Hashmap).

    The only issue I have is that some bands cater for unlimited values, e.g. 3% for £5000+.


  • Moderators, Technology & Internet Moderators Posts: 1,334 Mod ✭✭✭✭croo


    This might interest you...


  • Registered Users Posts: 2,781 ✭✭✭amen


    you should check the biggest value first i.e.
    all of 5000> then get more restrictive


  • Registered Users Posts: 22 AbuseMePlz


    Anyone else got any ideas?


  • Registered Users Posts: 22 AbuseMePlz


    Does anyone think reading the rates from a text file would be a good solution?


  • Advertisement
  • Registered Users Posts: 2,022 ✭✭✭Colonel Panic


    Solution to what? If the program is working and you want to enhance it, go ahead and add file loading support.


  • Registered Users Posts: 882 ✭✭✭moneymad


    AbuseMePlz wrote: »
    Anyone else got any ideas?

    Why don't you stop thinking in "java" and think in english?
    It's not that hard to write down what you need to do in english.


  • Registered Users Posts: 22 AbuseMePlz


    moneymad wrote: »
    Why don't you stop thinking in "java" and think in english?
    It's not that hard to write down what you need to do in english.

    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.


  • Registered Users Posts: 882 ✭✭✭moneymad


    AbuseMePlz wrote: »
    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.

    keep it as simple as you can in English first.
    would you use a conveyor belt to find something you could have put in a separate bucket? stop making it complicated.


  • Registered Users Posts: 22 AbuseMePlz


    moneymad wrote: »
    keep it as simple as you can in English first.
    would you use a conveyor belt to find something you could have put in a separate bucket? stop making it complicated.

    Thanks for your reply.

    Are you suggesting my process of storing the rate information is too complicated and that I should really be hardcoding the values?


Advertisement