From 0dbf2c731e8168e424e27c4aab62988e1cc3d38f Mon Sep 17 00:00:00 2001 From: Heesoo Date: Tue, 15 Oct 2024 18:16:26 +0900 Subject: [PATCH 1/5] =?UTF-8?q?[feat]=20=EC=88=AB=EC=9E=90=20=EC=95=BC?= =?UTF-8?q?=EA=B5=AC=20=EA=B2=8C=EC=9E=84=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 2 + src/main/java/BaseballApplication.java | 21 ++ src/main/java/study/Baseball.java | 61 +++++ src/main/java/study/BaseballScanner.java | 81 ++++++ src/main/java/study/BaseballScore.java | 61 +++++ src/main/java/study/ErrorCode.java | 25 ++ src/test/java/study/BaseballScannerTest.java | 263 +++++++++++++++++++ src/test/java/study/BaseballScoreTest.java | 179 +++++++++++++ src/test/java/study/BaseballTest.java | 142 ++++++++++ src/test/java/study/StringTest.java | 13 - 10 files changed, 835 insertions(+), 13 deletions(-) create mode 100644 src/main/java/BaseballApplication.java create mode 100644 src/main/java/study/Baseball.java create mode 100644 src/main/java/study/BaseballScanner.java create mode 100644 src/main/java/study/BaseballScore.java create mode 100644 src/main/java/study/ErrorCode.java create mode 100644 src/test/java/study/BaseballScannerTest.java create mode 100644 src/test/java/study/BaseballScoreTest.java create mode 100644 src/test/java/study/BaseballTest.java delete mode 100644 src/test/java/study/StringTest.java diff --git a/build.gradle b/build.gradle index 8172fb7..d585e57 100644 --- a/build.gradle +++ b/build.gradle @@ -12,6 +12,8 @@ repositories { dependencies { testImplementation "org.junit.jupiter:junit-jupiter:5.7.2" testImplementation "org.assertj:assertj-core:3.19.0" + testImplementation 'org.mockito:mockito-core:4.11.0' + } test { diff --git a/src/main/java/BaseballApplication.java b/src/main/java/BaseballApplication.java new file mode 100644 index 0000000..de8b7e8 --- /dev/null +++ b/src/main/java/BaseballApplication.java @@ -0,0 +1,21 @@ +import study.Baseball; +import study.BaseballScanner; + +public class BaseballApplication { + public static void main(String[] args){ + Baseball baseball = new Baseball(); + boolean isReplay = true; + + while(isReplay){ + String result = baseball.play(); + System.out.println(result); + + BaseballScanner scanner = new BaseballScanner(); + isReplay = scanner.isReplay(); + } + + System.out.println("게임이 종료되었습니다."); + + + } +} diff --git a/src/main/java/study/Baseball.java b/src/main/java/study/Baseball.java new file mode 100644 index 0000000..a6bd0fb --- /dev/null +++ b/src/main/java/study/Baseball.java @@ -0,0 +1,61 @@ +package study; + +import java.util.*; + +public class Baseball { + public String play(){ + List answerList = getAnswer(); + boolean collect = false; + + BaseballScanner scanner = new BaseballScanner(); + System.out.println(answerList.toString()); + + while(!collect){ + List input = scanner.execute(); + collect = checkAnswer(input, answerList); + } + + return "3개의 숫자를 모두 맞히셨습니다! 게임 종료"; + } + + + public boolean checkAnswer(List inputList, List answerList){ + BaseballScore score = new BaseballScore(); + + for (int i=0; i getAnswer() { + List answers = new ArrayList<>(); + + while(answers.size() < 3){ + int num = getRandomNumber(); + answers = addUniqueNumber(num, answers); + } + + return answers; + + } + + public List addUniqueNumber(int num, List answers){ + if (answers.stream().anyMatch(n -> n == num)){ + return answers; + } + + answers.add(num); + return answers; + } + + public int getRandomNumber(){ + Random random = new Random(); + return random.nextInt(9)+1; + } +} diff --git a/src/main/java/study/BaseballScanner.java b/src/main/java/study/BaseballScanner.java new file mode 100644 index 0000000..7b13f1a --- /dev/null +++ b/src/main/java/study/BaseballScanner.java @@ -0,0 +1,81 @@ +package study; + +import java.util.*; + +public class BaseballScanner { + private final Scanner scanner; + + public BaseballScanner(){ + this.scanner = new Scanner(System.in); + } + + public List execute() { + System.out.println("숫자를 입력해 주세요 : "); + String input; + + try{ + input = checkLength(scanner.next()); + } catch (NoSuchElementException ex){ + throw new NoSuchElementException(ErrorCode.INVALID_INPUT.getMessage()); + } + + return convertList(input); + } + + public String checkLength(String input){ + input = input.replace(" ", ""); + + if (input.length() != 3){ + throw new NoSuchElementException(ErrorCode.INVALID_INPUT.getMessage()); + } + return input; + } + + public List convertList(String input){ + List numbers = new ArrayList<>(); + String[] inputArr = input.split(""); + + for (String str: inputArr){ + int number = parseStringToInt(str); + numbers.add(number); + } + + return numbers; + } + + public int parseStringToInt(String str){ + int result; + + try{ + result = Integer.parseInt(str); + } catch (NumberFormatException ex){ + throw new NumberFormatException(ErrorCode.INVALID_INPUT_NUMBER.getMessage()); + } + + return result; + } + + public boolean isReplay(){ + System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); + int input; + + try{ + input = scanner.nextInt(); + } catch (NoSuchElementException ex){ + throw new NoSuchElementException(ErrorCode.INVALID_INPUT_REPLAY.getMessage()); + } + + if (input == 2){ + return false; + } else if(input != 1){ + throw new NoSuchElementException(ErrorCode.INVALID_INPUT_REPLAY.getMessage()); + } + + return true; + + } + + public void close(){ + scanner.close(); + } +} diff --git a/src/main/java/study/BaseballScore.java b/src/main/java/study/BaseballScore.java new file mode 100644 index 0000000..935856a --- /dev/null +++ b/src/main/java/study/BaseballScore.java @@ -0,0 +1,61 @@ +package study; + +public class BaseballScore { + private int strikeCnt; + private int ballCnt; + private int nothingCnt; + + + public BaseballScore() { + this.strikeCnt = 0; + this.ballCnt = 0; + this.nothingCnt = 0; + } + + public BaseballScore(int strikeCnt, int ballCnt, int nothingCnt) { + this.strikeCnt = strikeCnt; + this.ballCnt = ballCnt; + this.nothingCnt = nothingCnt; + } + + + public void checkScore(int index, int i){ + if (index == -1){ + nothingCnt++; + } else if (index == i){ + strikeCnt++; + } else if(index != i){ + ballCnt++; + } + } + + public boolean isThreeStrike(){ + return strikeCnt == 3; + } + + public String printScore(){ + if(nothingCnt == 3) { + return "낫싱"; + } else if (ballCnt != 0 && strikeCnt != 0){ + return ballCnt + "볼 " + strikeCnt + "스트라이크"; + } else if (ballCnt == 0 && strikeCnt != 0){ + return strikeCnt + "스트라이크"; + } else if(ballCnt != 0){ + return ballCnt + "볼"; + } + + return ""; + } + + public int getStrikeCnt() { + return strikeCnt; + } + + public int getBallCnt() { + return ballCnt; + } + + public int getNothingCnt() { + return nothingCnt; + } +} diff --git a/src/main/java/study/ErrorCode.java b/src/main/java/study/ErrorCode.java new file mode 100644 index 0000000..e0ead83 --- /dev/null +++ b/src/main/java/study/ErrorCode.java @@ -0,0 +1,25 @@ +package study; + +public enum ErrorCode { + + INVALID_INPUT(1, "3자리 숫자를 입력해주세요."), + INVALID_INPUT_NUMBER(2, "숫자만 입력 가능합니다."), + INVALID_INPUT_REPLAY(3, "1 또는 2 숫자만 입력 가능합니다."); + + + private final int id; + private final String message; + + ErrorCode(int id, String message) { + this.id = id; + this.message = message; + } + + public int getId() { + return id; + } + + public String getMessage() { + return message; + } +} diff --git a/src/test/java/study/BaseballScannerTest.java b/src/test/java/study/BaseballScannerTest.java new file mode 100644 index 0000000..c15da17 --- /dev/null +++ b/src/test/java/study/BaseballScannerTest.java @@ -0,0 +1,263 @@ +package study; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.util.List; +import java.util.NoSuchElementException; + +import static org.junit.jupiter.api.Assertions.*; + +public class BaseballScannerTest { + + @Test + @DisplayName("execute(): 사용자 입력 성공") + void execute(){ + // given + String input = "456"; + InputStream in = new ByteArrayInputStream(input.getBytes()); + System.setIn(in); + + // when + BaseballScanner scanner = new BaseballScanner(); + List response = scanner.execute(); + + // then + assertEquals(response.get(0), 4); + assertEquals(response.get(1), 5); + assertEquals(response.get(2), 6); + } + + @Test + @DisplayName("execute(): 공백이 포함된 숫자 입력으로 예외 발생") + void executeContainEmpty_exception(){ + // given + String input = "4 6"; + InputStream in = new ByteArrayInputStream(input.getBytes()); + System.setIn(in); + + // when + BaseballScanner scanner = new BaseballScanner(); + NoSuchElementException fail = assertThrows(NoSuchElementException.class, scanner::execute); + + // then + assertEquals(fail.getMessage(), ErrorCode.INVALID_INPUT.getMessage()); + } + + @Test + @DisplayName("execute(): 숫자 3자리 이상을 입력으로 예외 발생") + void executeInputOverSizeThree_exception(){ + // given + String input = "123456"; + InputStream in = new ByteArrayInputStream(input.getBytes()); + System.setIn(in); + + // when + BaseballScanner scanner = new BaseballScanner(); + NoSuchElementException fail = assertThrows(NoSuchElementException.class, scanner::execute); + + // then + assertEquals(fail.getMessage(), ErrorCode.INVALID_INPUT.getMessage()); + } + + @Test + @DisplayName("execute(): 문자열 입력으로 예외 발생") + void executeInputString_exception(){ + // given + String input = "문자열"; + InputStream in = new ByteArrayInputStream(input.getBytes()); + System.setIn(in); + + // when + BaseballScanner scanner = new BaseballScanner(); + NumberFormatException fail = assertThrows(NumberFormatException.class, scanner::execute); + + // then + assertEquals(fail.getMessage(), ErrorCode.INVALID_INPUT_NUMBER.getMessage()); + } + + @Test + @DisplayName("execute(): 공백 입력으로 예외 발생") + void executeInputEmpty_exception(){ + // given + String input = " "; + InputStream in = new ByteArrayInputStream(input.getBytes()); + System.setIn(in); + + // when + BaseballScanner scanner = new BaseballScanner(); + NoSuchElementException fail = assertThrows(NoSuchElementException.class, scanner::execute); + + // then + assertEquals(fail.getMessage(), ErrorCode.INVALID_INPUT.getMessage()); + } + + @Test + @DisplayName("checkLength(): 숫자 3자리 검증 성공") + void checkLength(){ + // given + String input = "123"; + + // when + BaseballScanner scanner = new BaseballScanner(); + String response = scanner.checkLength(input); + + // then + assertEquals(response, input); + } + + @Test + @DisplayName("checkLength(): 숫자 3자리 이상을 입력으로 예외 발생") + void checkLengthInputOverSizeThree_exception(){ + // given + String input = "123456"; + + // when + BaseballScanner scanner = new BaseballScanner(); + NoSuchElementException fail = assertThrows(NoSuchElementException.class, () -> scanner.checkLength(input)); + + // then + assertEquals(fail.getMessage(), ErrorCode.INVALID_INPUT.getMessage()); + } + + @Test + @DisplayName("parseStringToInt(): 문자열 숫자로 변환 성공") + void parseStringToInt(){ + // given + String input = "123"; + + // when + BaseballScanner scanner = new BaseballScanner(); + int response = scanner.parseStringToInt(input); + + // then + assertEquals(response, 123); + } + + @Test + @DisplayName("parseStringToInt(): 문자열 숫자 변환 실패로 예외 발생") + void parseInputString_exception(){ + // given + String input = "문자열"; + + // when + BaseballScanner scanner = new BaseballScanner(); + NumberFormatException fail = assertThrows(NumberFormatException.class, () -> scanner.parseStringToInt(input)); + + // then + assertEquals(fail.getMessage(), ErrorCode.INVALID_INPUT_NUMBER.getMessage()); + } + + @Test + @DisplayName("convertList(): 문자열을 숫자 리스트로 변환 성공") + void convertList(){ + // given + String input = "123"; + + // when + BaseballScanner scanner = new BaseballScanner(); + List response = scanner.convertList(input); + + // then + assertEquals(response.get(0), 1); + assertEquals(response.get(1), 2); + assertEquals(response.get(2), 3); + } + + @Test + @DisplayName("convertList(): 문자열 숫자 리스트 변환 실패로 예외 발생") + void convertListInputString_exception(){ + // given + String input = "문자열"; + + // when + BaseballScanner scanner = new BaseballScanner(); + NumberFormatException fail = assertThrows(NumberFormatException.class, () -> scanner.convertList(input)); + + // then + assertEquals(fail.getMessage(), ErrorCode.INVALID_INPUT_NUMBER.getMessage()); + } + + @Test + @DisplayName("isReplayScanner(): 재게임 여부 1 입력으로 true 반환") + void isReplayInput1(){ + // given + String input = "1"; + InputStream in = new ByteArrayInputStream(input.getBytes()); + System.setIn(in); + + // when + BaseballScanner scanner = new BaseballScanner(); + boolean response = scanner.isReplay(); + + // then + assertTrue(response); + } + + @Test + @DisplayName("isReplayScanner(): 재게임 여부 2 입력으로 false 반환") + void isReplayInput2(){ + // given + String input = "2"; + InputStream in = new ByteArrayInputStream(input.getBytes()); + System.setIn(in); + + // when + BaseballScanner scanner = new BaseballScanner(); + boolean response = scanner.isReplay(); + + // then + assertFalse(response); + } + + @Test + @DisplayName("isReplayScanner(): 문자열 입력으로 예외 발생") + void isReplayInputString_exception(){ + // given + String input = "문자열"; + InputStream in = new ByteArrayInputStream(input.getBytes()); + System.setIn(in); + + // when + BaseballScanner scanner = new BaseballScanner(); + NoSuchElementException fail = assertThrows(NoSuchElementException.class, scanner::isReplay); + + // then + assertEquals(fail.getMessage(), ErrorCode.INVALID_INPUT_REPLAY.getMessage()); + } + + + @Test + @DisplayName("isReplayScanner(): 숫자 2자리 입력") + void isReplayInputTwoNumber_exception(){ + String input = "34"; + InputStream in = new ByteArrayInputStream(input.getBytes()); + System.setIn(in); + + // when + BaseballScanner scanner = new BaseballScanner(); + NoSuchElementException fail = assertThrows(NoSuchElementException.class, scanner::isReplay); + + // then + assertEquals(fail.getMessage(), ErrorCode.INVALID_INPUT_REPLAY.getMessage()); + } + + @Test + @DisplayName("execute(): 공백 입력으로 예외 발생") + void isReplayInputEmpty_exception(){ + // given + String input = " "; + InputStream in = new ByteArrayInputStream(input.getBytes()); + System.setIn(in); + + // when + BaseballScanner scanner = new BaseballScanner(); + NoSuchElementException fail = assertThrows(NoSuchElementException.class, scanner::isReplay); + + // then + assertEquals(fail.getMessage(), ErrorCode.INVALID_INPUT_REPLAY.getMessage()); + } + +} diff --git a/src/test/java/study/BaseballScoreTest.java b/src/test/java/study/BaseballScoreTest.java new file mode 100644 index 0000000..03e4986 --- /dev/null +++ b/src/test/java/study/BaseballScoreTest.java @@ -0,0 +1,179 @@ +package study; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +public class BaseballScoreTest { + + @Test + @DisplayName("checkScore(): 스트라이크인 경우") + void checkScore_strike(){ + // given + int index = 1; + int i = 1; + + // when + BaseballScore score = new BaseballScore(); + score.checkScore(index, i); + + // then + assertEquals(score.getBallCnt(), 0); + assertEquals(score.getStrikeCnt(), 1); + assertEquals(score.getNothingCnt(), 0); + } + + @Test + @DisplayName("checkScore(): 볼인 경우") + void checkScore_ball(){ + // given + int index = 2; + int i = 1; + + // when + BaseballScore score = new BaseballScore(); + score.checkScore(index, i); + + // then + assertEquals(score.getBallCnt(), 1); + assertEquals(score.getStrikeCnt(), 0); + assertEquals(score.getNothingCnt(), 0); + } + + @Test + @DisplayName("checkScore(): ball, strike 둘 다 아닌 경우") + void checkScore_nothing(){ + // given + int index = -1; + int i = 1; + + // when + BaseballScore score = new BaseballScore(); + score.checkScore(index, i); + + // then + assertEquals(score.getBallCnt(), 0); + assertEquals(score.getStrikeCnt(), 0); + assertEquals(score.getNothingCnt(), 1); + } + + @Test + @DisplayName("isThreeStrike(): 3 스트라이크인 경우 true 반환") + void isThreeStrike(){ + // given + BaseballScore score = new BaseballScore(3, 0, 0); + + // when + boolean response = score.isThreeStrike(); + + // then + assertTrue(response); + } + + @Test + @DisplayName("isThreeStrike(): 3 스트라이크 아닌 경우 false 반환") + void isNotThreeStrike(){ + // given + BaseballScore score = new BaseballScore(1, 1, 1); + + // when + boolean response = score.isThreeStrike(); + + // then + assertFalse(response); + } + + @Test + @DisplayName("볼, 스트라이크 점수 출력") + void scorePrintBallAndStrike(){ + // given + int ballCnt = 1; + int strikeCnt = 2; + int nothingCnt = 0; + + BaseballScore score = new BaseballScore(strikeCnt, ballCnt, nothingCnt); + + // when + String response = score.printScore(); + + // then + assertEquals(response, ballCnt + "볼 " + strikeCnt + "스트라이크"); + + } + + @Test + @DisplayName("3 스트라이크 점수 출력") + void scorePrint3Strike(){ + // given + int ballCnt = 0; + int strikeCnt = 3; + int nothingCnt = 0; + + BaseballScore score = new BaseballScore(strikeCnt, ballCnt, nothingCnt); + + // when + String response = score.printScore(); + + // then + assertEquals(response, strikeCnt + "스트라이크"); + + } + + @Test + @DisplayName("스트라이크 점수 출력") + void scorePrintStrike(){ + // given + int ballCnt = 0; + int strikeCnt = 2; + int nothingCnt = 0; + + BaseballScore score = new BaseballScore(strikeCnt, ballCnt, nothingCnt); + + // when + String response = score.printScore(); + + // then + assertEquals(response, strikeCnt + "스트라이크"); + + } + + @Test + @DisplayName("볼 점수 출력") + void scorePrintBall(){ + // given + int ballCnt = 2; + int strikeCnt = 0; + int nothingCnt = 1; + + BaseballScore score = new BaseballScore(strikeCnt, ballCnt, nothingCnt); + + // when + String response = score.printScore(); + + // then + assertEquals(response, ballCnt + "볼"); + + } + + + @Test + @DisplayName("낫싱 점수 출력") + void scorePrintNothing(){ + // given + int ballCnt = 0; + int strikeCnt = 0; + int nothingCnt = 3; + + BaseballScore score = new BaseballScore(strikeCnt, ballCnt, nothingCnt); + + // when + String response = score.printScore(); + + // then + assertEquals(response, "낫싱"); + + } + + +} diff --git a/src/test/java/study/BaseballTest.java b/src/test/java/study/BaseballTest.java new file mode 100644 index 0000000..661d1ae --- /dev/null +++ b/src/test/java/study/BaseballTest.java @@ -0,0 +1,142 @@ +package study; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.RepeatedTest; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.when; + +public class BaseballTest { + + @Test + @DisplayName("3스트라이크") + void play3Strike(){ + // given + Baseball baseball = Mockito.spy(new Baseball()); + + List answerList = Arrays.asList(1, 2, 3); + when(baseball.getAnswer()).thenReturn(answerList); + + String input = "123"; + InputStream in = new ByteArrayInputStream(input.getBytes()); + System.setIn(in); + + // when + String response = baseball.play(); + + // then + assertEquals(response, "3개의 숫자를 모두 맞히셨습니다! 게임 종료"); + + } + + + @Test + @DisplayName("checkAnswer(): 3 스트라이크") + void checkAnswer_3Strike(){ + // given + List inputList = Arrays.asList(3, 5, 6); + List answerList = Arrays.asList(3, 5, 6); + + // when + Baseball baseball = new Baseball(); + boolean response = baseball.checkAnswer(inputList, answerList); + + // then + assertTrue(response); + } + + @Test + @DisplayName("checkAnswer(): 3 스트라이크 아닌 경우") + void checkAnswerNot3Strike(){ + // given + List inputList = Arrays.asList(3, 5, 6); + List answerList = Arrays.asList(3, 8, 1); + + // when + Baseball baseball = new Baseball(); + boolean response = baseball.checkAnswer(inputList, answerList); + + // then + assertFalse(response); + } + + + @Test + @DisplayName("getAnswer() 성공: 랜덤 3자리 수 생성") + void getAnswer(){ + // given + // when + Baseball baseball = new Baseball(); + List response = baseball.getAnswer(); + + // then + assertEquals(response.size(), 3); + assertNotNull(response.get(0)); + assertNotNull(response.get(1)); + assertNotNull(response.get(2)); + assertNotEquals(response.get(0), response.get(1)); + assertNotEquals(response.get(0), response.get(2)); + assertNotEquals(response.get(1), response.get(2)); + } + + @Test + @DisplayName("addUniqueNumber(): 리스트에 중복된 숫자 존재하지 않을 경우 리스트에 추가") + void addUniqueNumber(){ + // given + int num = 3; + List answers = new ArrayList<>(); + answers.add(4); + answers.add(1); + + // when + Baseball baseball = new Baseball(); + List response = baseball.addUniqueNumber(num, answers); + + // then + assertEquals(response.size(), 3); + assertEquals(response.get(0), answers.get(0)); + assertEquals(response.get(1), answers.get(1)); + assertEquals(response.get(2), num); + } + + @Test + @DisplayName("addUniqueNumber(): 리스트에 중복 숫자 존재하는 경우 리스트에 추가 안됨") + void addDuplicatedNumber(){ + // given + int num = 1; + List answers = new ArrayList<>(); + answers.add(4); + answers.add(1); + + // when + Baseball baseball = new Baseball(); + List response = baseball.addUniqueNumber(num, answers); + + // then + assertEquals(response.size(), answers.size()); + assertEquals(response.get(0), answers.get(0)); + assertEquals(response.get(1), answers.get(1)); + } + + + @RepeatedTest(10) // 10번 반복 테스트 + @DisplayName("getRandomNumber() 성공: 1부터 9까지 랜덤 수 생성 성공") + void getRandomNumber(){ + // given + // when + Baseball baseball = new Baseball(); + int response = baseball.getRandomNumber(); + + // then + System.out.println("response : " + response); + assertTrue(response > 0 && response <= 9); + } +} diff --git a/src/test/java/study/StringTest.java b/src/test/java/study/StringTest.java deleted file mode 100644 index 43e47d9..0000000 --- a/src/test/java/study/StringTest.java +++ /dev/null @@ -1,13 +0,0 @@ -package study; - -import org.junit.jupiter.api.Test; - -import static org.assertj.core.api.Assertions.assertThat; - -public class StringTest { - @Test - void replace() { - String actual = "abc".replace("b", "d"); - assertThat(actual).isEqualTo("adc"); - } -} From 7d38de1cfdab28d9347bd79bf12d5f2d3af39833 Mon Sep 17 00:00:00 2001 From: Heesoo Date: Fri, 18 Oct 2024 11:56:50 +0900 Subject: [PATCH 2/5] =?UTF-8?q?[refactor]=20BaseballGame=20=ED=81=B4?= =?UTF-8?q?=EB=9E=98=EC=8A=A4=20=EC=B6=94=EA=B0=80=20=EB=B0=8F=20=ED=95=A8?= =?UTF-8?q?=EC=88=98=20=EC=A0=95=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/BaseballApplication.java | 16 +++---- src/main/java/study/Baseball.java | 43 +++++++----------- src/main/java/study/BaseballGame.java | 37 +++++++++++++++ src/main/java/study/BaseballScanner.java | 57 +++++++++++++----------- src/main/java/study/BaseballScore.java | 2 +- 5 files changed, 91 insertions(+), 64 deletions(-) create mode 100644 src/main/java/study/BaseballGame.java diff --git a/src/main/java/BaseballApplication.java b/src/main/java/BaseballApplication.java index de8b7e8..8f1f83d 100644 --- a/src/main/java/BaseballApplication.java +++ b/src/main/java/BaseballApplication.java @@ -1,21 +1,19 @@ import study.Baseball; +import study.BaseballGame; import study.BaseballScanner; public class BaseballApplication { public static void main(String[] args){ + BaseballScanner scanner = new BaseballScanner(); Baseball baseball = new Baseball(); - boolean isReplay = true; + BaseballGame game = new BaseballGame(scanner, baseball); - while(isReplay){ - String result = baseball.play(); + do { + String result = game.playGame(); System.out.println(result); - BaseballScanner scanner = new BaseballScanner(); - isReplay = scanner.isReplay(); - } - - System.out.println("게임이 종료되었습니다."); - + } while(game.isReplay()); + System.out.println(game.endGame()); } } diff --git a/src/main/java/study/Baseball.java b/src/main/java/study/Baseball.java index a6bd0fb..c2c2a1d 100644 --- a/src/main/java/study/Baseball.java +++ b/src/main/java/study/Baseball.java @@ -3,41 +3,33 @@ import java.util.*; public class Baseball { - public String play(){ - List answerList = getAnswer(); - boolean collect = false; + private final List answerList; - BaseballScanner scanner = new BaseballScanner(); - System.out.println(answerList.toString()); - - while(!collect){ - List input = scanner.execute(); - collect = checkAnswer(input, answerList); - } - - return "3개의 숫자를 모두 맞히셨습니다! 게임 종료"; + public Baseball(){ + answerList = getAnswer(); } + public Baseball(List answerList) { + this.answerList = answerList; + } - public boolean checkAnswer(List inputList, List answerList){ + public BaseballScore play(List userInput){ BaseballScore score = new BaseballScore(); - for (int i=0; i getAnswer() { List answers = new ArrayList<>(); + Random random = new Random(); while(answers.size() < 3){ - int num = getRandomNumber(); + int num = random.nextInt(9)+1; answers = addUniqueNumber(num, answers); } @@ -46,16 +38,11 @@ public List getAnswer() { } public List addUniqueNumber(int num, List answers){ - if (answers.stream().anyMatch(n -> n == num)){ - return answers; + if (!answers.contains(num)){ + answers.add(num); } - answers.add(num); return answers; } - public int getRandomNumber(){ - Random random = new Random(); - return random.nextInt(9)+1; - } } diff --git a/src/main/java/study/BaseballGame.java b/src/main/java/study/BaseballGame.java new file mode 100644 index 0000000..b917f72 --- /dev/null +++ b/src/main/java/study/BaseballGame.java @@ -0,0 +1,37 @@ +package study; + +import java.util.List; + +public class BaseballGame { + private final BaseballScanner scanner; + private final Baseball baseball; + + public BaseballGame(BaseballScanner scanner, Baseball baseball){ + this.scanner = scanner; + this.baseball = baseball; + } + + public String playGame(){ + boolean collect = false; + + while(!collect){ + List userInput = scanner.askNumbers(); + BaseballScore score = baseball.play(userInput); + + System.out.println(score.printScore()); + collect = score.isThreeStrike(); + } + + return "3개의 숫자를 모두 맞히셨습니다! 게임 종료"; + } + + public boolean isReplay(){ + int input = scanner.askReplay(); + return input != 2; + } + + public String endGame(){ + return "게임이 종료되었습니다."; + } + +} diff --git a/src/main/java/study/BaseballScanner.java b/src/main/java/study/BaseballScanner.java index 7b13f1a..b293644 100644 --- a/src/main/java/study/BaseballScanner.java +++ b/src/main/java/study/BaseballScanner.java @@ -9,7 +9,7 @@ public BaseballScanner(){ this.scanner = new Scanner(System.in); } - public List execute() { + public List askNumbers() { System.out.println("숫자를 입력해 주세요 : "); String input; @@ -22,25 +22,27 @@ public List execute() { return convertList(input); } - public String checkLength(String input){ - input = input.replace(" ", ""); + public int askReplay(){ + System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); + int input; - if (input.length() != 3){ - throw new NoSuchElementException(ErrorCode.INVALID_INPUT.getMessage()); + try{ + input = validateOneOrTwo(scanner.nextInt()); + } catch (NoSuchElementException ex){ + throw new NoSuchElementException(ErrorCode.INVALID_INPUT_REPLAY.getMessage()); } + return input; + } - public List convertList(String input){ - List numbers = new ArrayList<>(); - String[] inputArr = input.split(""); + public String checkLength(String input){ + input = input.replace(" ", ""); - for (String str: inputArr){ - int number = parseStringToInt(str); - numbers.add(number); + if (input.length() != 3){ + throw new NoSuchElementException(ErrorCode.INVALID_INPUT.getMessage()); } - - return numbers; + return input; } public int parseStringToInt(String str){ @@ -55,26 +57,29 @@ public int parseStringToInt(String str){ return result; } - public boolean isReplay(){ - System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); - int input; - try{ - input = scanner.nextInt(); - } catch (NoSuchElementException ex){ - throw new NoSuchElementException(ErrorCode.INVALID_INPUT_REPLAY.getMessage()); - } + public List convertList(String input){ + List numbers = new ArrayList<>(); + String[] inputArr = input.split(""); - if (input == 2){ - return false; - } else if(input != 1){ - throw new NoSuchElementException(ErrorCode.INVALID_INPUT_REPLAY.getMessage()); + for (String str: inputArr){ + int number = parseStringToInt(str); + numbers.add(number); } - return true; + return numbers; + } + + public int validateOneOrTwo(int input){ + if (input == 1 || input == 2){ + return input; + } + + throw new NoSuchElementException(ErrorCode.INVALID_INPUT_REPLAY.getMessage()); } + public void close(){ scanner.close(); } diff --git a/src/main/java/study/BaseballScore.java b/src/main/java/study/BaseballScore.java index 935856a..1496acd 100644 --- a/src/main/java/study/BaseballScore.java +++ b/src/main/java/study/BaseballScore.java @@ -19,7 +19,7 @@ public BaseballScore(int strikeCnt, int ballCnt, int nothingCnt) { } - public void checkScore(int index, int i){ + public void addScore(int index, int i){ if (index == -1){ nothingCnt++; } else if (index == i){ From 4faa5e51d7d92acf724b6c8e661d6d9a926ae50c Mon Sep 17 00:00:00 2001 From: Heesoo Date: Fri, 18 Oct 2024 11:57:10 +0900 Subject: [PATCH 3/5] =?UTF-8?q?[test]=20BaseballGame=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/study/BaseballGameTest.java | 73 ++++++++ src/test/java/study/BaseballScannerTest.java | 171 +++++++++++-------- src/test/java/study/BaseballScoreTest.java | 12 +- src/test/java/study/BaseballTest.java | 83 +++++---- 4 files changed, 229 insertions(+), 110 deletions(-) create mode 100644 src/test/java/study/BaseballGameTest.java diff --git a/src/test/java/study/BaseballGameTest.java b/src/test/java/study/BaseballGameTest.java new file mode 100644 index 0000000..524f359 --- /dev/null +++ b/src/test/java/study/BaseballGameTest.java @@ -0,0 +1,73 @@ +package study; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class BaseballGameTest { + + @Test + @DisplayName("playGame(): 3 스트라이크") + void playGame3Strike(){ + // given + List userInput = Arrays.asList(1, 2, 3); + + BaseballScanner scanner = mock(BaseballScanner.class); + Baseball baseball = mock(Baseball.class); + BaseballGame game = new BaseballGame(scanner, baseball); + + when(scanner.askNumbers()).thenReturn(userInput); + when(baseball.play(userInput)).thenReturn(new BaseballScore(3, 0, 0)); + + + // when + String response = game.playGame(); + + // then + assertEquals(response, "3개의 숫자를 모두 맞히셨습니다! 게임 종료"); + } + + + @Test + @DisplayName("isReplayScanner(): 재게임 여부 1 입력으로 true 반환") + void isReplayInput1(){ + // given + BaseballScanner scanner = mock(BaseballScanner.class); + Baseball baseball = mock(Baseball.class); + BaseballGame game = new BaseballGame(scanner, baseball); + + when(scanner.askReplay()).thenReturn(1); + + // when + boolean response = game.isReplay(); + + // then + assertTrue(response); + } + + @Test + @DisplayName("isReplayScanner(): 재게임 여부 2 입력으로 false 반환") + void isReplayInput2(){ + // given + BaseballScanner scanner = mock(BaseballScanner.class); + Baseball baseball = mock(Baseball.class); + BaseballGame game = new BaseballGame(scanner, baseball); + + when(scanner.askReplay()).thenReturn(2); + + // when + boolean response = game.isReplay(); + + // then + assertFalse(response); + } + + + +} diff --git a/src/test/java/study/BaseballScannerTest.java b/src/test/java/study/BaseballScannerTest.java index c15da17..9f42793 100644 --- a/src/test/java/study/BaseballScannerTest.java +++ b/src/test/java/study/BaseballScannerTest.java @@ -8,13 +8,18 @@ import java.util.List; import java.util.NoSuchElementException; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; public class BaseballScannerTest { + // + // System.in 에 대한 입력 스트림이 설정 되기 전에 scanner 가 초기화 되면 scanner 가 읽을 데이터가 없는 상태가 될 수 있음 + // 특히 @BeforeEach 사용 시 System.in 설정 전에 먼저 객체가 생성되서 입력값이 없어서 예외 발생함 + @Test - @DisplayName("execute(): 사용자 입력 성공") - void execute(){ + @DisplayName("askNumbers(): 사용자 입력 성공") + void askNumbers(){ // given String input = "456"; InputStream in = new ByteArrayInputStream(input.getBytes()); @@ -22,7 +27,7 @@ void execute(){ // when BaseballScanner scanner = new BaseballScanner(); - List response = scanner.execute(); + List response = scanner.askNumbers(); // then assertEquals(response.get(0), 4); @@ -31,8 +36,8 @@ void execute(){ } @Test - @DisplayName("execute(): 공백이 포함된 숫자 입력으로 예외 발생") - void executeContainEmpty_exception(){ + @DisplayName("askNumbers(): 공백이 포함된 숫자 입력으로 예외 발생") + void askNumbersContainEmpty_exception(){ // given String input = "4 6"; InputStream in = new ByteArrayInputStream(input.getBytes()); @@ -40,15 +45,15 @@ void executeContainEmpty_exception(){ // when BaseballScanner scanner = new BaseballScanner(); - NoSuchElementException fail = assertThrows(NoSuchElementException.class, scanner::execute); + NoSuchElementException fail = assertThrows(NoSuchElementException.class, scanner::askNumbers); // then assertEquals(fail.getMessage(), ErrorCode.INVALID_INPUT.getMessage()); } @Test - @DisplayName("execute(): 숫자 3자리 이상을 입력으로 예외 발생") - void executeInputOverSizeThree_exception(){ + @DisplayName("askNumbers(): 숫자 3자리 이상을 입력으로 예외 발생") + void askNumbersInputOverSizeThree_exception(){ // given String input = "123456"; InputStream in = new ByteArrayInputStream(input.getBytes()); @@ -56,15 +61,15 @@ void executeInputOverSizeThree_exception(){ // when BaseballScanner scanner = new BaseballScanner(); - NoSuchElementException fail = assertThrows(NoSuchElementException.class, scanner::execute); + NoSuchElementException fail = assertThrows(NoSuchElementException.class, scanner::askNumbers); // then assertEquals(fail.getMessage(), ErrorCode.INVALID_INPUT.getMessage()); } @Test - @DisplayName("execute(): 문자열 입력으로 예외 발생") - void executeInputString_exception(){ + @DisplayName("askNumbers(): 문자열 입력으로 예외 발생") + void askNumbersInputString_exception(){ // given String input = "문자열"; InputStream in = new ByteArrayInputStream(input.getBytes()); @@ -72,15 +77,15 @@ void executeInputString_exception(){ // when BaseballScanner scanner = new BaseballScanner(); - NumberFormatException fail = assertThrows(NumberFormatException.class, scanner::execute); + NumberFormatException fail = assertThrows(NumberFormatException.class, scanner::askNumbers); // then assertEquals(fail.getMessage(), ErrorCode.INVALID_INPUT_NUMBER.getMessage()); } @Test - @DisplayName("execute(): 공백 입력으로 예외 발생") - void executeInputEmpty_exception(){ + @DisplayName("askNumbers(): 공백 입력으로 예외 발생") + void askNumbersInputEmpty_exception(){ // given String input = " "; InputStream in = new ByteArrayInputStream(input.getBytes()); @@ -88,12 +93,78 @@ void executeInputEmpty_exception(){ // when BaseballScanner scanner = new BaseballScanner(); - NoSuchElementException fail = assertThrows(NoSuchElementException.class, scanner::execute); + NoSuchElementException fail = assertThrows(NoSuchElementException.class, scanner::askNumbers); // then assertEquals(fail.getMessage(), ErrorCode.INVALID_INPUT.getMessage()); } + + @Test + @DisplayName("askReplay(): 재게임 입력 성공") + void askReplay(){ + // given + String input = "1"; + InputStream in = new ByteArrayInputStream(input.getBytes()); + System.setIn(in); + + // when + BaseballScanner scanner = new BaseballScanner(); + int response = scanner.askReplay(); + + // then + assertEquals(response, Integer.parseInt(input)); + } + + @Test + @DisplayName("askReplay(): 문자열 입력으로 예외 발생") + void askReplayInputString_exception(){ + // given + String input = "문자열"; + InputStream in = new ByteArrayInputStream(input.getBytes()); + System.setIn(in); + + // when + BaseballScanner scanner = new BaseballScanner(); + NoSuchElementException fail = assertThrows(NoSuchElementException.class, scanner::askReplay); + + // then + assertEquals(fail.getMessage(), ErrorCode.INVALID_INPUT_REPLAY.getMessage()); + } + + + @Test + @DisplayName("askReplay(): 공백 입력으로 예외 발생") + void askReplayInputEmpty_exception(){ + // given + String input = " "; + InputStream in = new ByteArrayInputStream(input.getBytes()); + System.setIn(in); + + // when + BaseballScanner scanner = new BaseballScanner(); + NoSuchElementException fail = assertThrows(NoSuchElementException.class, scanner::askReplay); + + // then + assertEquals(fail.getMessage(), ErrorCode.INVALID_INPUT_REPLAY.getMessage()); + } + + @Test + @DisplayName("askReplay(): 숫자 1 또는 2 이외의 숫자 입력으로 예외 발생") + void askReplayInputTwoNumber_exception(){ + String input = "34"; + InputStream in = new ByteArrayInputStream(input.getBytes()); + System.setIn(in); + + // when + BaseballScanner scanner = new BaseballScanner(); + NoSuchElementException fail = assertThrows(NoSuchElementException.class, scanner::askReplay); + + // then + assertEquals(fail.getMessage(), ErrorCode.INVALID_INPUT_REPLAY.getMessage()); + } + + @Test @DisplayName("checkLength(): 숫자 3자리 검증 성공") void checkLength(){ @@ -166,6 +237,8 @@ void convertList(){ assertEquals(response.get(2), 3); } + + @Test @DisplayName("convertList(): 문자열 숫자 리스트 변환 실패로 예외 발생") void convertListInputString_exception(){ @@ -181,83 +254,47 @@ void convertListInputString_exception(){ } @Test - @DisplayName("isReplayScanner(): 재게임 여부 1 입력으로 true 반환") - void isReplayInput1(){ + @DisplayName("validateOneOrTwo(): 숫자 1 입력으로 성공") + void validateOneOrTwoInput1(){ // given - String input = "1"; - InputStream in = new ByteArrayInputStream(input.getBytes()); - System.setIn(in); - - // when - BaseballScanner scanner = new BaseballScanner(); - boolean response = scanner.isReplay(); - - // then - assertTrue(response); - } - - @Test - @DisplayName("isReplayScanner(): 재게임 여부 2 입력으로 false 반환") - void isReplayInput2(){ - // given - String input = "2"; - InputStream in = new ByteArrayInputStream(input.getBytes()); - System.setIn(in); + int input = 1; // when BaseballScanner scanner = new BaseballScanner(); - boolean response = scanner.isReplay(); + int response = scanner.validateOneOrTwo(input); // then - assertFalse(response); + assertEquals(response, input); } @Test - @DisplayName("isReplayScanner(): 문자열 입력으로 예외 발생") - void isReplayInputString_exception(){ + @DisplayName("validateOneOrTwo(): 숫자 2 입력으로 성공") + void validateOneOrTwoInput2(){ // given - String input = "문자열"; - InputStream in = new ByteArrayInputStream(input.getBytes()); - System.setIn(in); + int input = 2; // when BaseballScanner scanner = new BaseballScanner(); - NoSuchElementException fail = assertThrows(NoSuchElementException.class, scanner::isReplay); + int response = scanner.validateOneOrTwo(input); // then - assertEquals(fail.getMessage(), ErrorCode.INVALID_INPUT_REPLAY.getMessage()); + assertEquals(response, input); } @Test - @DisplayName("isReplayScanner(): 숫자 2자리 입력") - void isReplayInputTwoNumber_exception(){ - String input = "34"; - InputStream in = new ByteArrayInputStream(input.getBytes()); - System.setIn(in); - - // when - BaseballScanner scanner = new BaseballScanner(); - NoSuchElementException fail = assertThrows(NoSuchElementException.class, scanner::isReplay); - - // then - assertEquals(fail.getMessage(), ErrorCode.INVALID_INPUT_REPLAY.getMessage()); - } - - @Test - @DisplayName("execute(): 공백 입력으로 예외 발생") - void isReplayInputEmpty_exception(){ + @DisplayName("validateOneOrTwo(): 1이나 2가 아닌 숫자 입력으로 예외 발생") + void validateOneOrTwoInputEither1Or2_exception(){ // given - String input = " "; - InputStream in = new ByteArrayInputStream(input.getBytes()); - System.setIn(in); + int input = 222; // when BaseballScanner scanner = new BaseballScanner(); - NoSuchElementException fail = assertThrows(NoSuchElementException.class, scanner::isReplay); + NoSuchElementException fail = assertThrows(NoSuchElementException.class, () -> scanner.validateOneOrTwo(input)); // then assertEquals(fail.getMessage(), ErrorCode.INVALID_INPUT_REPLAY.getMessage()); } + } diff --git a/src/test/java/study/BaseballScoreTest.java b/src/test/java/study/BaseballScoreTest.java index 03e4986..9ca3d25 100644 --- a/src/test/java/study/BaseballScoreTest.java +++ b/src/test/java/study/BaseballScoreTest.java @@ -9,14 +9,14 @@ public class BaseballScoreTest { @Test @DisplayName("checkScore(): 스트라이크인 경우") - void checkScore_strike(){ + void addScore_strike(){ // given int index = 1; int i = 1; // when BaseballScore score = new BaseballScore(); - score.checkScore(index, i); + score.addScore(index, i); // then assertEquals(score.getBallCnt(), 0); @@ -26,14 +26,14 @@ void checkScore_strike(){ @Test @DisplayName("checkScore(): 볼인 경우") - void checkScore_ball(){ + void addScore_ball(){ // given int index = 2; int i = 1; // when BaseballScore score = new BaseballScore(); - score.checkScore(index, i); + score.addScore(index, i); // then assertEquals(score.getBallCnt(), 1); @@ -43,14 +43,14 @@ void checkScore_ball(){ @Test @DisplayName("checkScore(): ball, strike 둘 다 아닌 경우") - void checkScore_nothing(){ + void addScore_nothing(){ // given int index = -1; int i = 1; // when BaseballScore score = new BaseballScore(); - score.checkScore(index, i); + score.addScore(index, i); // then assertEquals(score.getBallCnt(), 0); diff --git a/src/test/java/study/BaseballTest.java b/src/test/java/study/BaseballTest.java index 661d1ae..900f543 100644 --- a/src/test/java/study/BaseballTest.java +++ b/src/test/java/study/BaseballTest.java @@ -3,6 +3,7 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.Test; +import org.mockito.Mock; import org.mockito.Mockito; import java.io.ByteArrayInputStream; @@ -17,59 +18,80 @@ public class BaseballTest { @Test - @DisplayName("3스트라이크") + @DisplayName("play(): 3 스트라이크") void play3Strike(){ // given - Baseball baseball = Mockito.spy(new Baseball()); + List inputList = Arrays.asList(3, 5, 6); + List answerList = Arrays.asList(3, 5, 6); - List answerList = Arrays.asList(1, 2, 3); - when(baseball.getAnswer()).thenReturn(answerList); + // when + Baseball baseball = new Baseball(answerList); + BaseballScore response = baseball.play(inputList); - String input = "123"; - InputStream in = new ByteArrayInputStream(input.getBytes()); - System.setIn(in); + // then + assertEquals(response.getStrikeCnt(), 3); + assertEquals(response.getBallCnt(), 0); + assertEquals(response.getNothingCnt(), 0); + } + + @Test + @DisplayName("play(): 1 스트라이크 2볼") + void play1Strike2Ball(){ + // given + List inputList = Arrays.asList(3, 5, 6); + List answerList = Arrays.asList(3, 6, 5); // when - String response = baseball.play(); + Baseball baseball = new Baseball(answerList); + BaseballScore response = baseball.play(inputList); // then - assertEquals(response, "3개의 숫자를 모두 맞히셨습니다! 게임 종료"); - + assertEquals(response.getStrikeCnt(), 1); + assertEquals(response.getBallCnt(), 2); + assertEquals(response.getNothingCnt(), 0); } - @Test - @DisplayName("checkAnswer(): 3 스트라이크") - void checkAnswer_3Strike(){ + @DisplayName("play(): 3볼") + void play3Ball(){ // given List inputList = Arrays.asList(3, 5, 6); - List answerList = Arrays.asList(3, 5, 6); + List answerList = Arrays.asList(5, 6, 3); + // when - Baseball baseball = new Baseball(); - boolean response = baseball.checkAnswer(inputList, answerList); + Baseball baseball = new Baseball(answerList); + BaseballScore response = baseball.play(inputList); // then - assertTrue(response); + assertEquals(response.getStrikeCnt(), 0); + assertEquals(response.getBallCnt(), 3); + assertEquals(response.getNothingCnt(), 0); + } @Test - @DisplayName("checkAnswer(): 3 스트라이크 아닌 경우") - void checkAnswerNot3Strike(){ + @DisplayName("play(): 낫싱") + void playNothing(){ // given List inputList = Arrays.asList(3, 5, 6); - List answerList = Arrays.asList(3, 8, 1); + List answerList = Arrays.asList(7, 8, 9); + // when - Baseball baseball = new Baseball(); - boolean response = baseball.checkAnswer(inputList, answerList); + Baseball baseball = new Baseball(answerList); + BaseballScore response = baseball.play(inputList); + // then - assertFalse(response); + assertEquals(response.getStrikeCnt(), 0); + assertEquals(response.getBallCnt(), 0); + assertEquals(response.getNothingCnt(), 3); + } - @Test + @RepeatedTest(10) // 10번 반복 테스트 @DisplayName("getAnswer() 성공: 랜덤 3자리 수 생성") void getAnswer(){ // given @@ -126,17 +148,4 @@ void addDuplicatedNumber(){ assertEquals(response.get(1), answers.get(1)); } - - @RepeatedTest(10) // 10번 반복 테스트 - @DisplayName("getRandomNumber() 성공: 1부터 9까지 랜덤 수 생성 성공") - void getRandomNumber(){ - // given - // when - Baseball baseball = new Baseball(); - int response = baseball.getRandomNumber(); - - // then - System.out.println("response : " + response); - assertTrue(response > 0 && response <= 9); - } } From 1ed940d5c26f75959d3d7b3f44be7e991f777580 Mon Sep 17 00:00:00 2001 From: Heesoo Date: Fri, 18 Oct 2024 23:21:11 +0900 Subject: [PATCH 4/5] =?UTF-8?q?[test]=20DisplayName=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/study/BaseballGameTest.java | 1 - src/test/java/study/BaseballScoreTest.java | 22 +++++++++++----------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/test/java/study/BaseballGameTest.java b/src/test/java/study/BaseballGameTest.java index 524f359..fb77889 100644 --- a/src/test/java/study/BaseballGameTest.java +++ b/src/test/java/study/BaseballGameTest.java @@ -33,7 +33,6 @@ void playGame3Strike(){ assertEquals(response, "3개의 숫자를 모두 맞히셨습니다! 게임 종료"); } - @Test @DisplayName("isReplayScanner(): 재게임 여부 1 입력으로 true 반환") void isReplayInput1(){ diff --git a/src/test/java/study/BaseballScoreTest.java b/src/test/java/study/BaseballScoreTest.java index 9ca3d25..f1f28a3 100644 --- a/src/test/java/study/BaseballScoreTest.java +++ b/src/test/java/study/BaseballScoreTest.java @@ -8,8 +8,8 @@ public class BaseballScoreTest { @Test - @DisplayName("checkScore(): 스트라이크인 경우") - void addScore_strike(){ + @DisplayName("addScore(): 스트라이크인 경우") + void addScoreStrike(){ // given int index = 1; int i = 1; @@ -25,8 +25,8 @@ void addScore_strike(){ } @Test - @DisplayName("checkScore(): 볼인 경우") - void addScore_ball(){ + @DisplayName("addScore(): 볼인 경우") + void addScoreBall(){ // given int index = 2; int i = 1; @@ -42,8 +42,8 @@ void addScore_ball(){ } @Test - @DisplayName("checkScore(): ball, strike 둘 다 아닌 경우") - void addScore_nothing(){ + @DisplayName("addScore(): ball, strike 둘 다 아닌 경우") + void addScoreNothing(){ // given int index = -1; int i = 1; @@ -85,7 +85,7 @@ void isNotThreeStrike(){ } @Test - @DisplayName("볼, 스트라이크 점수 출력") + @DisplayName("printScore(): 볼, 스트라이크 점수 출력") void scorePrintBallAndStrike(){ // given int ballCnt = 1; @@ -103,7 +103,7 @@ void scorePrintBallAndStrike(){ } @Test - @DisplayName("3 스트라이크 점수 출력") + @DisplayName("printScore(): 3 스트라이크 점수 출력") void scorePrint3Strike(){ // given int ballCnt = 0; @@ -121,7 +121,7 @@ void scorePrint3Strike(){ } @Test - @DisplayName("스트라이크 점수 출력") + @DisplayName("printScore(): 스트라이크 점수 출력") void scorePrintStrike(){ // given int ballCnt = 0; @@ -139,7 +139,7 @@ void scorePrintStrike(){ } @Test - @DisplayName("볼 점수 출력") + @DisplayName("printScore(): 볼 점수 출력") void scorePrintBall(){ // given int ballCnt = 2; @@ -158,7 +158,7 @@ void scorePrintBall(){ @Test - @DisplayName("낫싱 점수 출력") + @DisplayName("printScore(): 낫싱 점수 출력") void scorePrintNothing(){ // given int ballCnt = 0; From c6f43ba971dc8e3372dc9afa1634c0690a41ebcf Mon Sep 17 00:00:00 2001 From: Heesoo Date: Sat, 19 Oct 2024 00:41:04 +0900 Subject: [PATCH 5/5] =?UTF-8?q?[feat]=20=EC=97=90=EB=9F=AC,=20=EC=84=B1?= =?UTF-8?q?=EA=B3=B5=20=EB=A9=94=EC=8B=9C=EC=A7=80=20=ED=81=B4=EB=9E=98?= =?UTF-8?q?=EC=8A=A4=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/study/BaseballGame.java | 6 +++-- src/main/java/study/BaseballScanner.java | 12 +++++---- src/main/java/study/ErrorCode.java | 25 ------------------- .../java/study/messages/ErrorMessages.java | 7 ++++++ .../java/study/messages/SuccessMessages.java | 6 +++++ src/test/java/study/BaseballGameTest.java | 3 ++- src/test/java/study/BaseballScannerTest.java | 23 +++++++++-------- 7 files changed, 38 insertions(+), 44 deletions(-) delete mode 100644 src/main/java/study/ErrorCode.java create mode 100644 src/main/java/study/messages/ErrorMessages.java create mode 100644 src/main/java/study/messages/SuccessMessages.java diff --git a/src/main/java/study/BaseballGame.java b/src/main/java/study/BaseballGame.java index b917f72..72200e4 100644 --- a/src/main/java/study/BaseballGame.java +++ b/src/main/java/study/BaseballGame.java @@ -1,5 +1,7 @@ package study; +import study.messages.SuccessMessages; + import java.util.List; public class BaseballGame { @@ -22,7 +24,7 @@ public String playGame(){ collect = score.isThreeStrike(); } - return "3개의 숫자를 모두 맞히셨습니다! 게임 종료"; + return SuccessMessages.CORRECT_GAME; } public boolean isReplay(){ @@ -31,7 +33,7 @@ public boolean isReplay(){ } public String endGame(){ - return "게임이 종료되었습니다."; + return SuccessMessages.END_GAME; } } diff --git a/src/main/java/study/BaseballScanner.java b/src/main/java/study/BaseballScanner.java index b293644..49ee2af 100644 --- a/src/main/java/study/BaseballScanner.java +++ b/src/main/java/study/BaseballScanner.java @@ -1,5 +1,7 @@ package study; +import study.messages.ErrorMessages; + import java.util.*; public class BaseballScanner { @@ -16,7 +18,7 @@ public List askNumbers() { try{ input = checkLength(scanner.next()); } catch (NoSuchElementException ex){ - throw new NoSuchElementException(ErrorCode.INVALID_INPUT.getMessage()); + throw new NoSuchElementException(ErrorMessages.INVALID_INPUT); } return convertList(input); @@ -29,7 +31,7 @@ public int askReplay(){ try{ input = validateOneOrTwo(scanner.nextInt()); } catch (NoSuchElementException ex){ - throw new NoSuchElementException(ErrorCode.INVALID_INPUT_REPLAY.getMessage()); + throw new NoSuchElementException(ErrorMessages.INVALID_INPUT_REPLAY); } return input; @@ -40,7 +42,7 @@ public String checkLength(String input){ input = input.replace(" ", ""); if (input.length() != 3){ - throw new NoSuchElementException(ErrorCode.INVALID_INPUT.getMessage()); + throw new NoSuchElementException(ErrorMessages.INVALID_INPUT); } return input; } @@ -51,7 +53,7 @@ public int parseStringToInt(String str){ try{ result = Integer.parseInt(str); } catch (NumberFormatException ex){ - throw new NumberFormatException(ErrorCode.INVALID_INPUT_NUMBER.getMessage()); + throw new NumberFormatException(ErrorMessages.INVALID_INPUT_NUMBER); } return result; @@ -76,7 +78,7 @@ public int validateOneOrTwo(int input){ return input; } - throw new NoSuchElementException(ErrorCode.INVALID_INPUT_REPLAY.getMessage()); + throw new NoSuchElementException(ErrorMessages.INVALID_INPUT_REPLAY); } diff --git a/src/main/java/study/ErrorCode.java b/src/main/java/study/ErrorCode.java deleted file mode 100644 index e0ead83..0000000 --- a/src/main/java/study/ErrorCode.java +++ /dev/null @@ -1,25 +0,0 @@ -package study; - -public enum ErrorCode { - - INVALID_INPUT(1, "3자리 숫자를 입력해주세요."), - INVALID_INPUT_NUMBER(2, "숫자만 입력 가능합니다."), - INVALID_INPUT_REPLAY(3, "1 또는 2 숫자만 입력 가능합니다."); - - - private final int id; - private final String message; - - ErrorCode(int id, String message) { - this.id = id; - this.message = message; - } - - public int getId() { - return id; - } - - public String getMessage() { - return message; - } -} diff --git a/src/main/java/study/messages/ErrorMessages.java b/src/main/java/study/messages/ErrorMessages.java new file mode 100644 index 0000000..e38c0e2 --- /dev/null +++ b/src/main/java/study/messages/ErrorMessages.java @@ -0,0 +1,7 @@ +package study.messages; + +public class ErrorMessages { + public static final String INVALID_INPUT = "3자리 숫자를 입력해주세요."; + public static final String INVALID_INPUT_NUMBER = "숫자만 입력 가능합니다."; + public static final String INVALID_INPUT_REPLAY = "1 또는 2 숫자만 입력 가능합니다."; +} diff --git a/src/main/java/study/messages/SuccessMessages.java b/src/main/java/study/messages/SuccessMessages.java new file mode 100644 index 0000000..d7e1918 --- /dev/null +++ b/src/main/java/study/messages/SuccessMessages.java @@ -0,0 +1,6 @@ +package study.messages; + +public class SuccessMessages { + public static final String CORRECT_GAME = "3개의 숫자를 모두 맞히셨습니다! 게임 종료"; + public static final String END_GAME = "게임이 종료되었습니다."; +} diff --git a/src/test/java/study/BaseballGameTest.java b/src/test/java/study/BaseballGameTest.java index fb77889..9ed50bf 100644 --- a/src/test/java/study/BaseballGameTest.java +++ b/src/test/java/study/BaseballGameTest.java @@ -2,6 +2,7 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import study.messages.SuccessMessages; import java.util.Arrays; import java.util.List; @@ -30,7 +31,7 @@ void playGame3Strike(){ String response = game.playGame(); // then - assertEquals(response, "3개의 숫자를 모두 맞히셨습니다! 게임 종료"); + assertEquals(response, SuccessMessages.CORRECT_GAME); } @Test diff --git a/src/test/java/study/BaseballScannerTest.java b/src/test/java/study/BaseballScannerTest.java index 9f42793..259b3a5 100644 --- a/src/test/java/study/BaseballScannerTest.java +++ b/src/test/java/study/BaseballScannerTest.java @@ -2,6 +2,7 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import study.messages.ErrorMessages; import java.io.ByteArrayInputStream; import java.io.InputStream; @@ -48,7 +49,7 @@ void askNumbersContainEmpty_exception(){ NoSuchElementException fail = assertThrows(NoSuchElementException.class, scanner::askNumbers); // then - assertEquals(fail.getMessage(), ErrorCode.INVALID_INPUT.getMessage()); + assertEquals(fail.getMessage(), ErrorMessages.INVALID_INPUT); } @Test @@ -64,7 +65,7 @@ void askNumbersInputOverSizeThree_exception(){ NoSuchElementException fail = assertThrows(NoSuchElementException.class, scanner::askNumbers); // then - assertEquals(fail.getMessage(), ErrorCode.INVALID_INPUT.getMessage()); + assertEquals(fail.getMessage(), ErrorMessages.INVALID_INPUT); } @Test @@ -80,7 +81,7 @@ void askNumbersInputString_exception(){ NumberFormatException fail = assertThrows(NumberFormatException.class, scanner::askNumbers); // then - assertEquals(fail.getMessage(), ErrorCode.INVALID_INPUT_NUMBER.getMessage()); + assertEquals(fail.getMessage(), ErrorMessages.INVALID_INPUT_NUMBER); } @Test @@ -96,7 +97,7 @@ void askNumbersInputEmpty_exception(){ NoSuchElementException fail = assertThrows(NoSuchElementException.class, scanner::askNumbers); // then - assertEquals(fail.getMessage(), ErrorCode.INVALID_INPUT.getMessage()); + assertEquals(fail.getMessage(), ErrorMessages.INVALID_INPUT); } @@ -129,7 +130,7 @@ void askReplayInputString_exception(){ NoSuchElementException fail = assertThrows(NoSuchElementException.class, scanner::askReplay); // then - assertEquals(fail.getMessage(), ErrorCode.INVALID_INPUT_REPLAY.getMessage()); + assertEquals(fail.getMessage(), ErrorMessages.INVALID_INPUT_REPLAY); } @@ -146,7 +147,7 @@ void askReplayInputEmpty_exception(){ NoSuchElementException fail = assertThrows(NoSuchElementException.class, scanner::askReplay); // then - assertEquals(fail.getMessage(), ErrorCode.INVALID_INPUT_REPLAY.getMessage()); + assertEquals(fail.getMessage(), ErrorMessages.INVALID_INPUT_REPLAY); } @Test @@ -161,7 +162,7 @@ void askReplayInputTwoNumber_exception(){ NoSuchElementException fail = assertThrows(NoSuchElementException.class, scanner::askReplay); // then - assertEquals(fail.getMessage(), ErrorCode.INVALID_INPUT_REPLAY.getMessage()); + assertEquals(fail.getMessage(), ErrorMessages.INVALID_INPUT_REPLAY); } @@ -190,7 +191,7 @@ void checkLengthInputOverSizeThree_exception(){ NoSuchElementException fail = assertThrows(NoSuchElementException.class, () -> scanner.checkLength(input)); // then - assertEquals(fail.getMessage(), ErrorCode.INVALID_INPUT.getMessage()); + assertEquals(fail.getMessage(), ErrorMessages.INVALID_INPUT); } @Test @@ -218,7 +219,7 @@ void parseInputString_exception(){ NumberFormatException fail = assertThrows(NumberFormatException.class, () -> scanner.parseStringToInt(input)); // then - assertEquals(fail.getMessage(), ErrorCode.INVALID_INPUT_NUMBER.getMessage()); + assertEquals(fail.getMessage(), ErrorMessages.INVALID_INPUT_NUMBER); } @Test @@ -250,7 +251,7 @@ void convertListInputString_exception(){ NumberFormatException fail = assertThrows(NumberFormatException.class, () -> scanner.convertList(input)); // then - assertEquals(fail.getMessage(), ErrorCode.INVALID_INPUT_NUMBER.getMessage()); + assertEquals(fail.getMessage(), ErrorMessages.INVALID_INPUT_NUMBER); } @Test @@ -293,7 +294,7 @@ void validateOneOrTwoInputEither1Or2_exception(){ NoSuchElementException fail = assertThrows(NoSuchElementException.class, () -> scanner.validateOneOrTwo(input)); // then - assertEquals(fail.getMessage(), ErrorCode.INVALID_INPUT_REPLAY.getMessage()); + assertEquals(fail.getMessage(), ErrorMessages.INVALID_INPUT_REPLAY); }