Skip to content
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

[4기 - 김재웅] 1~2주차 과제 : 계산기 구현 미션 제출합니다. #147

Open
wants to merge 26 commits into
base: kju2405
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
1c5c20b
feat: 사칙연산 기능 구현
kju2405 Jun 9, 2023
31d64fe
feat: 저장소 구현
kju2405 Jun 9, 2023
d42a6d9
feat: 조회 기능 추가
kju2405 Jun 9, 2023
b36e927
style: Interface 메서드 제어자 삭제
kju2405 Jun 13, 2023
c0c1a07
refactor: 메뉴 출력 기능 분리
kju2405 Jun 14, 2023
14789d6
refactor: 입력하는 기능 분리
kju2405 Jun 14, 2023
ef5eeca
style: Record -> Records
kju2405 Jun 14, 2023
1bd816a
refactor: 출력할 결과들을 가져오는 기능과 보여주는 기능 분리
kju2405 Jun 14, 2023
2b75683
refactor: Output 패키지의 Show인터페이스 파일로 합침
kju2405 Jun 14, 2023
8e7fc67
refactor: 기능별 패키지 생성
kju2405 Jun 14, 2023
2324418
refactor: type보다 input이 더 명확한것 같아 변경
kju2405 Jun 14, 2023
71917d0
refactor: 결과 출력 기능 분리
kju2405 Jun 14, 2023
15ffeda
refactor: 입력받은 식 전처리 작업 기능 분리
kju2405 Jun 14, 2023
50b581d
refactor: 사칙연산의 기능만 수행하도록 분리
kju2405 Jun 14, 2023
d31597e
refactor: 식을 입력받고 결과값을 반환하는 기능만 수행, 나머지 기능은 내부적으로 감춤
kju2405 Jun 14, 2023
057119f
refactor: DI컨테이너 생성
kju2405 Jun 14, 2023
8e49f93
style: 명확하게 이름 변경
kju2405 Jun 16, 2023
1429b49
refactor: 공통부붐 메서드 추출
kju2405 Jun 16, 2023
0f7a612
refactor: enum추가하여 각 메뉴 선택을 명확하게 함
kju2405 Jun 16, 2023
c22e1c6
refactor: 개행 문자 객체에서 수행
kju2405 Jun 16, 2023
4f44f1b
refactor: stream으로 변환
kju2405 Jun 16, 2023
f8ef218
test: 사칙연산 테스트 코드 작성
kju2405 Jun 16, 2023
63e778c
test: 테스트 코드 삭제
kju2405 Jun 16, 2023
8f97f36
refactor: 계산 순서는 내부적으로 숨김
kju2405 Jun 16, 2023
eee0518
style: CalculateImpl -> Calculator
kju2405 Jun 16, 2023
cb39427
test: 사칙연산 우선순위 테스트 코드 작성
kju2405 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
27 changes: 27 additions & 0 deletions calculator/src/main/java/org/example/CalConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.example;

import org.example.Calculate.*;
import org.example.Input.Input;
import org.example.Input.UserInput;
import org.example.Output.Show;
import org.example.Output.ShowText;
import org.example.Repository.ExpressionRepository;
import org.example.Repository.Repository;

public class CalConfig {
public Calculate calculate(){
return new CalculateImpl(new CalOrderImpl());
}
public Repository repository(){
return new ExpressionRepository();
}
public Input input(){
return new UserInput();
}
public Show show(){
return new ShowText();
}
public PreProcess preProcess() {
return new PreProcessImpl();
}
Copy link

Choose a reason for hiding this comment

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

Config를 통해 객체를 생성하면 어떤 장점이 있나요?

}
6 changes: 0 additions & 6 deletions calculator/src/main/java/org/example/CalOrder.java

This file was deleted.

9 changes: 9 additions & 0 deletions calculator/src/main/java/org/example/Calculate/CalOrder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.example.Calculate;

import java.util.Stack;

