Skip to content
Open
Show file tree
Hide file tree
Changes from 37 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
2f8bb46
feat : 입출력 인터페이스
SW-H Jun 11, 2023
5a6deed
feat : 예외 처리 문구
SW-H Jun 11, 2023
c4c3e88
feat : 계산기 - main문/계산
SW-H Jun 11, 2023
e2dcb42
feat : 입출력 인터페이스 구현
SW-H Jun 11, 2023
bd3aee3
feat : 연산자 enum으로 관리
SW-H Jun 11, 2023
52a1300
feat : 사용자 메뉴 enum
SW-H Jun 11, 2023
ee4d032
docs : java버전 명시
SW-H Jun 11, 2023
842467d
docs : 미사용중인 종속 삭제
SW-H Jun 11, 2023
c9c80e0
refactor : 코드 중복 제거
SW-H Jun 11, 2023
25a513e
refactor : 메인 함수를 포함한 클래스 추가
SW-H Jun 11, 2023
6d94091
refactor : 불필요한 개행 삭제
SW-H Jun 11, 2023
4d764ca
feat : 수식
SW-H Jun 15, 2023
882c684
feat : 삭제
SW-H Jun 15, 2023
1a150a5
feat : input/outputView 주입, 선택 메뉴별 수행 메소드를 HashMap으로 분리
SW-H Jun 15, 2023
a3901a5
feat : 출력 관련 내용 CalculatorInputView -> CalculatorOutputView 이동
SW-H Jun 15, 2023
451742e
refactor
SW-H Jun 15, 2023
775466d
feat : 수식 입력시 Expression 클래스 반환
SW-H Jun 15, 2023
5e41f40
feat : 불필요한 문법요소 제거, 연산자 우선순위 비교 메소드(hasLowerPrecedence) 추가
SW-H Jun 15, 2023
2590e8c
feat : 계산 결과 출력을 위해 인자->Integer
SW-H Jun 15, 2023
6b66bc0
feat : 불필요한 Optional 삭제, 메뉴-Integer 대응하도록 수정
SW-H Jun 14, 2023
2f88ba9
feat : main 함수 별도 분리
SW-H Jun 15, 2023
f154103
feat : 계산 인자 -> Double 형으로 변환(나눗셈)
SW-H Jun 15, 2023
ddc919c
feat : 함수 매핑 HashMap 삭제
SW-H Jun 15, 2023
9b34581
feat : CalculatorInputView 내에서 출력 함수 호출 삭제
SW-H Jun 15, 2023
9b943b9
feat : 계산 결과 Integer -> Double(나눗셈)
SW-H Jun 15, 2023
c6c5a68
feat : 문자열 자르기
SW-H Jun 15, 2023
6d43a13
feat : 계산 인자 Integer -> Double (나눗셈)
SW-H Jun 15, 2023
97b393a
feat : 계산 인자 Integer -> Double (나눗셈)
SW-H Jun 15, 2023
84540a2
feat : main 함수
SW-H Jun 15, 2023
eeb3796
feat : 계산 로직 수정(후위연산식 변환없이 연산자 우선 순위에 따라 계산)
SW-H Jun 15, 2023
c37cdad
feat : 수식 내의 공백 제거, split
SW-H Jun 15, 2023
50cabb1
refactor : list 타입 ArrayList -> List
SW-H Jun 15, 2023
0ac007e
feat : 연산자 우선 순위 계산
SW-H Jun 15, 2023
8b4ec3a
refactor : 미사용 메소드 삭제
SW-H Jun 15, 2023
a3544d8
feat : 없는 메뉴선택시 처리 추가
SW-H Jun 15, 2023
ea158fe
feat : 메시지 출력 메소드
SW-H Jun 15, 2023
6f1519d
feat : 메시지 출력 메소드
SW-H Jun 15, 2023
7fe46d5
feat : 없는 메뉴 선택시 처리 방법 수정
SW-H Jun 15, 2023
aa52739
refactor
SW-H Jun 15, 2023
202ce5d
feat : 계산 로직 버그 수정
SW-H Jun 15, 2023
716fb12
feat : 입력된 수식 null인 경우 처리
SW-H Jun 15, 2023
c2351bd
feat : 입력된 수식 null인 경우 처리
SW-H Jun 15, 2023
1f42c10
feat : 입력된 수식이 빈 문자열인 경우 처리
SW-H Jun 15, 2023
358117c
feat : 계산 기록 저장을 위한 저장소 추가
SW-H Jun 15, 2023
f635ecb
feat : 계산 기록 저장을 위한 저장소 추가, 조회 기능
SW-H Jun 15, 2023
06b76d3
feat : 계산 기록 출력
SW-H Jun 15, 2023
84ff197
feat : 수식 getter
SW-H Jun 15, 2023
49675ef
feat : 계산 기록 저장을 위한 저장소 추가
SW-H Jun 15, 2023
62afa70
feat : 계산 기록 저장을 위한 저장소 인터페이스 추가
SW-H Jun 16, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/main/java/co/programmers/App.java
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();
}
}
16 changes: 0 additions & 16 deletions src/main/java/co/programmers/application/Application.java

This file was deleted.

89 changes: 0 additions & 89 deletions src/main/java/co/programmers/application/Calculator.java

This file was deleted.

59 changes: 59 additions & 0 deletions src/main/java/co/programmers/domain/Calculation.java
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();
Comment on lines +19 to +20

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이렇게만 사용할거라면 굳이 미리 클래스에 선언해야할까요??

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

Choose a reason for hiding this comment

The 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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cnt와 count가 동일한 의미를 가지고 있어서 이름을 바꾸는 편이 좋아보여요

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넵 그 부분 수정하면서
두번째 인자도 삭제할 수식의 길이를 의미함을 알아보기 좋도록 length로 변경했습니다

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) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extractOperands는 어떤 기능을 하는 메서드 인가요?

Copy link
Author

Choose a reason for hiding this comment

The 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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parsedExpression.get(operatorIdx - 1)은 따로 선언해 준 후 넘겨주면 좀 더 가독성 좋게 읽힐 것 같아요

Copy link
Author

Choose a reason for hiding this comment

The 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));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parsedExpression의 상태에 따라서 해당 메소드가 결과가 다르고 심지어 에러도 발생할 수 있겠네요

}
}
62 changes: 62 additions & 0 deletions src/main/java/co/programmers/domain/CalculatorApp.java
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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ArithmeticException이 일어 날 수 있는 곳만 try-catch로 잡는 편이 좋지 않을까요?

}
}
47 changes: 47 additions & 0 deletions src/main/java/co/programmers/domain/Expression.java
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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private boolean validate() {
Matcher matcher = EXPRESSION_FORMAT.matcher(expression);
return matcher.matches();
}
private boolean isNotValidate() {
Matcher matcher = EXPRESSION_FORMAT.matcher(expression);
return !matcher.matches();
}

!를 쓰는 대신 메서드에 부정을 담으면 좀 더 명시적으로 읽힐 수 있어요!

Copy link
Author

@SW-H SW-H Jun 16, 2023

Choose a reason for hiding this comment

The 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;
}
}
Loading