Skip to content

Instantly share code, notes, and snippets.

@AnthonyClink
Created September 16, 2014 09:19
Show Gist options
  • Save AnthonyClink/1a0bc9e6e7e319ef3f90 to your computer and use it in GitHub Desktop.
Save AnthonyClink/1a0bc9e6e7e319ef3f90 to your computer and use it in GitHub Desktop.
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