public interface CalOrder {
void calMultiplyDivide();
int calPlusMinus();
Copy link

Choose a reason for hiding this comment

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

Suggested change
void calMultiplyDivide();
int calPlusMinus();
void calculateMultiplyDivide();
int calculatePlusMinus();

메소드 이름이 길어지더라도 명시적으로 작성해주세요!

void setStack(Stack<String> expressionStack);
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,14 @@
package org.example;
package org.example.Calculate;

import java.util.Stack;

public class Calculate implements CalOrder{
private String expression;
public class CalOrderImpl implements CalOrder {
private int result = 0;
Stack<String> expressionStack = new Stack<>();
ExpressionRepository repository = new ExpressionRepository();
int result = 0;

public Calculate(String expression) {
this.expression = expression;
String[] expressionArr = expression.split(" ");
for (String inputVal : expressionArr) {
expressionStack.add(inputVal);
}
}

public void calculate(){
calPriorityFirst();
calPrioritySecond();
}

@Override
public void calPriorityFirst() {
for (int i = 1; i < expressionStack.size(); i+=2) {
public void calMultiplyDivide() {
for (int i = 1; i < expressionStack.size(); i += 2) {
if (expressionStack.get(i).equals("*")) {
multiply(i);
i -= 2;
Expand All @@ -35,7 +20,7 @@ public void calPriorityFirst() {
}

@Override
public void calPrioritySecond() {
public int calPlusMinus() {
Copy link

Choose a reason for hiding this comment

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

이 메서드를 어떻게 테스트 할 수 있을까? 라는 질문을 하면서 리팩토링을 해보시면 좋을 것 같아요.

result = Integer.parseInt(expressionStack.get(0));
for (int i = 1; i < expressionStack.size(); i += 2) {
if (expressionStack.get(i).equals("+")) {
Expand All @@ -44,11 +29,15 @@ public void calPrioritySecond() {
result -= Integer.parseInt(expressionStack.get(i + 1));
}
}
System.out.println(result);
repository.save(expression,result);
return result;
}

@Override
public void setStack(Stack<String> expressionStack) {
this.expressionStack = expressionStack;
}

public void multiply(int idx){
public void multiply(int idx) {
result = Integer.parseInt(expressionStack.get(idx - 1)) * Integer.parseInt(expressionStack.get(idx + 1));
expressionStack.add(idx - 1, String.valueOf(result));
expressionStack.remove(idx);
Expand Down
7 changes: 7 additions & 0 deletions calculator/src/main/java/org/example/Calculate/Calculate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.example.Calculate;

import java.util.Stack;

public interface Calculate {
int calculate(Stack<String> expressionStack);
}
17 changes: 17 additions & 0 deletions calculator/src/main/java/org/example/Calculate/CalculateImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.example.Calculate;

import java.util.Stack;

public class CalculateImpl implements Calculate {
CalOrder calOrder;

public CalculateImpl(CalOrder calOrder) {
this.calOrder = calOrder;
}

public int calculate(Stack<String> expressionStack) {
calOrder.setStack(expressionStack);
calOrder.calMultiplyDivide();
return calOrder.calPlusMinus();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.example.Calculate;

import java.util.Stack;

public interface PreProcess {
Stack<String> expressionToStack(String expression);
}
18 changes: 18 additions & 0 deletions calculator/src/main/java/org/example/Calculate/PreProcessImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.example.Calculate;

import java.util.Stack;

public class PreProcessImpl implements PreProcess{
private String expression;
Stack<String> expressionStack = new Stack<>();
@Override
Copy link

Choose a reason for hiding this comment

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

Suggested change
Stack<String> expressionStack = new Stack<>();
@Override
Stack<String> expressionStack = new Stack<>();
@Override

public Stack<String> expressionToStack(String expression) {
this.expression = expression;
String[] expressionArr = expression.split(" ");
expressionStack.clear();
for (String inputVal : expressionArr) {
expressionStack.add(inputVal);
}
return expressionStack;
}
Copy link

Choose a reason for hiding this comment

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

너무 요구사항과 강하게 결합 되어 있는 것 같아서, 재사용성을 고려해보는 것도 좋을 거 같아요 :)

}
6 changes: 6 additions & 0 deletions calculator/src/main/java/org/example/Input/Input.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.example.Input;

public interface Input {
int inputChoice();
String inputExpression();
}
17 changes: 17 additions & 0 deletions calculator/src/main/java/org/example/Input/UserInput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.example.Input;

import java.util.Scanner;

public class UserInput implements Input {
Scanner scanner = new Scanner(System.in);
@Override
public int inputChoice() {
return scanner.nextInt();
Copy link

Choose a reason for hiding this comment

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

inputChoice()라는 메서드명과 실제로 하고 있는 일이 조금 달라보여요.

}

@Override
public String inputExpression() {
scanner.nextLine();
return scanner.nextLine();
}
}
43 changes: 23 additions & 20 deletions calculator/src/main/java/org/example/Main.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,39 @@
package org.example;

import org.example.Calculate.Calculate;
import org.example.Calculate.PreProcess;
import org.example.Input.Input;
import org.example.Output.Show;
import org.example.Repository.Repository;

import java.util.List;
import java.util.Scanner;
import java.util.Stack;

public class Main {
private static int choice;
public static void main(String[] args) {

int choice = 0;
CalConfig calConfig = new CalConfig();
Calculate calculator = calConfig.calculate();
Repository repository = calConfig.repository();
Input input = calConfig.input();
Show show = calConfig.show();
PreProcess preProcess = calConfig.preProcess();

while (true) {
choice = getChoice();
show.showMenu();
choice = input.inputChoice();
System.out.println();
Copy link

Choose a reason for hiding this comment

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

이것도 출력을 담당하는 객체에서 수행하면 좋을 것 같아요!

Copy link
Author

Choose a reason for hiding this comment

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

멘토님, 리뷰 감사드립니다!
혹시 show.showMenu()부분을 말씀하시는걸까요???

Copy link

Choose a reason for hiding this comment

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

System.out.println();으로 하드코딩 되어 있는 부분입니다 :)

Copy link
Author

@kju2405 kju2405 Jun 15, 2023

Choose a reason for hiding this comment

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

아하 감사합니다🙇‍♂️

if (choice == 1) {
Copy link

Choose a reason for hiding this comment

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

choice 1이 어떤 동작인지 어떻게 알 수 있을까요?

new UserInterfaceImpl().showRecord();
List<String> records = repository.getRecords();
show.showRecords(records);
} else {
String expression = typeExpression();
new Calculate(expression).calculate();
String expression = input.inputExpression();
Stack<String> expressionStack = preProcess.expressionToStack(expression);
int result = calculator.calculate(expressionStack);
repository.save(expression,result);
show.showResult(result);
}
System.out.println();
}
}

public static int getChoice() {
Scanner scanner = new Scanner(System.in);

System.out.println("1. 조회\n2. 계산\n");
System.out.print("선택 : ");
return scanner.nextInt();
}

public static String typeExpression() {
Scanner scanner = new Scanner(System.in);
return scanner.nextLine();
}
}
9 changes: 9 additions & 0 deletions calculator/src/main/java/org/example/Output/Show.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.example.Output;

import java.util.List;

public interface Show {
void showMenu();
void showRecords(List<String> records);
void showResult(int result);
}
21 changes: 21 additions & 0 deletions calculator/src/main/java/org/example/Output/ShowText.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.example.Output;

import java.util.List;

public class ShowText implements Show{
Copy link

Choose a reason for hiding this comment

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

클래스 이름은 명사로 만들어주세요

@Override
public void showMenu() {
System.out.println("1. 조회\n2. 계산\n");
System.out.print("선택 : ");
}

@Override
public void showRecords(List<String> records) {
records.forEach(System.out::println);
}

@Override
public void showResult(int result) {
System.out.println(result);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.example;
package org.example.Repository;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -12,7 +12,7 @@ public void save(String expression, int result) {
}

@Override
public List<String> getRecord() {
public List<String> getRecords() {
return store;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.example;
package org.example.Repository;

import java.util.List;

public interface Repository {
void save(String expression, int result);
List<String> getRecord();
List<String> getRecords();
}
5 changes: 0 additions & 5 deletions calculator/src/main/java/org/example/UserInterface.java

This file was deleted.

13 changes: 0 additions & 13 deletions calculator/src/main/java/org/example/UserInterfaceImpl.java

This file was deleted.