- 
                Notifications
    You must be signed in to change notification settings 
- Fork 155
[4기 - 한승원] 1~2주차 과제 : 계산기 구현 미션 제출합니다(사칙연산) #168
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: seungwon
Are you sure you want to change the base?
Changes from 37 commits
2f8bb46
              5a6deed
              c4c3e88
              e2dcb42
              bd3aee3
              52a1300
              ee4d032
              842467d
              c9c80e0
              25a513e
              6d94091
              4d764ca
              882c684
              1a150a5
              a3901a5
              451742e
              775466d
              5e41f40
              2590e8c
              6b66bc0
              2f88ba9
              f154103
              ddc919c
              9b34581
              9b943b9
              c6c5a68
              6d43a13
              97b393a
              84540a2
              eeb3796
              c37cdad
              50cabb1
              0ac007e
              8b4ec3a
              a3544d8
              ea158fe
              6f1519d
              7fe46d5
              aa52739
              202ce5d
              716fb12
              c2351bd
              1f42c10
              358117c
              f635ecb
              06b76d3
              84ff197
              49675ef
              62afa70
              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 | 
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package co.programmers; | ||
|  | ||
| import co.programmers.domain.CalculatorApp; | ||
| import co.programmers.repository.CalculatorRepository; | ||
| import co.programmers.repository.Repository; | ||
| import co.programmers.view.CalculatorInputView; | ||
| import co.programmers.view.CalculatorOutputView; | ||
| import co.programmers.view.InputView; | ||
| import co.programmers.view.OutputView; | ||
|  | ||
| public class App { | ||
|  | ||
| public static void main(String[] args) { | ||
| InputView inputView = new CalculatorInputView(); | ||
| OutputView outputView = new CalculatorOutputView(); | ||
| Repository repository = new CalculatorRepository(); | ||
| CalculatorApp calculatorApp = new CalculatorApp(inputView, outputView, repository); | ||
| calculatorApp.run(); | ||
| } | ||
| } | 
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| package co.programmers.domain; | ||
|  | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|  | ||
| public class Calculation { | ||
|  | ||
| private List<String> parsedExpression; | ||
| private List<String> operators; | ||
| private Expression expression; | ||
|  | ||
| public Calculation(Expression expression) { | ||
| this.expression = expression; | ||
| parsedExpression = new ArrayList<>(); | ||
| operators = new ArrayList<>(); | ||
| } | ||
|  | ||
| public Double calculate() throws ArithmeticException { | ||
| parsedExpression = expression.split(); | ||
| operators = extractOperators(); | ||
| Operator.decideCalculationOrder(operators); | ||
| for (String operator : operators) { | ||
| int operatorPosition = parsedExpression.indexOf(operator); | ||
| Double[] operands = extractOperands(operator); | ||
| Double calculationRes = Operator.calculate(operator, operands[0], operands[1]); | ||
| storeIntermediateResult(operatorPosition, calculationRes); | ||
| removeCompletedExpression(operatorPosition, operands.length); | ||
| } | ||
| 
      Comment on lines
    
      +22
     to 
      +28
    
   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. 더 단순하게 정리해볼까요? | ||
| return calcFinalResult(); | ||
| } | ||
|  | ||
| private void removeCompletedExpression(int operatorPosition, int count) { | ||
| for (int cnt = 0; cnt <= count; cnt++) { | ||
| parsedExpression.remove(operatorPosition); | ||
| } | ||
| 
      Comment on lines
    
      +32
     to 
      +35
    
   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. cnt와 count가 동일한 의미를 가지고 있어서 이름을 바꾸는 편이 좋아보여요 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. 넵 그 부분 수정하면서 private void removeCompletedExpression(int operatorPosition, int length) {
  for (int i = 0; i <= length; i++) {
	  parsedExpression.remove(operatorPosition);
  }
} | ||
| } | ||
|  | ||
| private void storeIntermediateResult(int operatorPosition, Double calculationRes) { | ||
| parsedExpression.add(operatorPosition - 1, String.valueOf(calculationRes)); | ||
| } | ||
|  | ||
| private Double[] extractOperands(String operator) { | ||
| 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. extractOperands는 어떤 기능을 하는 메서드 인가요? 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에 해당하는 피연산자를 뽑아내는 메서드입니다! | ||
| int operatorIdx = parsedExpression.indexOf(operator); | ||
| Double operand1 = Double.parseDouble(parsedExpression.get(operatorIdx - 1)); | ||
| Double operand2 = Double.parseDouble(parsedExpression.get(operatorIdx + 1)); | ||
| 
      Comment on lines
    
      +44
     to 
      +45
    
   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. parsedExpression.get(operatorIdx - 1)은 따로 선언해 준 후 넘겨주면 좀 더 가독성 좋게 읽힐 것 같아요 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. int operatorIdx = parsedExpression.indexOf(operator);
String operand1 = parsedExpression.get(operatorIdx - 1);
String operand2 = parsedExpression.get(operatorIdx + 1);
return new Double[] {Double.parseDouble(operand1), Double.parseDouble(operand2)};이렇게 수정하는 방향은 어떨까요? | ||
| return new Double[] {operand1, operand2}; | ||
| } | ||
|  | ||
| private List<String> extractOperators() { | ||
| expression.eliminateWhiteSpace(); | ||
| List<String> parsed = expression.split("\\d+"); | ||
| parsed.removeIf(String::isEmpty); | ||
| return parsed; | ||
| } | ||
|  | ||
| private Double calcFinalResult() { | ||
| return Double.parseDouble(parsedExpression.get(0)); | ||
| 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. parsedExpression의 상태에 따라서 해당 메소드가 결과가 다르고 심지어 에러도 발생할 수 있겠네요 | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| package co.programmers.domain; | ||
|  | ||
| import co.programmers.exception.ExceptionMessage; | ||
| import co.programmers.repository.Repository; | ||
| import co.programmers.view.CalculatorOutputView; | ||
| import co.programmers.view.InputView; | ||
| import co.programmers.view.OutputView; | ||
|  | ||
| public class CalculatorApp { | ||
|  | ||
| private final InputView inputView; | ||
| private final OutputView outputView; | ||
| private final Repository repository; | ||
|  | ||
| public CalculatorApp(InputView inputView, OutputView outputView, Repository repository) { | ||
| this.inputView = inputView; | ||
| this.outputView = outputView; | ||
| this.repository = repository; | ||
| } | ||
|  | ||
| public void run() { | ||
| UserMenu userMenu; | ||
| do { | ||
| CalculatorOutputView.printMenuChoiceGuide(); | ||
| userMenu = UserMenu.get(inputView.inputUserMenu()); | ||
| executeSelectedMenu(userMenu); | ||
| } while (userMenu != UserMenu.TERMINATE); | ||
| } | ||
|  | ||
| private void executeSelectedMenu(UserMenu userMenu) { | ||
| switch (userMenu) { | ||
| case INQUIRY: | ||
| inquiry(); | ||
| break; | ||
| case CALCULATE: | ||
| calculate(); | ||
| break; | ||
| case TERMINATE: | ||
| break; | ||
| default: | ||
| outputView.printMessage(ExceptionMessage.INVALID_INPUT); | ||
| break; | ||
| } | ||
| } | ||
|  | ||
| public void inquiry() { | ||
| outputView.printCalculationHistory(repository.read()); | ||
| } | ||
|  | ||
| public void calculate() { | ||
| try { | ||
| CalculatorOutputView.printCalculationGuide(); | ||
| Expression expression = inputView.inputExpression(); | ||
| Calculation calculator = new Calculation(expression); | ||
| Double output = calculator.calculate(); | ||
| outputView.printCalculationResult(output); | ||
| repository.save(expression.getExpression(), output); | ||
| } catch (ArithmeticException arithmeticException) { | ||
| System.out.println(arithmeticException.getMessage()); | ||
| } | ||
| 
      Comment on lines
    
      +51
     to 
      +60
    
   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. ArithmeticException이 일어 날 수 있는 곳만 try-catch로 잡는 편이 좋지 않을까요? | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,47 @@ | ||||||||||||||||||||
| package co.programmers.domain; | ||||||||||||||||||||
|  | ||||||||||||||||||||
| import java.util.ArrayList; | ||||||||||||||||||||
| import java.util.List; | ||||||||||||||||||||
| import java.util.regex.Matcher; | ||||||||||||||||||||
| import java.util.regex.Pattern; | ||||||||||||||||||||
|  | ||||||||||||||||||||
| import co.programmers.exception.ExceptionMessage; | ||||||||||||||||||||
|  | ||||||||||||||||||||
| public class Expression { | ||||||||||||||||||||
|  | ||||||||||||||||||||
| private static final Pattern EXPRESSION_FORMAT = Pattern.compile("(\\d+\\s[\\+\\-\\*\\/]\\s)+\\d+"); | ||||||||||||||||||||
| private static final String DELIMITER = " "; | ||||||||||||||||||||
| private String expression; | ||||||||||||||||||||
|  | ||||||||||||||||||||
| public Expression(String expression) { | ||||||||||||||||||||
| this.expression = expression; | ||||||||||||||||||||
| if (expression == null || expression.isEmpty()) { | ||||||||||||||||||||
| throw new IllegalArgumentException(ExceptionMessage.EMPTY_INPUT); | ||||||||||||||||||||
| } | ||||||||||||||||||||
| if (!validate()) { | ||||||||||||||||||||
| throw new ArithmeticException(ExceptionMessage.INVALID_EXPRESSION); | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
|  | ||||||||||||||||||||
| private boolean validate() { | ||||||||||||||||||||
|  | ||||||||||||||||||||
| Matcher matcher = EXPRESSION_FORMAT.matcher(expression); | ||||||||||||||||||||
| return matcher.matches(); | ||||||||||||||||||||
| } | ||||||||||||||||||||
| 
      Comment on lines
    
      +26
     to 
      +30
    
   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. 
        Suggested change
       
 !를 쓰는 대신 메서드에 부정을 담으면 좀 더 명시적으로 읽힐 수 있어요! 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. 깃헙에 업로드 하면서 오류가 있었나봐요... 아래처럼 작성하였습니다! 혹시  private boolean validate() {
  Matcher matcher = EXPRESSION_FORMAT.matcher(expression);
  return matcher.matches();
} | ||||||||||||||||||||
|  | ||||||||||||||||||||
| public List<String> split() { | ||||||||||||||||||||
| return new ArrayList<>(List.of(expression.split(DELIMITER))); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|  | ||||||||||||||||||||
| public List<String> split(String delimiter) { | ||||||||||||||||||||
| return new ArrayList<>(List.of(expression.split(delimiter))); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|  | ||||||||||||||||||||
| public void eliminateWhiteSpace() { | ||||||||||||||||||||
| expression = expression.replaceAll("\\s", ""); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|  | ||||||||||||||||||||
| public String getExpression() { | ||||||||||||||||||||
| return expression; | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
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.
이렇게만 사용할거라면 굳이 미리 클래스에 선언해야할까요??