-
Notifications
You must be signed in to change notification settings - Fork 156
[4기 - 김재웅] 1~2주차 과제 : 계산기 구현 미션 제출합니다. #147
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
Open
kju2405
wants to merge
26
commits into
prgrms-be-devcourse:kju2405
Choose a base branch
from
kju2405:main
base: kju2405
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
1c5c20b
feat: 사칙연산 기능 구현
kju2405 31d64fe
feat: 저장소 구현
kju2405 d42a6d9
feat: 조회 기능 추가
kju2405 b36e927
style: Interface 메서드 제어자 삭제
kju2405 c0c1a07
refactor: 메뉴 출력 기능 분리
kju2405 14789d6
refactor: 입력하는 기능 분리
kju2405 ef5eeca
style: Record -> Records
kju2405 1bd816a
refactor: 출력할 결과들을 가져오는 기능과 보여주는 기능 분리
kju2405 2b75683
refactor: Output 패키지의 Show인터페이스 파일로 합침
kju2405 8e7fc67
refactor: 기능별 패키지 생성
kju2405 2324418
refactor: type보다 input이 더 명확한것 같아 변경
kju2405 71917d0
refactor: 결과 출력 기능 분리
kju2405 15ffeda
refactor: 입력받은 식 전처리 작업 기능 분리
kju2405 50b581d
refactor: 사칙연산의 기능만 수행하도록 분리
kju2405 d31597e
refactor: 식을 입력받고 결과값을 반환하는 기능만 수행, 나머지 기능은 내부적으로 감춤
kju2405 057119f
refactor: DI컨테이너 생성
kju2405 8e49f93
style: 명확하게 이름 변경
kju2405 1429b49
refactor: 공통부붐 메서드 추출
kju2405 0f7a612
refactor: enum추가하여 각 메뉴 선택을 명확하게 함
kju2405 c22e1c6
refactor: 개행 문자 객체에서 수행
kju2405 4f44f1b
refactor: stream으로 변환
kju2405 f8ef218
test: 사칙연산 테스트 코드 작성
kju2405 63e778c
test: 테스트 코드 삭제
kju2405 8f97f36
refactor: 계산 순서는 내부적으로 숨김
kju2405 eee0518
style: CalculateImpl -> Calculator
kju2405 cb39427
test: 사칙연산 우선순위 테스트 코드 작성
kju2405 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.ShowingText; | ||
import org.example.Repository.ExpressionRepository; | ||
import org.example.Repository.Repository; | ||
|
||
public class CalConfig { | ||
public Calculate calculate(){ | ||
return new Calculator(new CalOrderImpl()); | ||
} | ||
public Repository repository(){ | ||
return new ExpressionRepository(); | ||
} | ||
public Input input(){ | ||
return new UserInput(); | ||
} | ||
public Show show(){ | ||
return new ShowingText(); | ||
} | ||
public PreProcess preProcess() { | ||
return new PreProcessImpl(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 setStack(Stack<String> expressionStack); | ||
|
||
int calculate(); | ||
} |
59 changes: 59 additions & 0 deletions
59
calculator/src/main/java/org/example/Calculate/CalOrderImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package org.example.Calculate; | ||
|
||
import java.util.Stack; | ||
|
||
public class CalOrderImpl implements CalOrder { | ||
private int result = 0; | ||
Stack<String> expressionStack = new Stack<>(); | ||
|
||
public int calculate() { | ||
calculateMultiplyDivide(); | ||
return calculatePlusMinus(); | ||
} | ||
|
||
private void calculateMultiplyDivide() { | ||
for (int i = 1; i < expressionStack.size(); i += 2) { | ||
if (expressionStack.get(i).equals("*")) { | ||
multiply(i); | ||
i -= 2; | ||
} else if (expressionStack.get(i).equals("/")) { | ||
divide(i); | ||
i -= 2; | ||
} | ||
} | ||
} | ||
|
||
private int calculatePlusMinus() { | ||
result = Integer.parseInt(expressionStack.get(0)); | ||
for (int i = 1; i < expressionStack.size(); i += 2) { | ||
if (expressionStack.get(i).equals("+")) { | ||
result += Integer.parseInt(expressionStack.get(i + 1)); | ||
} else { | ||
result -= Integer.parseInt(expressionStack.get(i + 1)); | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
@Override | ||
public void setStack(Stack<String> expressionStack) { | ||
this.expressionStack = expressionStack; | ||
} | ||
|
||
private void multiply(int idx) { | ||
result = Integer.parseInt(expressionStack.get(idx - 1)) * Integer.parseInt(expressionStack.get(idx + 1)); | ||
replaceNumOperatorNumWithResult(idx); | ||
} | ||
|
||
private void divide(int idx) { | ||
result = Integer.parseInt(expressionStack.get(idx - 1)) / Integer.parseInt(expressionStack.get(idx + 1)); | ||
replaceNumOperatorNumWithResult(idx); | ||
} | ||
|
||
private void replaceNumOperatorNumWithResult(int idx) { | ||
expressionStack.add(idx - 1, String.valueOf(result)); | ||
expressionStack.remove(idx); | ||
expressionStack.remove(idx); | ||
expressionStack.remove(idx); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
calculator/src/main/java/org/example/Calculate/Calculate.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
16 changes: 16 additions & 0 deletions
16
calculator/src/main/java/org/example/Calculate/Calculator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package org.example.Calculate; | ||
|
||
import java.util.Stack; | ||
|
||
public class Calculator implements Calculate { | ||
CalOrder calOrder; | ||
|
||
public Calculator(CalOrder calOrder) { | ||
this.calOrder = calOrder; | ||
} | ||
|
||
public int calculate(Stack<String> expressionStack) { | ||
calOrder.setStack(expressionStack); | ||
return calOrder.calculate(); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
calculator/src/main/java/org/example/Calculate/PreProcess.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
20 changes: 20 additions & 0 deletions
20
calculator/src/main/java/org/example/Calculate/PreProcessImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package org.example.Calculate; | ||
|
||
import java.util.Arrays; | ||
import java.util.Stack; | ||
|
||
public class PreProcessImpl implements PreProcess { | ||
Stack<String> expressionStack = new Stack<>(); | ||
|
||
@Override | ||
public Stack<String> expressionToStack(String expression) { | ||
makeEmptyStack(); | ||
Arrays.stream(expression.split(" ")) | ||
.forEach(expressionStack::add); | ||
return expressionStack; | ||
} | ||
|
||
private void makeEmptyStack() { | ||
expressionStack.clear(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package org.example; | ||
|
||
public enum Choice { | ||
HISTORY(1), CALCULATION(2), END(3),WRONGNUMBER(4); | ||
|
||
private final int inputValue; | ||
private static final Choice[] CHOICES = Choice.values(); | ||
|
||
Choice(int inputValue) { | ||
this.inputValue = inputValue; | ||
} | ||
|
||
public static Choice of(int inputValue) { | ||
if (inputValue < 1 || inputValue > 3) { | ||
return CHOICES[3]; | ||
} | ||
return CHOICES[inputValue - 1]; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package org.example.Input; | ||
|
||
public interface Input { | ||
int inputNumber(); | ||
String inputExpression(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 inputNumber() { | ||
return scanner.nextInt(); | ||
} | ||
|
||
@Override | ||
public String inputExpression() { | ||
scanner.nextLine(); | ||
return scanner.nextLine(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
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.Stack; | ||
|
||
public class Main { | ||
private static int choice; | ||
|
||
public static void main(String[] args) { | ||
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) { | ||
show.showMenu(); | ||
choice = input.inputNumber(); | ||
Choice menuChoice = Choice.of(choice); | ||
show.lineBreak(); | ||
switch (menuChoice) { | ||
case HISTORY: | ||
List<String> records = repository.getRecords(); | ||
show.showRecords(records); | ||
break; | ||
case CALCULATION: | ||
String expression = input.inputExpression(); | ||
Stack<String> expressionStack = preProcess.expressionToStack(expression); | ||
int result = calculator.calculate(expressionStack); | ||
repository.save(expression, result); | ||
show.showResult(result); | ||
break; | ||
case WRONGNUMBER: | ||
show.showInvalidInput(); | ||
break; | ||
case END: | ||
return; | ||
} | ||
show.lineBreak(); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package org.example.Output; | ||
|
||
import java.util.List; | ||
|
||
public interface Show { | ||
void showMenu(); | ||
|
||
void showRecords(List<String> records); | ||
|
||
void showResult(int result); | ||
|
||
void lineBreak(); | ||
|
||
void showInvalidInput(); | ||
} |
31 changes: 31 additions & 0 deletions
31
calculator/src/main/java/org/example/Output/ShowingText.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.example.Output; | ||
|
||
import java.util.List; | ||
|
||
public class ShowingText implements Show { | ||
@Override | ||
public void showMenu() { | ||
System.out.println("1. 조회\n2. 계산\n3. 종료\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); | ||
} | ||
|
||
@Override | ||
public void lineBreak() { | ||
System.out.println(); | ||
} | ||
|
||
@Override | ||
public void showInvalidInput() { | ||
System.out.println("잘못된 값을 입력하였습니다."); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
calculator/src/main/java/org/example/Repository/ExpressionRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package org.example.Repository; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class ExpressionRepository implements Repository { | ||
private static List<String> store = new ArrayList<>(); | ||
|
||
@Override | ||
public void save(String expression, int result) { | ||
store.add(expression + " = " + result); | ||
} | ||
|
||
@Override | ||
public List<String> getRecords() { | ||
return store; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
calculator/src/main/java/org/example/Repository/Repository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package org.example.Repository; | ||
|
||
import java.util.List; | ||
|
||
public interface Repository { | ||
void save(String expression, int result); | ||
List<String> getRecords(); | ||
} |
36 changes: 36 additions & 0 deletions
36
calculator/src/test/java/org/example/Calculate/CalculateImplTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package org.example.Calculate; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Stack; | ||
|
||
class CalculateImplTest { | ||
|
||
PreProcess preProcess = new PreProcessImpl(); | ||
Calculate calculator = new Calculator(new CalOrderImpl()); | ||
|
||
@Test | ||
void calculateTest() { | ||
String expression = "2 * 3 - 6 / 3"; | ||
Stack<String> expressionStack = preProcess.expressionToStack(expression); | ||
int result = calculator.calculate(expressionStack); | ||
Assertions.assertEquals(result, 4); | ||
} | ||
|
||
@Test | ||
void calculateTest2() { | ||
String expression = "3 * 6 * 2 / 2"; | ||
Stack<String> expressionStack = preProcess.expressionToStack(expression); | ||
int result = calculator.calculate(expressionStack); | ||
Assertions.assertEquals(result, 18); | ||
} | ||
|
||
@Test | ||
void calculateTest3() { | ||
String expression = "3 + 6 + 2 + 2"; | ||
Stack<String> expressionStack = preProcess.expressionToStack(expression); | ||
int result = calculator.calculate(expressionStack); | ||
Assertions.assertEquals(result, 13); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
inputChoice()라는 메서드명과 실제로 하고 있는 일이 조금 달라보여요.