Skip to content

Commit

Permalink
Replace a switch by a Map in ConsoleUI.java
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoineJT committed Jun 15, 2019
1 parent e098637 commit 8590b6b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
40 changes: 21 additions & 19 deletions src/com/github/antoinejt/jasc/ConsoleUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,31 @@

final class ConsoleUI {
private static final List<String> commands = new ArrayList<>();
private static final Map<String, FunctionType> functions = new HashMap<>();
private static final Map<String, OperationType> operators = new HashMap<>();
static {
functions.put("sqrt", FunctionType.SQRT);
functions.put("log", FunctionType.LOG10);
functions.put("ln", FunctionType.LN);
functions.put("lb", FunctionType.LOGB);
functions.put("cos", FunctionType.COS);
functions.put("sin", FunctionType.SIN);
functions.put("tan", FunctionType.TAN);
functions.put("arccos", FunctionType.ARCCOS);
functions.put("arcsin", FunctionType.ARCSIN);
functions.put("arctan", FunctionType.ARCTAN);
functions.put("exp", FunctionType.EXP);

operators.put("+", OperationType.ADDITION);
operators.put("-", OperationType.SUBSTRACTION);
operators.put("*", OperationType.MULTIPLICATION);
operators.put("/", OperationType.DIVISION);
operators.put("%", OperationType.MODULO);
operators.put("^", OperationType.POWER);

commands.addAll(functions.keySet()); // functions are added here
commands.addAll(operators.keySet()); // operators are added here
commands.addAll(Arrays.asList(
"sqrt", "log", "ln", "lb", "cos", "sin", "tan", "arccos", "arcsin", "arctan", "exp", // Functions
"=", "help", "clear", "quit" // Commands
));
}
Expand Down Expand Up @@ -64,7 +77,7 @@ private static void displayIntro(){
"Version : " + Constants.VERSION,
"Last update : " + Constants.LAST_UPDATE,
"--------------------------------",
"This calculator uses a stack, so you must defines at least 2 numbers before using some calculation operator",
"This calculator uses a stack, so you must define at least 2 numbers before using some calculation operator",
"You must type numbers with or without a dot, not a comma",
"To know available commands, you can type help");
}
Expand All @@ -85,8 +98,7 @@ public static void useConsole() throws Exception {
String input;
while (true){
input = scanner.next();
boolean hasInputNumber = !commands.contains(input);
if (hasInputNumber){
if (!commands.contains(input)){ // if it's a number
try {
float number = Float.parseFloat(input);
calculatorEngine.addNumber(number);
Expand All @@ -110,22 +122,12 @@ public static void useConsole() throws Exception {
default: isFunction = true;
}
if (isFunction){
FunctionType functionType;
switch(input){ // TODO Maybe implement that with a simple private enum
case "sqrt": functionType = FunctionType.SQRT; break;
case "log": functionType = FunctionType.LOG10; break;
case "ln": functionType = FunctionType.LN; break;
case "lb": functionType = FunctionType.LOGB; break;
case "cos": functionType = FunctionType.COS; break;
case "sin": functionType = FunctionType.SIN; break;
case "tan": functionType = FunctionType.TAN; break;
case "arccos": functionType = FunctionType.ARCCOS; break;
case "arcsin": functionType = FunctionType.ARCSIN; break;
case "arctan": functionType = FunctionType.ARCTAN; break;
case "exp": functionType = FunctionType.EXP; break;
default: throw new Exception("This is not good to corrupt my list, hack3rm4n!");
if (functions.containsKey(input)) {
FunctionType functionType = functions.get(input);
calculatorEngine.applyFunction(functionType);
} else {
throw new Exception("This is not good to corrupt my list, hack3rm4n!");
}
calculatorEngine.applyFunction(functionType);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/com/github/antoinejt/jasc/Constants.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.github.antoinejt.jasc;

final class Constants {
static final String VERSION = "0.3.3";
static final String LAST_UPDATE = "June 11th 2019";
static final String VERSION = "0.3.4";
static final String LAST_UPDATE = "June 15th 2019";
}

0 comments on commit 8590b6b

Please sign in to comment.