jeudi 14 avril 2016

Understanding of strategy pattern Java

I've been critised that I use procedure style of writing. So I decided to fix it. Now I work with Strategy pattern to replace if/else statement.

As one of the latest example I've chosen http://ift.tt/1NatbMm

As a result I decided to make evaluation of annual simple interest. The idea is the following: depending on amount of investing there are few rates that can be applied. Rates:

  • amount less than 100 - 1%
  • amount less than 1000 - 1.5%
  • amount less than 10000 - 2%
  • amount bigger than 10000 - 3%

My approach:

  • In the interface InterestEvaluation defined method getEvaluation.
  • In the classes FirstRate, SecondRate, ThirdRate, FourthRate realized interface InterestEvaluation. These classes contain evaluation for each rate.
  • In the class SimpleInterest created methods of choosing and calling of methods for annual amount evaluation.
  • In the classes FourthRateEvaluation, ThirdRateEvaluation, SecondRateEvaluation, FirstRateEvaluation created calling of necessary methods of evaluation annual amount depending on entered amount.

As a result, I did not have success in avoiding if/else statement, but maybe I made code more scalable.

Who had experience with this pattern, please, tell me is this code uses strategy pattern or I should change something? Thanks.

The code is below and on the google Drive - http://ift.tt/1SNZoFR

Calculator.java

package com.interestevaluation;

import java.util.Scanner;

public class Calculator {

public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);
    while (true) {
        try{
            System.out.println("Enter amount to calculate");
            String giveninvest =  scanner.next();

            StringParsingHelper stringParser = new StringParsingHelper();
            double invest = stringParser.getNumericAmount(giveninvest);

            if (invest <= 0)
                throw new IllegalArgumentException("Illegal parameter. Amount " + invest + " <= 0 ");

            if (invest < 100) {
                FirstRateEvaluation firstRateEvaluation = new FirstRateEvaluation();
                System.out.println("the first case: " + firstRateEvaluation.tryGetInterestEvaluation(invest));
                //example of dynamic change evaluation for particular case
                firstRateEvaluation.setInterestEvaluation(new ThirdRate());
                System.out.println("the first case modified: " + firstRateEvaluation.tryGetInterestEvaluation(invest));

            } else if(invest < 1000) {
                SecondRateEvaluation secondRateEvaluation = new SecondRateEvaluation();
                System.out.println("the second case: " + secondRateEvaluation.tryGetInterestEvaluation(invest));
            } else if(invest < 10000) {
                ThirdRateEvaluation thirdRateEvaluation = new ThirdRateEvaluation();
                System.out.println("the third case: " + thirdRateEvaluation.tryGetInterestEvaluation(invest));
            } else {
                FourthRateEvaluation fourthRateEvaluation = new FourthRateEvaluation();
                System.out.println("the fourth case: " + fourthRateEvaluation.tryGetInterestEvaluation(invest));
            }

            System.out.println();
        } catch (IllegalArgumentException ie) {
            System.out.println(ie);
            System.out.println("Bad input value. Try again");
            System.out.println();
        }
    }

}
}

InterestEvaluation.java

package com.interestevaluation;

public interface InterestEvaluation {
double getEvaluation(double invest);
}


class FirstRate implements InterestEvaluation {

public double getEvaluation(double invest){return invest*0.01 + invest;}
}


class SecondRate implements InterestEvaluation {

public double getEvaluation(double invest){
    return invest*0.015 + invest;
}
}


class ThirdRate implements InterestEvaluation {

public double getEvaluation(double invest){
    return invest*0.02 + invest;
}
}


class FourthRate implements InterestEvaluation {

public double getEvaluation(double invest){
    return invest*0.03 + invest;
}
}

SimpleInterest.java

package com.interestevaluation;

public class SimpleInterest {

public InterestEvaluation interestEvaluation;

public void setInterestEvaluation(InterestEvaluation newInterestEvaluation){
    interestEvaluation = newInterestEvaluation;
}

public double tryGetInterestEvaluation(double invest){
    return interestEvaluation.getEvaluation(invest);
}

}

FirstRateEvaluation.java

package com.interestevaluation;

public class FirstRateEvaluation extends SimpleInterest{

public FirstRateEvaluation(){
    super();
    interestEvaluation = new FirstRate();
}
}

SecondRateEvaluation.java

package com.interestevaluation;

public class SecondRateEvaluation extends SimpleInterest {

public SecondRateEvaluation(){
    super();
    interestEvaluation = new SecondRate();
}
}

ThirdRateEvaluation.java

package com.interestevaluation;

public class ThirdRateEvaluation extends SimpleInterest{

public ThirdRateEvaluation(){
    super();
    interestEvaluation = new ThirdRate();
}
}

FourthRateEvaluation.java

package com.interestevaluation;

public class FourthRateEvaluation extends SimpleInterest{

public FourthRateEvaluation(){
    super();
    interestEvaluation = new FourthRate();
}
}

StringParsingHelper.java

package com.interestevaluation;

public class StringParsingHelper {

protected double getNumericAmount(final String amount) throws NumberFormatException{
    try {
        return Double.parseDouble(amount);
    } catch (NumberFormatException e) {
        throw new NumberFormatException("Failed to parse a non-numeric argument: " + amount);
    }
}
}

Aucun commentaire:

Enregistrer un commentaire