- 
                Notifications
    You must be signed in to change notification settings 
- Fork 155
[4기 - 전선희] 1~2주차 과제 : 계산기 구현 미션 제출합니다. (2차) #172
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: funnysunny08
Are you sure you want to change the base?
Changes from all commits
d793990
              80ccd94
              0fac830
              559c4f9
              e6aa4d4
              a150c73
              166e131
              04fdc47
              d492c36
              File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,14 +1,15 @@ | ||
| package com.programmers; | ||
|  | ||
| import com.programmers.engine.JavaCalculator; | ||
| import com.programmers.engine.model.Result; | ||
| import com.programmers.engine.model.ResultManager; | ||
| import com.programmers.engine.module.BasicCalculator; | ||
|  | ||
| 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(); | ||
| } | ||
| } | 
This file was deleted.
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,29 +1,40 @@ | ||
| 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.Result; | ||
| import com.programmers.engine.model.Menu; | ||
| 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 | ||
| public void run() { | ||
| boolean isExecutable = true; | ||
| while (isExecutable) { | ||
| while (true) { | ||
| output.showMenu(); | ||
| switch (input.selectMenu()) { | ||
| case 1 -> result.readAllResults(); | ||
| case 2 -> result.save(bc.doCalculate(input.getExpression())); | ||
| case 3 -> isExecutable = false; | ||
| Menu menu = Menu.matchMenu(input.selectMenu()); | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 한줄로 쭉 쓰시면  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 
 | ||
|  | ||
| switch (menu) { | ||
| case LOOK_UP -> output.readAllResults(resultManager.readAllResults()); | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 메서드가 write로 시작하는 것이 맞지 않을까요? | ||
| case CALCULATE -> calculate(); | ||
| case EXIT -> { | ||
| return; | ||
| } | ||
| default -> output.inputError(); | ||
| } | ||
| } | ||
| } | ||
|  | ||
| private void calculate() { | ||
| String expression = input.getExpression(); | ||
| int answer = bc.doCalculate(expression); | ||
| output.printAnswer(answer); | ||
| resultManager.save(expression, answer); | ||
| 
      Comment on lines
    
      +23
     to 
      +38
    
   There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 실행되는  | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,6 +1,10 @@ | ||
| package com.programmers.engine.io; | ||
|  | ||
| import java.util.Map; | ||
|  | ||
| public interface Output { | ||
| void showMenu(); | ||
| void inputError(); | ||
| void readAllResults(Map<Integer, String> map); | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Map은 다양한 형태의 값이 들어올 수 있을 것 같아요! 이런 부분에 대한 검증을 할 수 있는 클래스로 변경하거나 검증 로직이 있어야할 것 같아요! | ||
| void printAnswer(int answer); | ||
| } | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -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; | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 
 | ||
|  | ||
| 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); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -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) -> b / a); | ||
|  | ||
| private final String symbol; | ||
| private final int priority; | ||
| private final BiFunction<Integer, Integer, Integer> action; | ||
|  | ||
| Operator(String symbol, int priority, BiFunction<Integer, Integer, Integer> 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); | ||
| } | ||
| } | 
This file was deleted.
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package com.programmers.engine.model; | ||
|  | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|  | ||
| public class ResultManager { | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ResultManager가 model 패키지에 있는게 어색해보이네요. 영속화를 담당하고 있는 것 같아서요 | ||
| private final Map<Integer, String> results = new HashMap<>(); | ||
| private int key = 0; | ||
|  | ||
| public void save(String expression, int answer) { | ||
| results.put(++key, expression + " = " + String.valueOf(answer)); | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nit] | ||
| } | ||
|  | ||
| public Map<Integer, String> readAllResults() { | ||
| return results; | ||
| } | ||
| 
      Comment on lines
    
      +14
     to 
      +16
    
   There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 복사본을 반환해주는 것이 좋아보이네요. 외부에서 Map 객체를 조작하면 내부가 변경될 수 있을 것 같아요. | ||
| } | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package com.programmers.engine.module; | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 패키지 이름을  | ||
|  | ||
| import com.programmers.engine.module.convert.AnswerConverter; | ||
| import com.programmers.engine.module.convert.PostfixConverter; | ||
| import java.util.List; | ||
|  | ||
| public class BasicCalculator { | ||
| private final PostfixConverter postfixConverter = new PostfixConverter(); | ||
| private final AnswerConverter answerConverter = new AnswerConverter(); | ||
|  | ||
| public int doCalculate(String expression) { | ||
| List<String> postfixList = postfixConverter.convertInfixToPostfix(expression); | ||
| return answerConverter.convertPostfixToAnswer(postfixList); | ||
| 
      Comment on lines
    
      +9
     to 
      +13
    
   There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 
 | ||
| } | ||
|  | ||
| } | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -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<String> list) { | ||
| Stack<Integer> st = new Stack<>(); | ||
| for (String now : list) { | ||
| if (!now.equals("+") && !now.equals("-") && !now.equals("*") && !now.equals("/")) { | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 문자열이 연산자인지 판단하는 로직은 Operator에 있어야 할 거 같네요. | ||
| st.push(Integer.parseInt(now)); | ||
| continue; | ||
| } | ||
| Operator operator = Operator.matchOperator(now); | ||
| int x = st.pop(); | ||
| int y = st.pop(); | ||
| 
      Comment on lines
    
      +17
     to 
      +18
    
   There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 의미있는 변수명을 사용하면 좋겠네요. 순서가 중요하다면  | ||
| st.push(operator.calculate(x, y)); | ||
| } | ||
| return st.pop(); | ||
| } | ||
| } | ||
| 
      Comment on lines
    
      +8
     to 
      +23
    
   There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 
 | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -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<String> convertInfixToPostfix(String expression) { | ||
| String[] strArr = expression.split(" "); | ||
| List<String> output = new ArrayList<>(); | ||
| Stack<String> 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()) { | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 우선순위를 판단하는 로직도 Operator가 가져야 할 책임인거 같네요 | ||
| output.add(st.pop()); | ||
| } | ||
| st.push(now); | ||
| continue; | ||
| } | ||
| output.add(now); | ||
| } | ||
| while (!st.isEmpty()) output.add(st.pop()); | ||
| return output; | ||
| } | ||
| } | ||
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이 부분은 반영전이 더 나은거 같은데 true를 사용하신 이유가 있을까요?