-
Notifications
You must be signed in to change notification settings - Fork 85
[계산기] 권민관 미션 1차 수정본 입니다! #25
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
mlngwan
wants to merge
40
commits into
next-step:mlngwan
Choose a base branch
from
mlngwan:step1
base: mlngwan
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
40 commits
Select commit
Hold shift + click to select a range
74f7544
docs: 기능 명세서 작성
mlngwan 903f21d
feat: 기초적인 계산기 초기 단계 구현
mlngwan 2823002
Docs: TODO 세분화 후 README 수정
mlngwan b0c6ed1
Remove: OutputView 삭제
mlngwan 3bfb2cf
Rename: 파일명 수정
mlngwan ed9a151
feat: MVC 구조를 위한 Controller 추가
mlngwan a082226
Refactor: Controller 추가로 인한 Caculator 수정
mlngwan 5352af0
feat: Calculator Test 초안 작성
mlngwan 2b11bab
feat: Calculator 계산 테스트 구현
mlngwan cf79840
feat: 입력내용 저장 테스트 구현
mlngwan ec61541
refactor: Test를 위한 CalculatorView 코드 수정
mlngwan b8751fc
docs: step3 진행을 위한 문서 작성
mlngwan ae57faf
Rename: step3 진행을 위한 문서 이동
mlngwan 6b1fde1
Feat: StringCalculator 구현
mlngwan f7900c1
Feat: StringCalculator DEFAULT 값 추가 및 숫자가 아닌 다른 형식의 경우 오류값 출력 구현
mlngwan b1398c6
Test: 테스트명 직관적으로 수정
mlngwan 31642a6
Test: 문자열 계산기 Test 구현
mlngwan 63e45f6
Test: 문자열 계산기 Test 쓸모없는 Import 삭제
mlngwan d97a6c6
Test: AssertJ를 활용한 Test 작성
mlngwan cfe56c3
Refactor: AssertJ 사용을 위한 dependencies 추가
mlngwan eb5a102
Rename: Test Code 위치 변경 및 파일 이름 수정
mlngwan fc96402
Docs: step4를 위한 문서 작성
mlngwan c175799
Refactor: 코드 컨벤션 - 개행 및 정렬 수정
mlngwan cab781b
Refactor: Test Code 네이밍 수정
mlngwan 127590a
Refactor: 상수값 네이밍 재설정 및 새로운 상수값 설정
mlngwan a2f073b
Remove: InputView와 OutputView로 분리로 인한 삭제
mlngwan 6efad2c
Feat: OutView 구현
mlngwan b14e0b1
Refactor: 불필요한 개행 삭제
mlngwan b01efb9
feat: view 분리 - InputView 구현
mlngwan a333ec8
Refactor: 요구사항 준수, 기존의 view에서 담당하던 로직을 model에서 처리하기, 예외처리 추가
mlngwan 3e57660
Refactor: View와 Model 리팩토링으로 인해 수정, 기존의 outputView가 담당해야 하는 부분 수정
mlngwan 76e6def
Refactor: Test 결과 예외처리 부분 수정
mlngwan 78c9949
Test: 리팩토링 후 새로운 Test 코드 작성
mlngwan d6e7d1c
Test: 테스트코드 상수 설정 및 정렬
mlngwan 40c8548
Test: 테스트코드 상수 설정, 및 개행 수정, 중첩 테스트 클래스 설정
mlngwan b115121
Test: 테스트코드 상수 설정, 및 개행 수정, 중첩 테스트 클래스 설정
mlngwan 4fb6e5c
Refactor: 사용자의 입력을 받는 inputView EOF처리
mlngwan 8de5ddc
Refactor: 파일의 끝에 개행을 통해 EOF 처리
mlngwan 4601969
Test: 컨벤션 지켜지지 않은 부분 수정
mlngwan f185842
Refactoring: 불필요한 부분 제거로 코드 간결화
mlngwan 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
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,65 @@ | ||
## Step1 & Step2 (초간단 계산기 구현 및 테스트) | ||
### 기능 요구사항 | ||
인자 2개를 받아 사칙연산을 할 수 있는 계산기를 구현한다. | ||
사칙연산과 매칭되는 4개의 메서드를 제공한다. | ||
계산된 결과는 정수를 반환한다. | ||
|
||
### 새로운 프로그래밍 요구사항 | ||
메인 메서드는 만들지 않는다. | ||
|
||
### 단위 테스트 | ||
작은 단위로 테스트 할 것. | ||
|
||
### ToDo | ||
- 사칙연산 구현하기 | ||
- 최대한 MVC 구조로 구현하기 | ||
- JUnit 공부하기 | ||
- JUnit 활용하기 | ||
- 단위 테스트 구현하기 | ||
|
||
## Step 3 (문자열 계산기) | ||
### 기능 요구사항 | ||
- 기본적으로 쉼표(,)나 콜론(:)을 구분자로 가지는 경우 | ||
- 기본 구분자로 분리한 각 숫자 합을 반환 | ||
- (예: "" => 0, "1,2" => 3, "1,2:3" => 6) | ||
- 커스텀 구분자 지정 가능, "//"와 "\n" 사이에 위치하는 문자를 사용하는 경우 | ||
-커스텀 구분자로 분리해서 각 숫자 합을 반환 | ||
- (예: "//;\n1;2;3" => 구분자:";", 결과로 6 반환) | ||
- 문자열 계산기에 숫자 이외의 값 전달 경우 RuntimeException 예외 throw | ||
- 문자열 계산기에 음수를 전달하는 경우 RuntimeException 예외 throw | ||
|
||
### 새로운 프로그래밍 요구사항 | ||
- 구현한 문자열 계산기가 예상한대로 작동하는지 JUnit5 활용하여 테스트 자동화 | ||
- 조금 더 복잡한 도메인 대상으로 테스트 작성 경험 | ||
|
||
### 기존 프로그래밍 요구사항 | ||
- 메인 메서드 작성하지 않기 | ||
|
||
### ToDo | ||
- 문자열 계산기 구현하기 | ||
- MVC 구조로 작성하기 | ||
- JUnit5 기반 테스트 작성하기 | ||
- 필요기능? | ||
- AssertJ 기반 테스트 작성하기 | ||
- AssertJ 관련 학습하기 | ||
- AssertJ로 구현하기 | ||
|
||
## Step4 리팩토링 | ||
### 코드 작성 시 주의할 점 | ||
- 클래스 첫 줄에는 개행 넣기 -> 코드 컨벤션 (개행 알아보기) | ||
- 코드에 EOF 발생 -> 무엇인지, 처리법 | ||
- 코드 내부에 사용하는 메서드의 접근 제어자 설정 | ||
- 상수 처리 및 네이밍 | ||
- 메서드 명은 동사로 시작하기 | ||
- static 사용이유 | ||
- test code 네이밍 알아보기 | ||
- test code given-when-then 알아보기 | ||
|
||
### 초간단 계산기 | ||
- 사용자의 입력에 따라서 해당 연산 실행하게 바꾸기 | ||
- 음수일 때 예외 추가 | ||
- 나누는 수가 0일때 예외 처리 | ||
- view에 로직을 처리하지 않게 하기 -> model에서 처리하기 | ||
|
||
### 문자열 계산기 | ||
- sum 메서드 처리 |
43 changes: 43 additions & 0 deletions
43
src/main/java/simpleCalculator/controller/CalculatorController.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,43 @@ | ||
package simpleCalculator.controller; | ||
|
||
import simpleCalculator.model.Calculator; | ||
import simpleCalculator.model.StringCalculator; | ||
import simpleCalculator.view.InputView; | ||
import simpleCalculator.view.OutView; | ||
|
||
public class CalculatorController { | ||
|
||
private InputView inputView; | ||
private OutView outView; | ||
private Calculator calculator; | ||
private StringCalculator stringCalculator; | ||
|
||
public CalculatorController() { | ||
this.inputView = new InputView(); | ||
this.outView = new OutView(); | ||
this.calculator = new Calculator(); | ||
this.stringCalculator = new StringCalculator(); | ||
} | ||
|
||
private void runSimpleCalculator() { | ||
String input = inputView.getSimpleNumber(); | ||
calculator.setNumber(input); | ||
|
||
int resultAdd = calculator.addNumbers(); | ||
int resultSub = calculator.subNumbers(); | ||
int resultDiv = calculator.divideNumbers(); | ||
int resultMul = calculator.multipleNumbers(); | ||
|
||
outView.printResult(resultAdd); | ||
outView.printResult(resultSub); | ||
outView.printResult(resultDiv); | ||
outView.printResult(resultMul); | ||
} | ||
|
||
private void runStringCalculator() { | ||
String input = inputView.getStringNumber(); | ||
int result = stringCalculator.add(input); | ||
|
||
outView.printResult(result); | ||
} | ||
} |
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,68 @@ | ||
package simpleCalculator.model; | ||
|
||
public class Calculator { | ||
|
||
private static final int ZERO = 0; | ||
private static final int ONE = 1; | ||
private static final int TWO = 2; | ||
|
||
private int num1; | ||
private int num2; | ||
|
||
private boolean checkHowManyInput(String[] inputList) { | ||
return inputList.length != TWO; | ||
} | ||
|
||
private String[] splitList(String input) { | ||
return input.split(","); | ||
} | ||
|
||
private int convertToInt(String input) { | ||
int number = Integer.parseInt(input); | ||
if (number < ZERO) { | ||
throw new RuntimeException("음수는 입력할 수 없습니다."); | ||
} | ||
return number; | ||
} | ||
|
||
private int[] convertToIntList(String[] inputList) { | ||
try { | ||
int[] numberList = new int[inputList.length]; | ||
for (int i = ZERO; i < inputList.length; i++) { | ||
numberList[i] = convertToInt(inputList[i]); | ||
} | ||
return numberList; | ||
} catch (NumberFormatException e) { | ||
throw new RuntimeException("숫자가 아닌 다른 형식이 포함되어 있습니다."); | ||
} | ||
} | ||
|
||
public void setNumber(String input) { | ||
String[] inputList = splitList(input); | ||
if (checkHowManyInput(inputList)) { | ||
throw new RuntimeException("2개의 숫자를 입력해주세요."); | ||
} | ||
int[] numbers = convertToIntList(inputList); | ||
this.num1 = numbers[ZERO]; | ||
this.num2 = numbers[ONE]; | ||
} | ||
|
||
public int addNumbers() { | ||
return num1 + num2; | ||
} | ||
|
||
public int subNumbers() { | ||
return num1 - num2; | ||
} | ||
|
||
public int divideNumbers() { | ||
if (num2 == 0) { | ||
throw new RuntimeException("0으로 나누기는 불가합니다."); | ||
} | ||
return num1 / num2; | ||
} | ||
|
||
public int multipleNumbers() { | ||
return num1 * num2; | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
src/main/java/simpleCalculator/model/StringCalculator.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 simpleCalculator.model; | ||
|
||
public class StringCalculator { | ||
|
||
private static final String SEPARATOR_DEFAULTS = ",|:"; | ||
private static final int ZERO = 0; | ||
|
||
private boolean checkBlank(String input) { | ||
return input == null || input.isEmpty(); | ||
} | ||
|
||
private String[] splitInput(String input) { | ||
if (input.startsWith("//")) { | ||
return splitByCustom(input); | ||
} | ||
return input.split(SEPARATOR_DEFAULTS); | ||
} | ||
|
||
private String[] splitByCustom(String input) { | ||
String[] separates = input.split("\n", 2); | ||
String custom = separates[ZERO].substring(2); | ||
return separates[1].split(custom); | ||
} | ||
|
||
private int[] listToInt(String[] inputList) { | ||
try { | ||
int[] numberList = new int[inputList.length]; | ||
for (int i = ZERO; i < inputList.length; i++) { | ||
numberList[i] = convertToInt(inputList[i]); | ||
} | ||
return numberList; | ||
} catch (NumberFormatException e) { | ||
throw new RuntimeException("숫자가 아닌 다른 형식이 포함되었습니다."); | ||
} | ||
} | ||
|
||
private int convertToInt(String input) { | ||
int number = Integer.parseInt(input); | ||
if (number < ZERO) { | ||
throw new RuntimeException("음수는 입력할 수 없습니다."); | ||
} | ||
return number; | ||
} | ||
|
||
private int sum(int[] inputList) { | ||
int total = ZERO; | ||
for (int number : inputList) { | ||
total += number; | ||
} | ||
return total; | ||
} | ||
|
||
public int add(String input) { | ||
if (checkBlank(input)) { | ||
return ZERO; | ||
} | ||
return sum(listToInt(splitInput(input))); | ||
} | ||
} |
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,22 @@ | ||
package simpleCalculator.view; | ||
|
||
import java.util.Scanner; | ||
|
||
public class InputView { | ||
|
||
private final Scanner scanner; | ||
|
||
public InputView() { | ||
this.scanner = new Scanner(System.in); | ||
} | ||
|
||
public String getSimpleNumber() { | ||
System.out.println("계산할 내용을 입력해주세요.(숫자,연산자,숫자 형식)"); | ||
return scanner.nextLine(); | ||
} | ||
|
||
public String getStringNumber() { | ||
System.out.println("기본(, :)이나 커스텀 구분자(;)를 가지는 문자열을 입력해주세요. (예: 1,2 => 3, //;\\n1;2;3 => 6 반환)"); | ||
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,8 @@ | ||
package simpleCalculator.view; | ||
|
||
public class OutView { | ||
|
||
public void printResult(int result) { | ||
System.out.println("계산 결과: " + result); | ||
} | ||
} |
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,78 @@ | ||
package calculator; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
import simpleCalculator.model.Calculator; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
public class CalculatorTest { | ||
|
||
private static final int EXPECT_VALUE = 3; | ||
|
||
private Calculator calculator; | ||
|
||
@BeforeEach | ||
void setCalculator() { | ||
calculator = new Calculator(); | ||
} | ||
|
||
@DisplayName("연산기능 테스트") | ||
@Nested | ||
class functionTest { | ||
|
||
@DisplayName("더하기") | ||
@Test | ||
void testAdd() { | ||
calculator.setNumber("1,2"); | ||
assertEquals(EXPECT_VALUE, calculator.addNumbers()); | ||
} | ||
|
||
@DisplayName("빼기") | ||
@Test | ||
void testSub() { | ||
calculator.setNumber("4,1"); | ||
assertEquals(EXPECT_VALUE, calculator.subNumbers()); | ||
} | ||
|
||
@DisplayName("나누기") | ||
@Test | ||
void testDiv() { | ||
calculator.setNumber("9,3"); | ||
assertEquals(EXPECT_VALUE, calculator.divideNumbers()); | ||
} | ||
|
||
@DisplayName("곱하기") | ||
@Test | ||
void testAMul() { | ||
calculator.setNumber("3,1"); | ||
assertEquals(EXPECT_VALUE, calculator.multipleNumbers()); | ||
} | ||
} | ||
|
||
@DisplayName("예외처리 테스트") | ||
@Nested | ||
class exceptionTest { | ||
|
||
@DisplayName("0으로 나누기를 시도한 경우") | ||
@Test | ||
void testDivideByZero() { | ||
calculator.setNumber("10,0"); | ||
assertThrows(RuntimeException.class, () -> calculator.divideNumbers()); | ||
} | ||
|
||
@DisplayName("입력을 2개 미만으로 시도한 경우") | ||
@Test | ||
void testInputUnderTwo() { | ||
assertThrows(RuntimeException.class, () -> calculator.setNumber("1")); | ||
} | ||
|
||
@DisplayName("입력을 2개 초과로 시도한 경우") | ||
@Test | ||
void testInputOverTwo() { | ||
assertThrows(RuntimeException.class, () -> calculator.setNumber("1,2,3")); | ||
} | ||
} | ||
} |
Oops, something went wrong.
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.
해당 클래스 외에도 첫줄 개행 추가해주세요
Uh oh!
There was an error while loading. Please reload this page.
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.
네 수정하겠습니다!