From d793990b8cf40d4e63ea2b1dd5466b305817d5a8 Mon Sep 17 00:00:00 2001 From: Sunny Date: Mon, 12 Jun 2023 21:37:15 +0900 Subject: [PATCH 1/9] =?UTF-8?q?REFACTOR=20:=20=EC=B6=9C=EB=A0=A5=20?= =?UTF-8?q?=EB=B6=80=EB=B6=84=20=EB=AA=A8=EB=91=90=20Console=EB=A1=9C=20?= =?UTF-8?q?=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/programmers/BasicCalculator.java | 6 ++---- src/main/java/com/programmers/Console.java | 17 +++++++++++++++++ .../com/programmers/engine/JavaCalculator.java | 9 +++++++-- .../java/com/programmers/engine/io/Output.java | 4 ++++ .../com/programmers/engine/model/Result.java | 14 ++++---------- 5 files changed, 34 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/programmers/BasicCalculator.java b/src/main/java/com/programmers/BasicCalculator.java index 4bb03b5df..3a9d536ab 100644 --- a/src/main/java/com/programmers/BasicCalculator.java +++ b/src/main/java/com/programmers/BasicCalculator.java @@ -4,10 +4,8 @@ import java.util.Stack; public class BasicCalculator { - public String doCalculate(String expression) { - int answer = toPostfix(expression); - System.out.println(answer); - return expression + " = " + String.valueOf(answer); + public int doCalculate(String expression) { + return toPostfix(expression); } private int getPriority(String operator) { diff --git a/src/main/java/com/programmers/Console.java b/src/main/java/com/programmers/Console.java index bf162d8bb..3d3feefb4 100644 --- a/src/main/java/com/programmers/Console.java +++ b/src/main/java/com/programmers/Console.java @@ -3,6 +3,7 @@ import com.programmers.engine.io.Input; import com.programmers.engine.io.Output; +import java.util.Map; import java.util.Scanner; public class Console implements Input, Output { @@ -28,4 +29,20 @@ public String getExpression() { System.out.println("계산하고자 하는 식을 입력해주세요!"); return sc.nextLine(); } + + @Override + public void readAllResults(Map map) { + if (map.isEmpty()) { + System.out.println("계산 이력이 없습니다."); + return; + } + System.out.println(); + map.forEach((key, value) -> System.out.println(value)); + System.out.println(); + } + + @Override + public void printAnswer(int answer) { + System.out.println(answer); + } } diff --git a/src/main/java/com/programmers/engine/JavaCalculator.java b/src/main/java/com/programmers/engine/JavaCalculator.java index 3a54b7b1a..f7ea13004 100644 --- a/src/main/java/com/programmers/engine/JavaCalculator.java +++ b/src/main/java/com/programmers/engine/JavaCalculator.java @@ -19,8 +19,13 @@ public void run() { while (isExecutable) { output.showMenu(); switch (input.selectMenu()) { - case 1 -> result.readAllResults(); - case 2 -> result.save(bc.doCalculate(input.getExpression())); + case 1 -> output.readAllResults(result.readAllResults()); + case 2 -> { + String expression = input.getExpression(); + int answer = bc.doCalculate(expression); + output.printAnswer(answer); + result.save(expression, answer); + } case 3 -> isExecutable = false; default -> output.inputError(); } diff --git a/src/main/java/com/programmers/engine/io/Output.java b/src/main/java/com/programmers/engine/io/Output.java index 9b1a0303b..3d280928a 100644 --- a/src/main/java/com/programmers/engine/io/Output.java +++ b/src/main/java/com/programmers/engine/io/Output.java @@ -1,6 +1,10 @@ package com.programmers.engine.io; +import java.util.Map; + public interface Output { void showMenu(); void inputError(); + void readAllResults(Map map); + void printAnswer(int answer); } diff --git a/src/main/java/com/programmers/engine/model/Result.java b/src/main/java/com/programmers/engine/model/Result.java index 334b90c02..1574cac50 100644 --- a/src/main/java/com/programmers/engine/model/Result.java +++ b/src/main/java/com/programmers/engine/model/Result.java @@ -6,17 +6,11 @@ public class Result { private final Map results = new HashMap<>(); - public void save(String result) { - results.put(results.size() + 1, result); + public void save(String expression, int answer) { + results.put(results.size() + 1, expression + " = " + String.valueOf(answer)); } - public void readAllResults() { - if (results.size() == 0) { - System.out.println("계산 이력이 없습니다."); - return; - } - System.out.println(); - results.forEach((key, value) -> System.out.println(value)); - System.out.println();; + public Map readAllResults() { + return results; } } From 80ccd94db45c7603bb1260ad4e8524f1ceebcf7e Mon Sep 17 00:00:00 2001 From: Sunny Date: Mon, 12 Jun 2023 21:39:40 +0900 Subject: [PATCH 2/9] =?UTF-8?q?REFACTOR=20:=20Result=20=ED=81=B4=EB=9E=98?= =?UTF-8?q?=EC=8A=A4=20=EC=A0=81=EC=A0=88=ED=95=9C=20=EC=9D=B4=EB=A6=84?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EB=B0=94=EA=BE=B8=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/programmers/App.java | 6 +++--- src/main/java/com/programmers/engine/JavaCalculator.java | 8 ++++---- .../engine/model/{Result.java => ResultManager.java} | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) rename src/main/java/com/programmers/engine/model/{Result.java => ResultManager.java} (92%) diff --git a/src/main/java/com/programmers/App.java b/src/main/java/com/programmers/App.java index 3b2e00b5d..e686054b4 100644 --- a/src/main/java/com/programmers/App.java +++ b/src/main/java/com/programmers/App.java @@ -1,14 +1,14 @@ package com.programmers; import com.programmers.engine.JavaCalculator; -import com.programmers.engine.model.Result; +import com.programmers.engine.model.ResultManager; public class App { public static void main(String[] args) { Console console = new Console(); - Result result = new Result(); + ResultManager resultManager = new ResultManager(); BasicCalculator bc = new BasicCalculator(); - new JavaCalculator(console, console, result, bc).run(); + new JavaCalculator(console, console, resultManager, bc).run(); } } diff --git a/src/main/java/com/programmers/engine/JavaCalculator.java b/src/main/java/com/programmers/engine/JavaCalculator.java index f7ea13004..9e59c1240 100644 --- a/src/main/java/com/programmers/engine/JavaCalculator.java +++ b/src/main/java/com/programmers/engine/JavaCalculator.java @@ -3,14 +3,14 @@ import com.programmers.BasicCalculator; import com.programmers.engine.io.Input; import com.programmers.engine.io.Output; -import com.programmers.engine.model.Result; +import com.programmers.engine.model.ResultManager; import lombok.AllArgsConstructor; @AllArgsConstructor public class JavaCalculator implements Runnable{ private final Input input; private final Output output; - private final Result result; + private final ResultManager resultManager; private final BasicCalculator bc; @Override @@ -19,12 +19,12 @@ public void run() { while (isExecutable) { output.showMenu(); switch (input.selectMenu()) { - case 1 -> output.readAllResults(result.readAllResults()); + case 1 -> output.readAllResults(resultManager.readAllResults()); case 2 -> { String expression = input.getExpression(); int answer = bc.doCalculate(expression); output.printAnswer(answer); - result.save(expression, answer); + resultManager.save(expression, answer); } case 3 -> isExecutable = false; default -> output.inputError(); diff --git a/src/main/java/com/programmers/engine/model/Result.java b/src/main/java/com/programmers/engine/model/ResultManager.java similarity index 92% rename from src/main/java/com/programmers/engine/model/Result.java rename to src/main/java/com/programmers/engine/model/ResultManager.java index 1574cac50..2ffe824ad 100644 --- a/src/main/java/com/programmers/engine/model/Result.java +++ b/src/main/java/com/programmers/engine/model/ResultManager.java @@ -3,7 +3,7 @@ import java.util.HashMap; import java.util.Map; -public class Result { +public class ResultManager { private final Map results = new HashMap<>(); public void save(String expression, int answer) { From 0fac830ce1c36b5318ee6d51938e7f3a6a319a89 Mon Sep 17 00:00:00 2001 From: Sunny Date: Mon, 12 Jun 2023 21:52:47 +0900 Subject: [PATCH 3/9] =?UTF-8?q?REFACTOR=20:=20=EB=A9=94=EB=89=B4=20?= =?UTF-8?q?=EC=84=A0=ED=83=9D=EC=97=90=EC=84=9C=20=EB=A7=A4=EC=A7=81=20?= =?UTF-8?q?=EB=84=98=EB=B2=84=EB=A5=BC=20=EC=83=81=EC=88=98=EB=A1=9C=20?= =?UTF-8?q?=EB=B0=94=EA=BE=B8=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../programmers/engine/JavaCalculator.java | 11 ++++++---- .../com/programmers/engine/model/Menu.java | 22 +++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/programmers/engine/model/Menu.java diff --git a/src/main/java/com/programmers/engine/JavaCalculator.java b/src/main/java/com/programmers/engine/JavaCalculator.java index 9e59c1240..adec65ed4 100644 --- a/src/main/java/com/programmers/engine/JavaCalculator.java +++ b/src/main/java/com/programmers/engine/JavaCalculator.java @@ -3,6 +3,7 @@ import com.programmers.BasicCalculator; import com.programmers.engine.io.Input; import com.programmers.engine.io.Output; +import com.programmers.engine.model.Menu; import com.programmers.engine.model.ResultManager; import lombok.AllArgsConstructor; @@ -18,15 +19,17 @@ public void run() { boolean isExecutable = true; while (isExecutable) { output.showMenu(); - switch (input.selectMenu()) { - case 1 -> output.readAllResults(resultManager.readAllResults()); - case 2 -> { + Menu menu = Menu.matchMenu(input.selectMenu()); + + switch (menu) { + case LOOK_UP -> output.readAllResults(resultManager.readAllResults()); + case CALCULATE -> { String expression = input.getExpression(); int answer = bc.doCalculate(expression); output.printAnswer(answer); resultManager.save(expression, answer); } - case 3 -> isExecutable = false; + case EXIT -> isExecutable = false; default -> output.inputError(); } } diff --git a/src/main/java/com/programmers/engine/model/Menu.java b/src/main/java/com/programmers/engine/model/Menu.java new file mode 100644 index 000000000..c13bb2901 --- /dev/null +++ b/src/main/java/com/programmers/engine/model/Menu.java @@ -0,0 +1,22 @@ +package com.programmers.engine.model; + +import java.util.Arrays; + +public enum Menu { + LOOK_UP("1"), + CALCULATE("2"), + EXIT("3"); + + private final String menu; + + Menu(String menu) { + this.menu = menu; + } + + public static Menu matchMenu(int number) { + return Arrays.stream(values()) + .filter(v -> number == Integer.parseInt(v.menu)) + .findFirst() + .orElseThrow(IllegalStateException::new); + } +} From 559c4f9a39cf67904dd7d10bfa01e31f1f6666cb Mon Sep 17 00:00:00 2001 From: Sunny Date: Mon, 12 Jun 2023 22:01:16 +0900 Subject: [PATCH 4/9] =?UTF-8?q?REFACTOR=20:=20boolean=20isExcutable=20?= =?UTF-8?q?=EC=82=AC=EC=9A=A9=20X?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/programmers/engine/JavaCalculator.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/programmers/engine/JavaCalculator.java b/src/main/java/com/programmers/engine/JavaCalculator.java index adec65ed4..782384036 100644 --- a/src/main/java/com/programmers/engine/JavaCalculator.java +++ b/src/main/java/com/programmers/engine/JavaCalculator.java @@ -16,8 +16,7 @@ public class JavaCalculator implements Runnable{ @Override public void run() { - boolean isExecutable = true; - while (isExecutable) { + while (true) { output.showMenu(); Menu menu = Menu.matchMenu(input.selectMenu()); @@ -29,7 +28,9 @@ public void run() { output.printAnswer(answer); resultManager.save(expression, answer); } - case EXIT -> isExecutable = false; + case EXIT -> { + return; + } default -> output.inputError(); } } From e6aa4d44d570208e6a99ba4d3296537f0c296588 Mon Sep 17 00:00:00 2001 From: Sunny Date: Mon, 12 Jun 2023 22:11:21 +0900 Subject: [PATCH 5/9] =?UTF-8?q?REFACTOR=20:=20JavaCalculator=EC=97=90?= =?UTF-8?q?=EC=84=9C=20calculate=20=EB=A9=94=EC=84=9C=EB=93=9C=EB=A1=9C=20?= =?UTF-8?q?=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/programmers/engine/JavaCalculator.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/programmers/engine/JavaCalculator.java b/src/main/java/com/programmers/engine/JavaCalculator.java index 782384036..589c648f0 100644 --- a/src/main/java/com/programmers/engine/JavaCalculator.java +++ b/src/main/java/com/programmers/engine/JavaCalculator.java @@ -22,12 +22,7 @@ public void run() { switch (menu) { case LOOK_UP -> output.readAllResults(resultManager.readAllResults()); - case CALCULATE -> { - String expression = input.getExpression(); - int answer = bc.doCalculate(expression); - output.printAnswer(answer); - resultManager.save(expression, answer); - } + case CALCULATE -> calculate(); case EXIT -> { return; } @@ -35,4 +30,11 @@ public void run() { } } } + + private void calculate() { + String expression = input.getExpression(); + int answer = bc.doCalculate(expression); + output.printAnswer(answer); + resultManager.save(expression, answer); + } } From a150c736bd1f8245d4a0fdffa3e8708ba1fb0cbc Mon Sep 17 00:00:00 2001 From: Sunny Date: Mon, 12 Jun 2023 23:15:53 +0900 Subject: [PATCH 6/9] =?UTF-8?q?REFACTOR=20:=20BasicCalculator=20=EB=B6=84?= =?UTF-8?q?=EB=A6=AC=ED=95=98=EA=B8=B0,=20=EA=B4=80=EB=A0=A8=20=EB=A6=AC?= =?UTF-8?q?=ED=8C=A9=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/programmers/App.java | 1 + .../java/com/programmers/BasicCalculator.java | 68 ------------------- .../programmers/engine/JavaCalculator.java | 2 +- .../programmers/engine/model/Operator.java | 35 ++++++++++ .../engine/module/BasicCalculator.java | 19 ++++++ .../module/convert/AnswerConverter.java | 23 +++++++ .../module/convert/PostfixConverter.java | 28 ++++++++ src/test/java/BasicCalculatorTest.java | 2 +- 8 files changed, 108 insertions(+), 70 deletions(-) delete mode 100644 src/main/java/com/programmers/BasicCalculator.java create mode 100644 src/main/java/com/programmers/engine/model/Operator.java create mode 100644 src/main/java/com/programmers/engine/module/BasicCalculator.java create mode 100644 src/main/java/com/programmers/engine/module/convert/AnswerConverter.java create mode 100644 src/main/java/com/programmers/engine/module/convert/PostfixConverter.java diff --git a/src/main/java/com/programmers/App.java b/src/main/java/com/programmers/App.java index e686054b4..bb538a331 100644 --- a/src/main/java/com/programmers/App.java +++ b/src/main/java/com/programmers/App.java @@ -2,6 +2,7 @@ import com.programmers.engine.JavaCalculator; import com.programmers.engine.model.ResultManager; +import com.programmers.engine.module.BasicCalculator; public class App { public static void main(String[] args) { diff --git a/src/main/java/com/programmers/BasicCalculator.java b/src/main/java/com/programmers/BasicCalculator.java deleted file mode 100644 index 3a9d536ab..000000000 --- a/src/main/java/com/programmers/BasicCalculator.java +++ /dev/null @@ -1,68 +0,0 @@ -package com.programmers; - -import java.util.ArrayList; -import java.util.Stack; - -public class BasicCalculator { - public int doCalculate(String expression) { - return toPostfix(expression); - } - - private int getPriority(String operator) { - if (operator.equals("(")) return 0; - else if (operator.equals("+") || operator.equals("-")) return 1; - else return 2; - } - - private int toPostfix(String str) { - String[] strArr = str.split(" "); - ArrayList output = new ArrayList<>(); - Stack st = new Stack<>(); - - for (int i = 0; i < strArr.length; i++) { - String now = strArr[i]; - switch (now) { - case "(" -> st.push(now); - case ")" -> { - while (!st.isEmpty()) { - if (st.peek().equals("(")) { - st.pop(); - break; - } - output.add(st.pop()); - } - } - case "+", "-", "*", "/" -> { - while (!st.isEmpty() && getPriority(st.peek()) >= getPriority(now)) { - output.add(st.pop()); - } - st.push(now); - } - default -> output.add(now); - } - } - while (!st.isEmpty()) output.add(st.pop()); - return getResult(output); - } - - public int getResult(ArrayList list) { - Stack st = new Stack<>(); - for (int i = 0; i < list.size(); i++) { - String now = list.get(i); - - if (!now.equals("+") && !now.equals("-") && !now.equals("*") && !now.equals("/")){ - st.push(Integer.parseInt(now)); - } else { - int x = st.pop(); - int y = st.pop(); - switch (now) { - case "+" -> st.push(y + x); - case "-" -> st.push(y - x); - case "*" -> st.push(y * x); - case "/" -> st.push(y / x); - } - } - } - return st.pop(); - } -} diff --git a/src/main/java/com/programmers/engine/JavaCalculator.java b/src/main/java/com/programmers/engine/JavaCalculator.java index 589c648f0..ff4ccfa15 100644 --- a/src/main/java/com/programmers/engine/JavaCalculator.java +++ b/src/main/java/com/programmers/engine/JavaCalculator.java @@ -1,6 +1,6 @@ package com.programmers.engine; -import com.programmers.BasicCalculator; +import com.programmers.engine.module.BasicCalculator; import com.programmers.engine.io.Input; import com.programmers.engine.io.Output; import com.programmers.engine.model.Menu; diff --git a/src/main/java/com/programmers/engine/model/Operator.java b/src/main/java/com/programmers/engine/model/Operator.java new file mode 100644 index 000000000..b4e6f6d63 --- /dev/null +++ b/src/main/java/com/programmers/engine/model/Operator.java @@ -0,0 +1,35 @@ +package com.programmers.engine.model; + +import lombok.Getter; + +import java.util.Arrays; +import java.util.function.BiFunction; + +@Getter +public enum Operator { + ADD("+", 1, (a, b) -> a + b), + SUBTRACTION("-", 1, (a, b) -> a - b), + MULTIPLY("*", 2, (a, b) -> a * b), + DIVIDE("/", 2, (a, b) -> a/ b); + + private final String symbol; + private final int priority; + private final BiFunction action; + + Operator(String symbol, int priority, BiFunction action) { + this.symbol = symbol; + this.priority = priority; + this.action = action; + } + + public static Operator matchOperator(String operator) { + return Arrays.stream(values()) + .filter(v -> operator.equals(v.symbol)) + .findFirst() + .orElseThrow(IllegalArgumentException::new); + } + + public int calculate(int a, int b) { + return action.apply(a, b); + } +} diff --git a/src/main/java/com/programmers/engine/module/BasicCalculator.java b/src/main/java/com/programmers/engine/module/BasicCalculator.java new file mode 100644 index 000000000..31b61fb70 --- /dev/null +++ b/src/main/java/com/programmers/engine/module/BasicCalculator.java @@ -0,0 +1,19 @@ +package com.programmers.engine.module; + +import com.programmers.engine.model.Operator; +import com.programmers.engine.module.convert.AnswerConverter; +import com.programmers.engine.module.convert.PostfixConverter; +import java.util.ArrayList; +import java.util.List; +import java.util.Stack; + +public class BasicCalculator { + private final PostfixConverter postfixConverter = new PostfixConverter(); + private final AnswerConverter answerConverter = new AnswerConverter(); + + public int doCalculate(String expression) { + List postfixList = postfixConverter.convertInfixToPostfix(expression); + return answerConverter.convertPostfixToAnswer(postfixList); + } + +} diff --git a/src/main/java/com/programmers/engine/module/convert/AnswerConverter.java b/src/main/java/com/programmers/engine/module/convert/AnswerConverter.java new file mode 100644 index 000000000..d8543f478 --- /dev/null +++ b/src/main/java/com/programmers/engine/module/convert/AnswerConverter.java @@ -0,0 +1,23 @@ +package com.programmers.engine.module.convert; + +import com.programmers.engine.model.Operator; + +import java.util.List; +import java.util.Stack; + +public class AnswerConverter { + public int convertPostfixToAnswer(List list) { + Stack st = new Stack<>(); + for (String now : list) { + if (!now.equals("+") && !now.equals("-") && !now.equals("*") && !now.equals("/")) { + st.push(Integer.parseInt(now)); + continue; + } + Operator operator = Operator.matchOperator(now); + int x = st.pop(); + int y = st.pop(); + st.push(operator.calculate(x, y)); + } + return st.pop(); + } +} diff --git a/src/main/java/com/programmers/engine/module/convert/PostfixConverter.java b/src/main/java/com/programmers/engine/module/convert/PostfixConverter.java new file mode 100644 index 000000000..a34d9e40b --- /dev/null +++ b/src/main/java/com/programmers/engine/module/convert/PostfixConverter.java @@ -0,0 +1,28 @@ +package com.programmers.engine.module.convert; + +import com.programmers.engine.model.Operator; +import java.util.ArrayList; +import java.util.List; +import java.util.Stack; + +public class PostfixConverter { + + public List convertInfixToPostfix(String expression) { + String[] strArr = expression.split(" "); + List output = new ArrayList<>(); + Stack st = new Stack<>(); + + for (String now: strArr) { + if (now.equals("+") || now.equals("-") || now.equals("*") || now.equals("/")) { + while (!st.isEmpty() && Operator.matchOperator(st.peek()).getPriority() >= Operator.matchOperator(now).getPriority()) { + output.add(st.pop()); + } + st.push(now); + continue; + } + output.add(now); + } + while (!st.isEmpty()) output.add(st.pop()); + return output; + } +} diff --git a/src/test/java/BasicCalculatorTest.java b/src/test/java/BasicCalculatorTest.java index 688e95984..f7be1cad6 100644 --- a/src/test/java/BasicCalculatorTest.java +++ b/src/test/java/BasicCalculatorTest.java @@ -1,4 +1,4 @@ -import com.programmers.BasicCalculator; +import com.programmers.engine.module.BasicCalculator; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; From 166e1319bb95d18e62dda5304aa23e4c5e10aca0 Mon Sep 17 00:00:00 2001 From: Sunny Date: Mon, 12 Jun 2023 23:17:06 +0900 Subject: [PATCH 7/9] =?UTF-8?q?REFACTOR=20:=20=EB=B6=88=ED=95=84=EC=9A=94?= =?UTF-8?q?=ED=95=9C=20import=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/programmers/engine/module/BasicCalculator.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/main/java/com/programmers/engine/module/BasicCalculator.java b/src/main/java/com/programmers/engine/module/BasicCalculator.java index 31b61fb70..cd202a395 100644 --- a/src/main/java/com/programmers/engine/module/BasicCalculator.java +++ b/src/main/java/com/programmers/engine/module/BasicCalculator.java @@ -1,11 +1,8 @@ package com.programmers.engine.module; -import com.programmers.engine.model.Operator; import com.programmers.engine.module.convert.AnswerConverter; import com.programmers.engine.module.convert.PostfixConverter; -import java.util.ArrayList; import java.util.List; -import java.util.Stack; public class BasicCalculator { private final PostfixConverter postfixConverter = new PostfixConverter(); From 04fdc472a5cf66af00e0b7d9702fd2c12d119265 Mon Sep 17 00:00:00 2001 From: Sunny Date: Mon, 12 Jun 2023 23:20:11 +0900 Subject: [PATCH 8/9] =?UTF-8?q?REFACTOR=20:=20save=20=ED=95=A8=EC=88=98=20?= =?UTF-8?q?=EC=82=AD=EC=A0=9C=20=EB=AC=B8=EC=A0=9C=20=EA=B4=80=EB=A0=A8=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/programmers/engine/model/ResultManager.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/programmers/engine/model/ResultManager.java b/src/main/java/com/programmers/engine/model/ResultManager.java index 2ffe824ad..3785802b5 100644 --- a/src/main/java/com/programmers/engine/model/ResultManager.java +++ b/src/main/java/com/programmers/engine/model/ResultManager.java @@ -5,9 +5,10 @@ public class ResultManager { private final Map results = new HashMap<>(); + private int key = 0; public void save(String expression, int answer) { - results.put(results.size() + 1, expression + " = " + String.valueOf(answer)); + results.put(++key, expression + " = " + String.valueOf(answer)); } public Map readAllResults() { From d492c3642b41d14e5e98829ba8ded2eb047d5886 Mon Sep 17 00:00:00 2001 From: Sunny Date: Mon, 12 Jun 2023 23:47:30 +0900 Subject: [PATCH 9/9] =?UTF-8?q?FEAT=20:=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../programmers/engine/model/Operator.java | 2 +- src/test/java/BasicCalculatorTest.java | 15 ------- src/test/java/CalculatorTest.java | 42 +++++++++++++++++++ src/test/java/MenuTest.java | 13 ++++++ src/test/java/OperatorTest.java | 24 +++++++++++ 5 files changed, 80 insertions(+), 16 deletions(-) delete mode 100644 src/test/java/BasicCalculatorTest.java create mode 100644 src/test/java/CalculatorTest.java create mode 100644 src/test/java/MenuTest.java create mode 100644 src/test/java/OperatorTest.java diff --git a/src/main/java/com/programmers/engine/model/Operator.java b/src/main/java/com/programmers/engine/model/Operator.java index b4e6f6d63..6b5dccef1 100644 --- a/src/main/java/com/programmers/engine/model/Operator.java +++ b/src/main/java/com/programmers/engine/model/Operator.java @@ -10,7 +10,7 @@ public enum Operator { ADD("+", 1, (a, b) -> a + b), SUBTRACTION("-", 1, (a, b) -> a - b), MULTIPLY("*", 2, (a, b) -> a * b), - DIVIDE("/", 2, (a, b) -> a/ b); + DIVIDE("/", 2, (a, b) -> b / a); private final String symbol; private final int priority; diff --git a/src/test/java/BasicCalculatorTest.java b/src/test/java/BasicCalculatorTest.java deleted file mode 100644 index f7be1cad6..000000000 --- a/src/test/java/BasicCalculatorTest.java +++ /dev/null @@ -1,15 +0,0 @@ -import com.programmers.engine.module.BasicCalculator; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; -import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; -public class BasicCalculatorTest { - BasicCalculator bc = new BasicCalculator(); - @DisplayName("계산기 테스트") - @Test - void BasicCalculatorTest() { - String str = "3 + 2 + 4 * 5 + 3 / 1"; - String result = bc.doCalculate(str); - assertThat(result).isEqualTo("3 + 2 + 4 * 5 + 3 / 1 = 28"); - } - -} diff --git a/src/test/java/CalculatorTest.java b/src/test/java/CalculatorTest.java new file mode 100644 index 000000000..f95b7e65b --- /dev/null +++ b/src/test/java/CalculatorTest.java @@ -0,0 +1,42 @@ +import com.programmers.engine.module.BasicCalculator; +import com.programmers.engine.module.convert.AnswerConverter; +import com.programmers.engine.module.convert.PostfixConverter; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; +public class CalculatorTest { + BasicCalculator bc = new BasicCalculator(); + AnswerConverter ac = new AnswerConverter(); + PostfixConverter pc = new PostfixConverter(); + + @DisplayName("계산기 테스트") + @Test + void basicCalculatorTest() { + String str = "3 + 2 + 4 * 5 + 3 / 1"; + int result = bc.doCalculate(str); + assertThat(result).isEqualTo(28); + } + + @DisplayName("정답 변환기 테스트") + @Test + void answerConverterTest() { + List list = new ArrayList<>(Arrays.asList("3", "2", "+", "4", "5", "*", "+", "3", "1", "/", "+")); + int answer = ac.convertPostfixToAnswer(list); + assertThat(answer).isEqualTo(28); + } + + @DisplayName("후위표기 변환기 테스트") + @Test + void postfixConverterTest() { + String str = "3 + 2 + 4 * 5 + 3 / 1"; + List list = pc.convertInfixToPostfix(str); + List ans = new ArrayList<>(Arrays.asList("3", "2", "+", "4", "5", "*", "+", "3", "1", "/", "+")); + assertThat(list).isEqualTo(ans); + } + +} diff --git a/src/test/java/MenuTest.java b/src/test/java/MenuTest.java new file mode 100644 index 000000000..24a5f6b37 --- /dev/null +++ b/src/test/java/MenuTest.java @@ -0,0 +1,13 @@ +import com.programmers.engine.model.Menu; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; +public class MenuTest { + @DisplayName("메뉴 선택 테스트") + @Test + void menuTest() { + assertThat(Menu.matchMenu(1)).isEqualTo(Menu.LOOK_UP); + assertThat(Menu.matchMenu(2)).isEqualTo(Menu.CALCULATE); + assertThat(Menu.matchMenu(3)).isEqualTo(Menu.EXIT); + } +} diff --git a/src/test/java/OperatorTest.java b/src/test/java/OperatorTest.java new file mode 100644 index 000000000..b82a6d15f --- /dev/null +++ b/src/test/java/OperatorTest.java @@ -0,0 +1,24 @@ +import com.programmers.engine.model.Operator; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; + +public class OperatorTest { + @DisplayName("우선순위 테스트") + @Test + void getPriorityTest() { + assertThat(Operator.matchOperator("+").getPriority()).isEqualTo(1); + assertThat(Operator.matchOperator("-").getPriority()).isEqualTo(1); + assertThat(Operator.matchOperator("*").getPriority()).isEqualTo(2); + assertThat(Operator.matchOperator("/").getPriority()).isEqualTo(2); + } + + @DisplayName("사칙연산 테스트") + @Test + void calculateTest() { + assertThat(Operator.ADD.calculate(2, 6)).isEqualTo(8); + assertThat(Operator.SUBTRACTION.calculate(2, 6)).isEqualTo(-4); + assertThat(Operator.MULTIPLY.calculate(2, 6)).isEqualTo(12); + assertThat(Operator.DIVIDE.calculate(2, 6)).isEqualTo(3); + } +}