Created
September 16, 2014 09:19
-
-
Save AnthonyClink/1a0bc9e6e7e319ef3f90 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
If this is expected to be a non trivial application I would probably use the strategy pattern. | |
public class CommandLineCalculator{ | |
public static void main(String[] args){ | |
CommandLineCalculator commandLineCalculator = new CommandLineCalculator(); | |
int total = 0; | |
int lastValue = 0; | |
for(String arg : args){ | |
if(commandLineCalculator.isSymbol(arg)){ | |
total = | |
} | |
} | |
} | |
private Map<String, CalculationStrategy> strategies; | |
public CommandLineCalculator(){ | |
strategies = new HashMap<String, CalculationStrategy>(); | |
strategies.add("+", new AddStrategy()); | |
} | |
public boolean isSymbol(String possibleSymbol){ | |
return strategies.get(possibleSymbol) != null; | |
} | |
public static interface CalculationStrategy{ | |
public int calculate(int num1, int num2); | |
} | |
public static class AddStrategy implements CalculationStrategy{ | |
@Override | |
public int calculate(int num1, int num2){ | |
return num1 + num2; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment