• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Interest Calculator Design

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Marshal
Posts: 16591
277
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Seems to me that the Strategy Pattern is what you're looking for
 
Junilu Lacar
Marshal
Posts: 16591
277
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, you have declared some pseudo constants--I say "pseudo" because they follow the naming convention but they're not declared as final--but you fail to use them in your calculations, preferring to use hard-coded values instead.
 
David McWilliams
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Also, you have declared some pseudo constants--I say "pseudo" because they follow the naming convention but they're not declared as final--but you fail to use them in your calculations, preferring to use hard-coded values instead.



Thanks - I'll fix that.

I'll have a read on Strategy pattern. Do you think implementing a strategy is over-complicating it? I'm not saying it is, I'm just trying to be careful not to over complicate the solution.
 
Marshal
Posts: 74025
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also do not use -1. How do you know you will get -1 back from the compareTo() method? If you look at its documentation, it says nothing about returning 1 or -1. It says negative positive or 0. It says signum returns -1 0 1, but you are not using signum.
 
Campbell Ritchie
Marshal
Posts: 74025
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David McWilliams wrote: . . . Do you think implementing a strategy is over-complicating it? . . .

Probably not. At present you have hard-coded values, so changing the rules would be awkward.
 
David McWilliams
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Also do not use -1. How do you know you will get -1 back from the compareTo() method? If you look at its documentation, it says nothing about returning 1 or -1. It says negative positive or 0. It says signum returns -1 0 1, but you are not using signum.



Thanks. I have changed that.
 
Campbell Ritchie
Marshal
Posts: 74025
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you find the version of compareTo in the BigDecimal documentation, it gives suggestions about what to use.
 
David McWilliams
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've been reading up on the Strategy pattern. A different strategy is chosen based on the specific data inputted. Am I correct in saying I should have a separate strategy for each year?
 
Junilu Lacar
Marshal
Posts: 16591
277
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David McWilliams wrote:Do you think implementing a strategy is over-complicating it? I'm not saying it is, I'm just trying to be careful not to over complicate the solution.


It totally depends on how you implement it but no, I don't think it would overcomplicate things if you exercise restraint and good judgement. I like the approach of refactoring towards a pattern. That is, start with simple code, like what you have, with no particular pattern in mind. When you detect duplication and hard-coding, the desire to relieve these kinds of "tensions" in the design is what drives you to refactor. Usually you will add abstractions and parameterize. This is where your code will start looking like the pattern and you use the idea of the pattern to guide you in further refactoring, as you "feel" your way to a better design.
 
Junilu Lacar
Marshal
Posts: 16591
277
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David McWilliams wrote:Am I correct in saying I should have a separate strategy for each year?


That's probably too fine-grained for what you described. I would go for a strategy for each type of customer. That is, however many different interest tables you have, that would be how many strategies you'd have to implement and choose from.

Edit: Sorry, I thought you meant something else when I read "separate strategy for each year".

Another way might be to base it on the different sets of "bands" you have, then just parameterize the interest rates. In the examples you gave, you'd have two strategies, with the strategy with three bands being parameterized for it's interest rates. That might be over-complicating things though. I usually experiment with different approaches before I decide which one to go with. Seeing the code gives you a better feel for how maintainable and understandable it is vs. just thinking about it.
 
David McWilliams
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

David McWilliams wrote:Am I correct in saying I should have a separate strategy for each year?


That's probably too fine-grained for what you described. I would go for a strategy for each type of customer. That is, however many different interest tables you have, that would be how many strategies you'd have to implement and choose from.

Edit: Sorry, I thought you meant something else when I read "separate strategy for each year".

Another way might be to base it on the different sets of "bands" you have, then just parameterize the interest rates. In the examples you gave, you'd have two strategies, with the strategy with three bands being parameterized for it's interest rates. That might be over-complicating things though. I usually experiment with different approaches before I decide which one to go with. Seeing the code gives you a better feel for how maintainable and understandable it is vs. just thinking about it.



Would that be 7 strategies for my example then? Sorry about the questions. I'm new to this game and am not sure how to implement this strategy approach.
 
Junilu Lacar
Marshal
Posts: 16591
277
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David McWilliams wrote:Would that be 7 strategies for my example then? Sorry about the questions. I'm new to this game and am not sure how to implement this strategy approach.


No, based on the example you gave, it would be three or two, depending on whether or not you decide to parameterize the interest rates for the table with three bands.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic