Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Interest Calculator Design

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

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


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Interest Calculator Design


  3. #3
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default 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.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  4. #4
    Junior Member
    Join Date
    Apr 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Interest Calculator Design

    Quote Originally Posted by Chris.Brown.SPE View Post
    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?

  5. #5
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Interest Calculator Design

    Correct one map per year, good idea putting them in a list so you can find each one easily.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

Similar Threads

  1. How to Compute Interest
    By salsera1908 in forum Object Oriented Programming
    Replies: 5
    Last Post: December 4th, 2012, 02:38 PM
  2. Interest calculator applet
    By bruizer in forum Java Applets
    Replies: 4
    Last Post: September 22nd, 2012, 08:38 PM
  3. Working on a code to calculate interest rates...
    By ColeTrain in forum Java Theory & Questions
    Replies: 7
    Last Post: September 20th, 2012, 06:49 AM
  4. investment interest display problem
    By df75douglas in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 4th, 2011, 07:32 AM

Tags for this Thread