From f46cf53f220042481ad217db515813fcd6569db6 Mon Sep 17 00:00:00 2001 From: Heesoo Date: Thu, 24 Oct 2024 11:22:32 +0900 Subject: [PATCH 1/7] =?UTF-8?q?[feat]=20=EC=88=AB=EC=9E=90=EC=95=BC?= =?UTF-8?q?=EA=B5=AC=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 --- src/main/java/BaseballApplication.java | 11 ++ src/main/java/study/Baseball.java | 45 ++++++ src/main/java/study/BaseballGame.java | 16 ++ src/main/java/study/BaseballGenerator.java | 31 ++++ src/main/java/study/BaseballScore.java | 51 ++++++ src/main/java/study/BaseballUtils.java | 57 +++++++ .../java/study/message/ErrorMessages.java | 6 + src/test/java/study/BaseballGameTest.java | 23 +++ .../java/study/BaseballGeneratorTest.java | 80 ++++++++++ src/test/java/study/BaseballScoreTest.java | 133 ++++++++++++++++ src/test/java/study/BaseballTest.java | 79 ++++++++++ src/test/java/study/BaseballUtilsTest.java | 149 ++++++++++++++++++ src/test/java/study/StringTest.java | 13 -- 13 files changed, 681 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/BaseballGame.java create mode 100644 src/main/java/study/BaseballGenerator.java create mode 100644 src/main/java/study/BaseballScore.java create mode 100644 src/main/java/study/BaseballUtils.java create mode 100644 src/main/java/study/message/ErrorMessages.java create mode 100644 src/test/java/study/BaseballGameTest.java create mode 100644 src/test/java/study/BaseballGeneratorTest.java create mode 100644 src/test/java/study/BaseballScoreTest.java create mode 100644 src/test/java/study/BaseballTest.java create mode 100644 src/test/java/study/BaseballUtilsTest.java delete mode 100644 src/test/java/study/StringTest.java diff --git a/src/main/java/BaseballApplication.java b/src/main/java/BaseballApplication.java new file mode 100644 index 0000000..3b0d8ae --- /dev/null +++ b/src/main/java/BaseballApplication.java @@ -0,0 +1,11 @@ +import study.BaseballGame; +import study.BaseballScore; + +public class BaseballApplication { + public static void main(String[] args) { + BaseballGame baseballGame = new BaseballGame(); + BaseballScore score = baseballGame.playGame("123"); + + System.out.println(score.printScore()); + } +} diff --git a/src/main/java/study/Baseball.java b/src/main/java/study/Baseball.java new file mode 100644 index 0000000..a047fb1 --- /dev/null +++ b/src/main/java/study/Baseball.java @@ -0,0 +1,45 @@ +package study; + +import java.util.*; + +public class Baseball { + + private static final int PLAY_ROUND = 3; + private final List answer; + private final BaseballScore score; + + public Baseball(BaseballScore score, List answer){ + this.score = score; + this.answer = answer; + } + + public BaseballScore play(List userInput) { + System.out.println(answer.toString()); + for(int i=0; i userInput = generator.createUserInputList(number); + List answer = generator.createAnswer(); + + Baseball baseball = new Baseball(new BaseballScore(), answer); + return baseball.play(userInput); + + } +} diff --git a/src/main/java/study/BaseballGenerator.java b/src/main/java/study/BaseballGenerator.java new file mode 100644 index 0000000..a02826e --- /dev/null +++ b/src/main/java/study/BaseballGenerator.java @@ -0,0 +1,31 @@ +package study; + +import study.message.ErrorMessages; + +import java.util.ArrayList; +import java.util.List; + +public class BaseballGenerator { + + private static final int LIST_SIZE = 3; + + public List createUserInputList(String input){ + input = input.replaceAll(" ", ""); + + if (!BaseballUtils.checkLength(input)){ + throw new IllegalArgumentException(ErrorMessages.INVALID_INPUT_LENGTH); + } + + return BaseballUtils.stringToIntegerList(input); + } + + public List createAnswer() { + List answer = new ArrayList<>(); + + while(answer.size() < LIST_SIZE){ + answer = BaseballUtils.addUniqueNumber(answer); + } + + return answer; + } +} diff --git a/src/main/java/study/BaseballScore.java b/src/main/java/study/BaseballScore.java new file mode 100644 index 0000000..dd5cc29 --- /dev/null +++ b/src/main/java/study/BaseballScore.java @@ -0,0 +1,51 @@ +package study; + +public class BaseballScore { + private int strike; + private int ball; + + public BaseballScore(){ + this.strike = 0; + this.ball = 0; + } + + public void addStrike(){ + this.strike++; + } + + public void addBall() { + this.ball++; + } + + public int getStrike() { + return strike; + } + + public int getBall() { + return ball; + } + + public String printScore() { + StringBuilder sb = new StringBuilder(); + + if (ball != 0){ + sb.append(this.ball).append("볼"); + } + if (ball != 0 && strike != 0){ + sb.append(" "); + } + if (strike != 0){ + sb.append(this.strike).append("스트라이크"); + } + if (ball == 0 && strike == 0){ + sb.append("낫싱"); + } + + return sb.toString(); + } + + + public boolean isThreeStrike() { + return strike == 3; + } +} diff --git a/src/main/java/study/BaseballUtils.java b/src/main/java/study/BaseballUtils.java new file mode 100644 index 0000000..9d08213 --- /dev/null +++ b/src/main/java/study/BaseballUtils.java @@ -0,0 +1,57 @@ +package study; + +import study.message.ErrorMessages; + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +public class BaseballUtils { + + private static final int LIST_SIZE = 3; + private static final int MAX_NUMBER = 9; + private static final Random random = new Random(); + + + public static List addUniqueNumber(List answer) { + int num = getRandomNumber(); + + if (!answer.contains(num)){ + answer.add(num); + } + + return answer; + } + + public static int getRandomNumber() { + return random.nextInt(MAX_NUMBER)+1; + } + + public static List stringToIntegerList(String input) { + List list = new ArrayList<>(); + String[] strArr = input.split(""); + + for (String s : strArr) { + list.add(stringParseInt(s)); + } + + return list; + } + + public static int stringParseInt(String str){ + int value = 0; + + try { + value = Integer.parseInt(str); + } catch (NumberFormatException e){ + throw new NumberFormatException(ErrorMessages.INVALID_INPUT_NUMBER); + + } + + return value; + } + + public static boolean checkLength(String input) { + return input.length() == LIST_SIZE; + } +} diff --git a/src/main/java/study/message/ErrorMessages.java b/src/main/java/study/message/ErrorMessages.java new file mode 100644 index 0000000..48a435c --- /dev/null +++ b/src/main/java/study/message/ErrorMessages.java @@ -0,0 +1,6 @@ +package study.message; + +public class ErrorMessages { + public static final String INVALID_INPUT_NUMBER = "숫자만 입력 가능합니다."; + public static final String INVALID_INPUT_LENGTH = "3자리 수만 입력 가능합니다."; +} diff --git a/src/test/java/study/BaseballGameTest.java b/src/test/java/study/BaseballGameTest.java new file mode 100644 index 0000000..04d35b3 --- /dev/null +++ b/src/test/java/study/BaseballGameTest.java @@ -0,0 +1,23 @@ +package study; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class BaseballGameTest { + + @Test + @DisplayName("게임 진행을 위한 필요 데이터 생성") + void plaGame(){ + // given + String userInput = "123"; + + // when + BaseballGame game = new BaseballGame(); + BaseballScore score = game.playGame(userInput); + + // then + assertNotNull(score); + } +} diff --git a/src/test/java/study/BaseballGeneratorTest.java b/src/test/java/study/BaseballGeneratorTest.java new file mode 100644 index 0000000..b2a754b --- /dev/null +++ b/src/test/java/study/BaseballGeneratorTest.java @@ -0,0 +1,80 @@ +package study; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import study.message.ErrorMessages; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +public class BaseballGeneratorTest { + + @Test + @DisplayName("사용자 입력값 리스트로 변경") + void createUserInputList(){ + // given + String input = "123"; + + // when + BaseballGenerator generator = new BaseballGenerator(); + List response = generator.createUserInputList(input); + + // then + assertEquals(response.size(), 3); + assertEquals(response.get(0), 1); + assertEquals(response.get(1), 2); + assertEquals(response.get(2), 3); + } + + + + @Test + @DisplayName("공백이 포함된 사용자 입력값 리스트로 변경") + void createUserInputListContainBlank(){ + // given + String input = "12 3 "; + + // when + BaseballGenerator generator = new BaseballGenerator(); + List response = generator.createUserInputList(input); + + // then + assertEquals(response.size(), 3); + assertEquals(response.get(0), 1); + assertEquals(response.get(1), 2); + assertEquals(response.get(2), 3); + } + + @Test + @DisplayName("숫자가 아닌 문자열 입력으로 예외 발생") + void createUserInputListInputOver3Length_exception(){ + // given + String input = "문자열"; + + // when + BaseballGenerator generator = new BaseballGenerator(); + NumberFormatException fail = assertThrows(NumberFormatException.class, () -> generator.createUserInputList(input)); + + // then + assertEquals(fail.getMessage(), ErrorMessages.INVALID_INPUT_NUMBER); + } + + + @Test + @DisplayName("정답 생성") + void getAnswer(){ + // given + + // when + BaseballGenerator generator = new BaseballGenerator(); + List response = generator.createAnswer(); + + // then + assertEquals(response.size(), 3); + assertNotEquals(response.get(0), response.get(1)); + assertNotEquals(response.get(0), response.get(2)); + assertNotEquals(response.get(1), response.get(2)); + } + +} diff --git a/src/test/java/study/BaseballScoreTest.java b/src/test/java/study/BaseballScoreTest.java new file mode 100644 index 0000000..5715afe --- /dev/null +++ b/src/test/java/study/BaseballScoreTest.java @@ -0,0 +1,133 @@ +package study; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class BaseballScoreTest { + + @Test + @DisplayName("스트라이크 점수 증가") + void addStrike() { + // given + + // when + BaseballScore score = new BaseballScore(); + score.addStrike(); + + // then + assertEquals(score.getStrike(), 1); + } + + @Test + @DisplayName("볼 점수 증가") + void addBall() { + // given + + // when + BaseballScore score = new BaseballScore(); + score.addBall(); + + // then + assertEquals(score.getBall(), 1); + } + + + @Test + @DisplayName("점수 출력 : 3스트라이크") + void printScore3Strike(){ + // given + BaseballScore score = new BaseballScore(); + score.addStrike(); + score.addStrike(); + score.addStrike(); + + // when + String response = score.printScore(); + + // then + assertEquals(response, "3스트라이크"); + + } + + @Test + @DisplayName("점수 출력 : 1볼 2스트라이크") + void printScore1Ball2Strike(){ + // given + BaseballScore score = new BaseballScore(); + score.addBall(); + score.addStrike(); + score.addStrike(); + + // when + String response = score.printScore(); + + // then + assertEquals(response, "1볼 2스트라이크"); + + } + + @Test + @DisplayName("점수 출력 : 3볼") + void printScore3Ball(){ + // given + BaseballScore score = new BaseballScore(); + score.addBall(); + score.addBall(); + score.addBall(); + + // when + String response = score.printScore(); + + // then + assertEquals(response, "3볼"); + } + + @Test + @DisplayName("점수 출력 : 낫싱") + void printScoreNothing(){ + // given + BaseballScore score = new BaseballScore(); + + // when + String response = score.printScore(); + + // then + assertEquals(response, "낫싱"); + } + + @Test + @DisplayName("3스크라이크 여부") + void isThreeStrikeTrue(){ + // given + BaseballScore score = new BaseballScore(); + score.addStrike(); + score.addStrike(); + score.addStrike(); + + // when + boolean response = score.isThreeStrike(); + + // then + assertEquals(response, true); + + } + + @Test + @DisplayName("3스크라이크 여부") + void isThreeStrikeFalse(){ + // given + BaseballScore score = new BaseballScore(); + score.addStrike(); + score.addStrike(); + + // when + boolean response = score.isThreeStrike(); + + // then + assertEquals(response, false); + + } + +} diff --git a/src/test/java/study/BaseballTest.java b/src/test/java/study/BaseballTest.java new file mode 100644 index 0000000..28d93f1 --- /dev/null +++ b/src/test/java/study/BaseballTest.java @@ -0,0 +1,79 @@ +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.assertEquals; + +public class BaseballTest { + + @Test + @DisplayName("3스트라이크") + void playStrike(){ + // given + List answer = Arrays.asList(1, 2, 3); + List userInput = Arrays.asList(1, 2, 3); + + Baseball baseball = new Baseball(new BaseballScore(), answer); + + // when + BaseballScore response = baseball.play(userInput); + + // then + assertEquals(response.getStrike(), 3); + } + + @Test + @DisplayName("1스트라이크 2볼") + void play1Strike2Ball(){ + // given + List answer = Arrays.asList(1, 2, 3); + List userInput = Arrays.asList(1, 3, 2); + + Baseball baseball = new Baseball(new BaseballScore(), answer); + + // when + BaseballScore response = baseball.play(userInput); + + // then + assertEquals(response.getStrike(), 1); + assertEquals(response.getBall(), 2); + } + + @Test + @DisplayName("3볼") + void play3Ball(){ + // given + List answer = Arrays.asList(1, 2, 3); + List userInput = Arrays.asList(3, 1, 2); + + Baseball baseball = new Baseball(new BaseballScore(), answer); + + // when + BaseballScore response = baseball.play(userInput); + + // then + assertEquals(response.getBall(), 3); + } + + @Test + @DisplayName("낫씽") + void playNothing(){ + // given + List answer = Arrays.asList(1, 2, 3); + List userInput = Arrays.asList(4, 5, 6); + + Baseball baseball = new Baseball(new BaseballScore(), answer); + + // when + BaseballScore response = baseball.play(userInput); + + // then + assertEquals(response.getBall(), 0); + assertEquals(response.getBall(), 0); + } + +} diff --git a/src/test/java/study/BaseballUtilsTest.java b/src/test/java/study/BaseballUtilsTest.java new file mode 100644 index 0000000..1354828 --- /dev/null +++ b/src/test/java/study/BaseballUtilsTest.java @@ -0,0 +1,149 @@ +package study; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.RepeatedTest; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import study.message.ErrorMessages; + +import java.util.ArrayList; +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 BaseballUtilsTest { + + + @Test + @DisplayName("리스트 내 중복 체크 후 중복값 없어 리스트에 추가") + void addUniqueNumber() { + // given + List answer = new ArrayList<>(); + answer.add(1); + answer.add(2); + + // when + List response = BaseballUtils.addUniqueNumber(answer); + + // then + assertEquals(response.size(), 3); + assertEquals(response.get(0), 1); + assertEquals(response.get(1), 2); + assertNotNull(response.get(2)); + + } + + @RepeatedTest(10) + @DisplayName("랜덤 숫자 생성") + void getRandomNumber() { + // given + // when + int response = BaseballUtils.getRandomNumber(); + + // then + assertTrue(response > 0); + assertTrue(response < 10); + + } + + + @Test + @DisplayName("String 을 List로 변경") + void convertIntToList(){ + // given + String input = "123"; + + // when + List response = BaseballUtils.stringToIntegerList(input); + + // then + assertEquals(response.size(), 3); + assertEquals(response.get(0), 1); + assertEquals(response.get(1), 2); + assertEquals(response.get(2), 3); + } + + @Test + @DisplayName("String 을 List로 변경 시 숫자가 아닌 값 입력으로 예외 발생") + void convertIntToList_exception(){ + // given + String input = "문자열"; + + // when + NumberFormatException fail = assertThrows(NumberFormatException.class, () -> BaseballUtils.stringToIntegerList(input)); + + // then + assertEquals(fail.getMessage(), ErrorMessages.INVALID_INPUT_NUMBER); + + } + + + @Test + @DisplayName("String 을 Integer 로 변경") + void stringParseInt(){ + // given + String input = "1"; + + // when + int response = BaseballUtils.stringParseInt(input); + + // then + assertEquals(response, Integer.parseInt(input)); + } + + @Test + @DisplayName("String 을 Integer 로 변경 시 숫자 문자열이 아닌 값으로 인해 예외 발생") + void stringParseIntInputNotNumber1_exception(){ + // given + String input = "문자열"; + + // when + NumberFormatException fail = assertThrows(NumberFormatException.class, () -> BaseballUtils.stringParseInt(input)); + + // then + assertEquals(fail.getMessage(), ErrorMessages.INVALID_INPUT_NUMBER); + } + + @Test + @DisplayName("String 을 Integer 로 변경 시 숫자 문자열이 아닌 값으로 인해 예외 발생") + void stringParseIntInputNotNumber2_exception(){ + // given + String input = "_"; + + // when + NumberFormatException fail = assertThrows(NumberFormatException.class, () -> BaseballUtils.stringParseInt(input)); + + // then + assertEquals(fail.getMessage(), ErrorMessages.INVALID_INPUT_NUMBER); + } + + @Test + @DisplayName("문자열 길이 체크로 3자리 문자열 입력으로 true 반환") + void checkLengthTrue(){ + // given + String input = "123"; + + // when + boolean response = BaseballUtils.checkLength(input); + + // then + assertTrue(response); + } + + @Test + @DisplayName("문자열 길이 체크로 3자리 이상 문자열 입력으로 false 반환") + void checkLengthFalse(){ + // given + String input = "12345"; + + // when + boolean response = BaseballUtils.checkLength(input); + + // then + assertFalse(response); + } + + +} 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 fcdd3236a24f9faed243d554cc9e6e2d81a3020e Mon Sep 17 00:00:00 2001 From: Heesoo Date: Thu, 24 Oct 2024 15:37:37 +0900 Subject: [PATCH 2/7] =?UTF-8?q?[style]=20=ED=95=A8=EC=88=98=EB=AA=85,=20?= =?UTF-8?q?=EB=B3=80=EC=88=98=EB=AA=85=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/study/Baseball.java | 10 ++-- src/main/java/study/BaseballGenerator.java | 2 +- src/main/java/study/BaseballUtils.java | 4 +- .../java/study/BaseballGeneratorTest.java | 1 - src/test/java/study/BaseballScoreTest.java | 9 ++- src/test/java/study/BaseballTest.java | 59 ++++++++++++++++++- src/test/java/study/BaseballUtilsTest.java | 14 ++--- 7 files changed, 74 insertions(+), 25 deletions(-) diff --git a/src/main/java/study/Baseball.java b/src/main/java/study/Baseball.java index a047fb1..ca99f9f 100644 --- a/src/main/java/study/Baseball.java +++ b/src/main/java/study/Baseball.java @@ -16,19 +16,19 @@ public Baseball(BaseballScore score, List answer){ public BaseballScore play(List userInput) { System.out.println(answer.toString()); for(int i=0; i createAnswer() { List answer = new ArrayList<>(); while(answer.size() < LIST_SIZE){ - answer = BaseballUtils.addUniqueNumber(answer); + BaseballUtils.addUniqueNumber(answer); } return answer; diff --git a/src/main/java/study/BaseballUtils.java b/src/main/java/study/BaseballUtils.java index 9d08213..7428602 100644 --- a/src/main/java/study/BaseballUtils.java +++ b/src/main/java/study/BaseballUtils.java @@ -13,14 +13,12 @@ public class BaseballUtils { private static final Random random = new Random(); - public static List addUniqueNumber(List answer) { + public static void addUniqueNumber(List answer) { int num = getRandomNumber(); if (!answer.contains(num)){ answer.add(num); } - - return answer; } public static int getRandomNumber() { diff --git a/src/test/java/study/BaseballGeneratorTest.java b/src/test/java/study/BaseballGeneratorTest.java index b2a754b..5dd940c 100644 --- a/src/test/java/study/BaseballGeneratorTest.java +++ b/src/test/java/study/BaseballGeneratorTest.java @@ -65,7 +65,6 @@ void createUserInputListInputOver3Length_exception(){ @DisplayName("정답 생성") void getAnswer(){ // given - // when BaseballGenerator generator = new BaseballGenerator(); List response = generator.createAnswer(); diff --git a/src/test/java/study/BaseballScoreTest.java b/src/test/java/study/BaseballScoreTest.java index 5715afe..67085b0 100644 --- a/src/test/java/study/BaseballScoreTest.java +++ b/src/test/java/study/BaseballScoreTest.java @@ -11,9 +11,9 @@ public class BaseballScoreTest { @DisplayName("스트라이크 점수 증가") void addStrike() { // given + BaseballScore score = new BaseballScore(); // when - BaseballScore score = new BaseballScore(); score.addStrike(); // then @@ -24,16 +24,15 @@ void addStrike() { @DisplayName("볼 점수 증가") void addBall() { // given + BaseballScore score = new BaseballScore(); // when - BaseballScore score = new BaseballScore(); score.addBall(); // then assertEquals(score.getBall(), 1); } - @Test @DisplayName("점수 출력 : 3스트라이크") void printScore3Strike(){ @@ -98,7 +97,7 @@ void printScoreNothing(){ } @Test - @DisplayName("3스크라이크 여부") + @DisplayName("3스트라이크인 경우 true 반환") void isThreeStrikeTrue(){ // given BaseballScore score = new BaseballScore(); @@ -115,7 +114,7 @@ void isThreeStrikeTrue(){ } @Test - @DisplayName("3스크라이크 여부") + @DisplayName("3스트라이크가 아닌 경우 false 반환") void isThreeStrikeFalse(){ // given BaseballScore score = new BaseballScore(); diff --git a/src/test/java/study/BaseballTest.java b/src/test/java/study/BaseballTest.java index 28d93f1..069ea41 100644 --- a/src/test/java/study/BaseballTest.java +++ b/src/test/java/study/BaseballTest.java @@ -60,7 +60,7 @@ void play3Ball(){ } @Test - @DisplayName("낫씽") + @DisplayName("낫싱") void playNothing(){ // given List answer = Arrays.asList(1, 2, 3); @@ -76,4 +76,61 @@ void playNothing(){ assertEquals(response.getBall(), 0); } + @Test + @DisplayName("답 확인 : 스트라이크") + void calculateScoreStrike(){ + // given + int index = 2; + int input = 3; + + List answer = Arrays.asList(1, 2, 3); + BaseballScore score = new BaseballScore(); + Baseball baseball = new Baseball(score, answer); + + // when + baseball.calculateScore(index, input); + + // then + assertEquals(score.getStrike(), 1); + assertEquals(score.getBall(), 0); + } + + @Test + @DisplayName("답 확인 : 볼") + void calculateScoreBall(){ + // given + int index = 1; + int input = 3; + + List answer = Arrays.asList(1, 2, 3); + BaseballScore score = new BaseballScore(); + Baseball baseball = new Baseball(score, answer); + + // when + baseball.calculateScore(index, input); + + // then + assertEquals(score.getStrike(), 0); + assertEquals(score.getBall(), 1); + } + + @Test + @DisplayName("답 확인 : 낫싱") + void calculateScoreNothing(){ + // given + int index = 1; + int input = 5; + + List answer = Arrays.asList(1, 2, 3); + BaseballScore score = new BaseballScore(); + Baseball baseball = new Baseball(score, answer); + + // when + baseball.calculateScore(index, input); + + // then + assertEquals(score.getStrike(), 0); + assertEquals(score.getBall(), 0); + } + } diff --git a/src/test/java/study/BaseballUtilsTest.java b/src/test/java/study/BaseballUtilsTest.java index 1354828..7ab4cd1 100644 --- a/src/test/java/study/BaseballUtilsTest.java +++ b/src/test/java/study/BaseballUtilsTest.java @@ -3,19 +3,15 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.Test; -import org.mockito.InjectMocks; import study.message.ErrorMessages; import java.util.ArrayList; 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 BaseballUtilsTest { - @Test @DisplayName("리스트 내 중복 체크 후 중복값 없어 리스트에 추가") void addUniqueNumber() { @@ -25,13 +21,13 @@ void addUniqueNumber() { answer.add(2); // when - List response = BaseballUtils.addUniqueNumber(answer); + BaseballUtils.addUniqueNumber(answer); // then - assertEquals(response.size(), 3); - assertEquals(response.get(0), 1); - assertEquals(response.get(1), 2); - assertNotNull(response.get(2)); + assertEquals(answer.size(), 3); + assertEquals(answer.get(0), 1); + assertEquals(answer.get(1), 2); + assertNotNull(answer.get(2)); } From 7026d9d56d9722e2a8ec266cfa3cbda3d104877e Mon Sep 17 00:00:00 2001 From: Heesoo Date: Thu, 24 Oct 2024 16:23:16 +0900 Subject: [PATCH 3/7] =?UTF-8?q?[update]=20BaseballAnswer=20=EA=B0=9D?= =?UTF-8?q?=EC=B2=B4=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/Baseball.java | 14 +--- src/main/java/study/BaseballAnswer.java | 33 +++++++++ src/main/java/study/BaseballGame.java | 9 ++- src/main/java/study/BaseballGenerator.java | 46 ++++--------- src/main/java/study/BaseballUtils.java | 16 ++--- src/test/java/study/BaseballAnswerTest.java | 50 ++++++++++++++ .../java/study/BaseballGeneratorTest.java | 63 ----------------- src/test/java/study/BaseballTest.java | 68 ++++++++----------- src/test/java/study/BaseballUtilsTest.java | 51 +++++++------- 9 files changed, 163 insertions(+), 187 deletions(-) create mode 100644 src/main/java/study/BaseballAnswer.java create mode 100644 src/test/java/study/BaseballAnswerTest.java diff --git a/src/main/java/study/Baseball.java b/src/main/java/study/Baseball.java index ca99f9f..2b1f269 100644 --- a/src/main/java/study/Baseball.java +++ b/src/main/java/study/Baseball.java @@ -4,13 +4,14 @@ public class Baseball { + private static final int PLAY_ROUND = 3; private final List answer; private final BaseballScore score; - public Baseball(BaseballScore score, List answer){ + public Baseball(BaseballScore score, BaseballAnswer baseballAnswer){ this.score = score; - this.answer = answer; + this.answer = baseballAnswer.getAnswer(); } public BaseballScore play(List userInput) { @@ -33,13 +34,4 @@ public void calculateScore(int index, int input){ } } - - - - - - - - - } diff --git a/src/main/java/study/BaseballAnswer.java b/src/main/java/study/BaseballAnswer.java new file mode 100644 index 0000000..e5a9aa1 --- /dev/null +++ b/src/main/java/study/BaseballAnswer.java @@ -0,0 +1,33 @@ +package study; + +import java.util.ArrayList; +import java.util.List; + +public class BaseballAnswer { + private static final int LIST_SIZE = 3; + private final List answer; + + public BaseballAnswer(){ + this.answer = new ArrayList<>(); + createAnswer(); + } + + public List getAnswer() { + return answer; + } + + public void createAnswer() { + while(answer.size() < LIST_SIZE){ + addUniqueNumber(answer); + } + + } + + public void addUniqueNumber(List answer) { + int num = BaseballUtils.getRandomNumber(); + + if (!answer.contains(num)){ + answer.add(num); + } + } +} diff --git a/src/main/java/study/BaseballGame.java b/src/main/java/study/BaseballGame.java index cf87d61..cce9f40 100644 --- a/src/main/java/study/BaseballGame.java +++ b/src/main/java/study/BaseballGame.java @@ -4,12 +4,11 @@ public class BaseballGame { - public BaseballScore playGame(String number) { - BaseballGenerator generator = new BaseballGenerator(); - List userInput = generator.createUserInputList(number); - List answer = generator.createAnswer(); + public BaseballScore playGame(String input) { + BaseballUtils.checkLength(input); + List userInput = BaseballUtils.stringToIntegerList(input); - Baseball baseball = new Baseball(new BaseballScore(), answer); + Baseball baseball = new Baseball(new BaseballScore(), new BaseballAnswer()); return baseball.play(userInput); } diff --git a/src/main/java/study/BaseballGenerator.java b/src/main/java/study/BaseballGenerator.java index 3a3e68b..0c866d4 100644 --- a/src/main/java/study/BaseballGenerator.java +++ b/src/main/java/study/BaseballGenerator.java @@ -1,31 +1,15 @@ -package study; - -import study.message.ErrorMessages; - -import java.util.ArrayList; -import java.util.List; - -public class BaseballGenerator { - - private static final int LIST_SIZE = 3; - - public List createUserInputList(String input){ - input = input.replaceAll(" ", ""); - - if (!BaseballUtils.checkLength(input)){ - throw new IllegalArgumentException(ErrorMessages.INVALID_INPUT_LENGTH); - } - - return BaseballUtils.stringToIntegerList(input); - } - - public List createAnswer() { - List answer = new ArrayList<>(); - - while(answer.size() < LIST_SIZE){ - BaseballUtils.addUniqueNumber(answer); - } - - return answer; - } -} +//package study; +// +//import study.message.ErrorMessages; +// +//import java.util.List; +// +//public class BaseballGenerator { +// +// private static final int LIST_SIZE = 3; +// +// public List createUserInputList(String input){ +// +// } +// +//} diff --git a/src/main/java/study/BaseballUtils.java b/src/main/java/study/BaseballUtils.java index 7428602..7af3091 100644 --- a/src/main/java/study/BaseballUtils.java +++ b/src/main/java/study/BaseballUtils.java @@ -13,14 +13,6 @@ public class BaseballUtils { private static final Random random = new Random(); - public static void addUniqueNumber(List answer) { - int num = getRandomNumber(); - - if (!answer.contains(num)){ - answer.add(num); - } - } - public static int getRandomNumber() { return random.nextInt(MAX_NUMBER)+1; } @@ -49,7 +41,11 @@ public static int stringParseInt(String str){ return value; } - public static boolean checkLength(String input) { - return input.length() == LIST_SIZE; + public static void checkLength(String input) { + input = input.replaceAll(" ", ""); + + if (input.length() != LIST_SIZE){ + throw new IllegalArgumentException(ErrorMessages.INVALID_INPUT_LENGTH); + } } } diff --git a/src/test/java/study/BaseballAnswerTest.java b/src/test/java/study/BaseballAnswerTest.java new file mode 100644 index 0000000..4ea24a8 --- /dev/null +++ b/src/test/java/study/BaseballAnswerTest.java @@ -0,0 +1,50 @@ +package study; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +public class BaseballAnswerTest { + + @Test + @DisplayName("정답 생성") + void getAnswer(){ + // given + BaseballAnswer baseballAnswer = new BaseballAnswer(); + + // when + baseballAnswer.createAnswer(); + + // then + List response = baseballAnswer.getAnswer(); + assertEquals(response.size(), 3); + assertNotEquals(response.get(0), response.get(1)); + assertNotEquals(response.get(0), response.get(2)); + assertNotEquals(response.get(1), response.get(2)); + } + + @Test + @DisplayName("리스트 내 중복 체크 후 중복값 없어 리스트에 추가") + void addUniqueNumber() { + // given + List answer = new ArrayList<>(); + answer.add(1); + answer.add(2); + + BaseballAnswer baseballAnswer = new BaseballAnswer(); + + // when + baseballAnswer.addUniqueNumber(answer); + + // then + assertEquals(answer.size(), 3); + assertEquals(answer.get(0), 1); + assertEquals(answer.get(1), 2); + assertNotNull(answer.get(2)); + + } +} diff --git a/src/test/java/study/BaseballGeneratorTest.java b/src/test/java/study/BaseballGeneratorTest.java index 5dd940c..b32e365 100644 --- a/src/test/java/study/BaseballGeneratorTest.java +++ b/src/test/java/study/BaseballGeneratorTest.java @@ -10,70 +10,7 @@ public class BaseballGeneratorTest { - @Test - @DisplayName("사용자 입력값 리스트로 변경") - void createUserInputList(){ - // given - String input = "123"; - // when - BaseballGenerator generator = new BaseballGenerator(); - List response = generator.createUserInputList(input); - // then - assertEquals(response.size(), 3); - assertEquals(response.get(0), 1); - assertEquals(response.get(1), 2); - assertEquals(response.get(2), 3); - } - - - - @Test - @DisplayName("공백이 포함된 사용자 입력값 리스트로 변경") - void createUserInputListContainBlank(){ - // given - String input = "12 3 "; - - // when - BaseballGenerator generator = new BaseballGenerator(); - List response = generator.createUserInputList(input); - - // then - assertEquals(response.size(), 3); - assertEquals(response.get(0), 1); - assertEquals(response.get(1), 2); - assertEquals(response.get(2), 3); - } - - @Test - @DisplayName("숫자가 아닌 문자열 입력으로 예외 발생") - void createUserInputListInputOver3Length_exception(){ - // given - String input = "문자열"; - - // when - BaseballGenerator generator = new BaseballGenerator(); - NumberFormatException fail = assertThrows(NumberFormatException.class, () -> generator.createUserInputList(input)); - - // then - assertEquals(fail.getMessage(), ErrorMessages.INVALID_INPUT_NUMBER); - } - - - @Test - @DisplayName("정답 생성") - void getAnswer(){ - // given - // when - BaseballGenerator generator = new BaseballGenerator(); - List response = generator.createAnswer(); - - // then - assertEquals(response.size(), 3); - assertNotEquals(response.get(0), response.get(1)); - assertNotEquals(response.get(0), response.get(2)); - assertNotEquals(response.get(1), response.get(2)); - } } diff --git a/src/test/java/study/BaseballTest.java b/src/test/java/study/BaseballTest.java index 069ea41..030ee2f 100644 --- a/src/test/java/study/BaseballTest.java +++ b/src/test/java/study/BaseballTest.java @@ -14,13 +14,13 @@ public class BaseballTest { @DisplayName("3스트라이크") void playStrike(){ // given - List answer = Arrays.asList(1, 2, 3); - List userInput = Arrays.asList(1, 2, 3); + BaseballAnswer baseballAnswer = new BaseballAnswer(); + List answer = baseballAnswer.getAnswer(); - Baseball baseball = new Baseball(new BaseballScore(), answer); + Baseball baseball = new Baseball(new BaseballScore(), baseballAnswer); // when - BaseballScore response = baseball.play(userInput); + BaseballScore response = baseball.play(answer); // then assertEquals(response.getStrike(), 3); @@ -30,10 +30,11 @@ void playStrike(){ @DisplayName("1스트라이크 2볼") void play1Strike2Ball(){ // given - List answer = Arrays.asList(1, 2, 3); - List userInput = Arrays.asList(1, 3, 2); + BaseballAnswer baseballAnswer = new BaseballAnswer(); + List answer = baseballAnswer.getAnswer(); + List userInput = Arrays.asList(answer.get(0), answer.get(2), answer.get(1)); - Baseball baseball = new Baseball(new BaseballScore(), answer); + Baseball baseball = new Baseball(new BaseballScore(), baseballAnswer); // when BaseballScore response = baseball.play(userInput); @@ -47,10 +48,11 @@ void play1Strike2Ball(){ @DisplayName("3볼") void play3Ball(){ // given - List answer = Arrays.asList(1, 2, 3); - List userInput = Arrays.asList(3, 1, 2); + BaseballAnswer baseballAnswer = new BaseballAnswer(); + List answer = baseballAnswer.getAnswer(); + List userInput = Arrays.asList(answer.get(1), answer.get(2), answer.get(0)); - Baseball baseball = new Baseball(new BaseballScore(), answer); + Baseball baseball = new Baseball(new BaseballScore(), baseballAnswer); // when BaseballScore response = baseball.play(userInput); @@ -59,33 +61,20 @@ void play3Ball(){ assertEquals(response.getBall(), 3); } - @Test - @DisplayName("낫싱") - void playNothing(){ - // given - List answer = Arrays.asList(1, 2, 3); - List userInput = Arrays.asList(4, 5, 6); - - Baseball baseball = new Baseball(new BaseballScore(), answer); - - // when - BaseballScore response = baseball.play(userInput); - - // then - assertEquals(response.getBall(), 0); - assertEquals(response.getBall(), 0); - } @Test @DisplayName("답 확인 : 스트라이크") void calculateScoreStrike(){ // given - int index = 2; - int input = 3; + BaseballAnswer baseballAnswer = new BaseballAnswer(); + List answer = baseballAnswer.getAnswer(); - List answer = Arrays.asList(1, 2, 3); BaseballScore score = new BaseballScore(); - Baseball baseball = new Baseball(score, answer); + Baseball baseball = new Baseball(score, baseballAnswer); + + int index = 2; + int input = answer.get(2); + // when baseball.calculateScore(index, input); @@ -99,12 +88,14 @@ void calculateScoreStrike(){ @DisplayName("답 확인 : 볼") void calculateScoreBall(){ // given - int index = 1; - int input = 3; + BaseballAnswer baseballAnswer = new BaseballAnswer(); + List answer = baseballAnswer.getAnswer(); - List answer = Arrays.asList(1, 2, 3); BaseballScore score = new BaseballScore(); - Baseball baseball = new Baseball(score, answer); + Baseball baseball = new Baseball(score, baseballAnswer); + + int index = 1; + int input = answer.get(2); // when baseball.calculateScore(index, input); @@ -118,12 +109,11 @@ void calculateScoreBall(){ @DisplayName("답 확인 : 낫싱") void calculateScoreNothing(){ // given - int index = 1; - int input = 5; - - List answer = Arrays.asList(1, 2, 3); BaseballScore score = new BaseballScore(); - Baseball baseball = new Baseball(score, answer); + Baseball baseball = new Baseball(score, new BaseballAnswer()); + + int index = 1; + int input = 10; // when baseball.calculateScore(index, input); diff --git a/src/test/java/study/BaseballUtilsTest.java b/src/test/java/study/BaseballUtilsTest.java index 7ab4cd1..404847a 100644 --- a/src/test/java/study/BaseballUtilsTest.java +++ b/src/test/java/study/BaseballUtilsTest.java @@ -5,32 +5,12 @@ import org.junit.jupiter.api.Test; import study.message.ErrorMessages; -import java.util.ArrayList; import java.util.List; import static org.junit.jupiter.api.Assertions.*; public class BaseballUtilsTest { - @Test - @DisplayName("리스트 내 중복 체크 후 중복값 없어 리스트에 추가") - void addUniqueNumber() { - // given - List answer = new ArrayList<>(); - answer.add(1); - answer.add(2); - - // when - BaseballUtils.addUniqueNumber(answer); - - // then - assertEquals(answer.size(), 3); - assertEquals(answer.get(0), 1); - assertEquals(answer.get(1), 2); - assertNotNull(answer.get(2)); - - } - @RepeatedTest(10) @DisplayName("랜덤 숫자 생성") void getRandomNumber() { @@ -61,6 +41,10 @@ void convertIntToList(){ assertEquals(response.get(2), 3); } + + + + @Test @DisplayName("String 을 List로 변경 시 숫자가 아닌 값 입력으로 예외 발생") void convertIntToList_exception(){ @@ -116,30 +100,41 @@ void stringParseIntInputNotNumber2_exception(){ } @Test - @DisplayName("문자열 길이 체크로 3자리 문자열 입력으로 true 반환") - void checkLengthTrue(){ + @DisplayName("문자열 길이 체크") + void checkLength(){ // given String input = "123"; // when - boolean response = BaseballUtils.checkLength(input); - // then - assertTrue(response); + assertDoesNotThrow(() -> BaseballUtils.checkLength(input)); } @Test - @DisplayName("문자열 길이 체크로 3자리 이상 문자열 입력으로 false 반환") + @DisplayName("공백이 포함된 문자열 길이 체크") + void checkLengthContainBlank(){ + // given + String input = "12 3 "; + + // when + assertDoesNotThrow(() -> BaseballUtils.checkLength(input)); + } + + @Test + @DisplayName("문자열 길이 체크로 3자리 이상 문자열 입력 예외 발생") void checkLengthFalse(){ // given String input = "12345"; // when - boolean response = BaseballUtils.checkLength(input); + IllegalArgumentException fail = assertThrows(IllegalArgumentException.class, () -> BaseballUtils.checkLength(input)); // then - assertFalse(response); + assertEquals(fail.getMessage(), ErrorMessages.INVALID_INPUT_LENGTH); } + + + } From 906639277ac9d56b8ae1c65287944a3528d8c7d5 Mon Sep 17 00:00:00 2001 From: Heesoo Date: Fri, 25 Oct 2024 15:29:46 +0900 Subject: [PATCH 4/7] =?UTF-8?q?[update]=20=EC=82=AC=EC=9A=A9=EC=9E=90=20?= =?UTF-8?q?=EC=9E=85=EB=A0=A5=20=EA=B4=80=EB=A0=A8=20=ED=81=B4=EB=9E=98?= =?UTF-8?q?=EC=8A=A4=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/study/{ => model}/Baseball.java | 8 +- src/main/java/study/model/BaseballUser.java | 35 +++++ .../java/study/{ => model}/BaseballTest.java | 21 ++- .../java/study/model/BaseballUserTest.java | 137 ++++++++++++++++++ 4 files changed, 190 insertions(+), 11 deletions(-) rename src/main/java/study/{ => model}/Baseball.java (84%) create mode 100644 src/main/java/study/model/BaseballUser.java rename src/test/java/study/{ => model}/BaseballTest.java (81%) create mode 100644 src/test/java/study/model/BaseballUserTest.java diff --git a/src/main/java/study/Baseball.java b/src/main/java/study/model/Baseball.java similarity index 84% rename from src/main/java/study/Baseball.java rename to src/main/java/study/model/Baseball.java index 2b1f269..2e4f1d2 100644 --- a/src/main/java/study/Baseball.java +++ b/src/main/java/study/model/Baseball.java @@ -1,10 +1,8 @@ -package study; +package study.model; import java.util.*; public class Baseball { - - private static final int PLAY_ROUND = 3; private final List answer; private final BaseballScore score; @@ -14,10 +12,10 @@ public Baseball(BaseballScore score, BaseballAnswer baseballAnswer){ this.answer = baseballAnswer.getAnswer(); } - public BaseballScore play(List userInput) { + public BaseballScore play(BaseballUser user) { System.out.println(answer.toString()); for(int i=0; i userInput; + + public BaseballUser(String input){ + InputValidate inputValidate = new InputValidate(input); + this.userInput = inputValidate.validate(); + } + + public int getUserInputNumber(int index){ + return userInput.get(index); + } + + public static class InputValidate { + private final String input; + + public InputValidate(String input){ + this.input = BaseballUtils.removeSpaces(input); + } + + public List validate(){ + BaseballUtils.checkLength(input); + return BaseballUtils.stringToIntegerList(input); + } + + } + +} + diff --git a/src/test/java/study/BaseballTest.java b/src/test/java/study/model/BaseballTest.java similarity index 81% rename from src/test/java/study/BaseballTest.java rename to src/test/java/study/model/BaseballTest.java index 030ee2f..29d008a 100644 --- a/src/test/java/study/BaseballTest.java +++ b/src/test/java/study/model/BaseballTest.java @@ -1,7 +1,8 @@ -package study; +package study.model; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import study.utils.BaseballUtils; import java.util.Arrays; import java.util.List; @@ -17,10 +18,13 @@ void playStrike(){ BaseballAnswer baseballAnswer = new BaseballAnswer(); List answer = baseballAnswer.getAnswer(); + String userInput = String.valueOf(answer.get(0)) + answer.get(1) + answer.get(2); + BaseballUser user = new BaseballUser(userInput); + Baseball baseball = new Baseball(new BaseballScore(), baseballAnswer); // when - BaseballScore response = baseball.play(answer); + BaseballScore response = baseball.play(user); // then assertEquals(response.getStrike(), 3); @@ -32,12 +36,14 @@ void play1Strike2Ball(){ // given BaseballAnswer baseballAnswer = new BaseballAnswer(); List answer = baseballAnswer.getAnswer(); - List userInput = Arrays.asList(answer.get(0), answer.get(2), answer.get(1)); + + String userInput = String.valueOf(answer.get(0)) + answer.get(2) + answer.get(1); + BaseballUser user = new BaseballUser(userInput); Baseball baseball = new Baseball(new BaseballScore(), baseballAnswer); // when - BaseballScore response = baseball.play(userInput); + BaseballScore response = baseball.play(user); // then assertEquals(response.getStrike(), 1); @@ -50,12 +56,14 @@ void play3Ball(){ // given BaseballAnswer baseballAnswer = new BaseballAnswer(); List answer = baseballAnswer.getAnswer(); - List userInput = Arrays.asList(answer.get(1), answer.get(2), answer.get(0)); + + String userInput = String.valueOf(answer.get(1)) + answer.get(2) + answer.get(0); + BaseballUser user = new BaseballUser(userInput); Baseball baseball = new Baseball(new BaseballScore(), baseballAnswer); // when - BaseballScore response = baseball.play(userInput); + BaseballScore response = baseball.play(user); // then assertEquals(response.getBall(), 3); @@ -123,4 +131,5 @@ void calculateScoreNothing(){ assertEquals(score.getBall(), 0); } + } diff --git a/src/test/java/study/model/BaseballUserTest.java b/src/test/java/study/model/BaseballUserTest.java new file mode 100644 index 0000000..fc2a245 --- /dev/null +++ b/src/test/java/study/model/BaseballUserTest.java @@ -0,0 +1,137 @@ +package study.model; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import study.message.ErrorMessages; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class BaseballUserTest { + + @Test + @DisplayName("사용자 입력 값이 담긴 BaseballUser 클래스 생성") + void baseballUser(){ + // given + String userInput = "123"; + + // when + BaseballUser user = new BaseballUser(userInput); + + // then + assertEquals(user.getUserInputNumber(0), 1); + assertEquals(user.getUserInputNumber(1), 2); + assertEquals(user.getUserInputNumber(2), 3); + } + + @Test + @DisplayName("BaseballUser 클래스 생성 시 사용자 입력에 공백이 포함되어 있어도 공백 제거 후 클래스 생성 성공") + void baseballUserContainsSpaces(){ + // given + String userInput = "12 3"; + + // when + BaseballUser user = new BaseballUser(userInput); + + // then + assertEquals(user.getUserInputNumber(0), 1); + assertEquals(user.getUserInputNumber(1), 2); + assertEquals(user.getUserInputNumber(2), 3); + } + + @Test + @DisplayName("BaseballUser 클래스 생성 시 사용자 입력 길이가 3자리가 아니여서 예외 발생") + void baseballUserInputNot3Length_exception(){ + // given + String userInput = "3"; + + // when + IllegalArgumentException fail = assertThrows(IllegalArgumentException.class, () -> new BaseballUser(userInput)); + + // then + assertEquals(fail.getMessage(), ErrorMessages.INVALID_INPUT_LENGTH); + + } + + @Test + @DisplayName("BaseballUser 클래스 생성 시 사용자 입력에 숫자 문자열이 아닌 값이 포함되 예외 발생") + void baseballUserInputNotNumber_exception(){ + // given + String userInput = "1자리"; + + // when + NumberFormatException fail = assertThrows(NumberFormatException.class, () -> new BaseballUser(userInput)); + + // then + assertEquals(fail.getMessage(), ErrorMessages.INVALID_INPUT_NUMBER); + + } + + @Test + @DisplayName("InputValidate 클래스 이용해 입력 값 검증") + void innerClassValidate(){ + // given + String userInput = "123"; + + BaseballUser.InputValidate validate = new BaseballUser.InputValidate(userInput); + + // when + List response = validate.validate(); + + assertEquals(response.get(0), 1); + assertEquals(response.get(1), 2); + assertEquals(response.get(2), 3); + + } + + @Test + @DisplayName("InputValidate inner 클래스 이용해 입력 값 검증 시 입력 값에 공백 포함된 경우") + void innerClassValidateContainsSpaces(){ + // given + String userInput = "1 23 "; + + BaseballUser.InputValidate inputValidate = new BaseballUser.InputValidate(userInput); + + // when + List response = inputValidate.validate(); + + assertEquals(response.get(0), 1); + assertEquals(response.get(1), 2); + assertEquals(response.get(2), 3); + + } + + @Test + @DisplayName("InputValidate inner 클래스 이용해 입력 값 검증 시 사용자 입력 길이가 3자리가 아니여서 예외 발생") + void innerClassInputNot3Length_exception(){ + // given + String userInput = "3"; + + BaseballUser.InputValidate inputValidate = new BaseballUser.InputValidate(userInput); + + // when + IllegalArgumentException fail = assertThrows(IllegalArgumentException.class, () -> inputValidate.validate()); + + // then + assertEquals(fail.getMessage(), ErrorMessages.INVALID_INPUT_LENGTH); + + } + + @Test + @DisplayName("InputValidate inner 클래스 이용해 입력 값 검증 시 숫자 문자열이 아닌 값이 포함되 예외 발생") + void innerClassInputNotNumber_exception(){ + // given + String userInput = "1자리"; + + BaseballUser.InputValidate inputValidate = new BaseballUser.InputValidate(userInput); + // when + NumberFormatException fail = assertThrows(NumberFormatException.class, () -> inputValidate.validate()); + + // then + assertEquals(fail.getMessage(), ErrorMessages.INVALID_INPUT_NUMBER); + + } + +} From d30a69c5b9add5a1ecf8026dc1b043e540a86c00 Mon Sep 17 00:00:00 2001 From: Heesoo Date: Fri, 25 Oct 2024 15:30:17 +0900 Subject: [PATCH 5/7] =?UTF-8?q?[update]=20=EC=B6=9C=EB=A0=A5=20=EA=B4=80?= =?UTF-8?q?=EB=A0=A8=EB=90=9C=20=ED=95=A8=EC=88=98=20=ED=81=B4=EB=9E=98?= =?UTF-8?q?=EC=8A=A4=EB=A1=9C=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/study/BaseballScore.java | 51 ------------ src/main/java/study/model/BaseballScore.java | 31 ++++++++ src/main/java/study/view/ResultView.java | 27 +++++++ .../study/{ => model}/BaseballScoreTest.java | 65 +-------------- src/test/java/study/view/ResultViewTest.java | 79 +++++++++++++++++++ 5 files changed, 138 insertions(+), 115 deletions(-) delete mode 100644 src/main/java/study/BaseballScore.java create mode 100644 src/main/java/study/model/BaseballScore.java create mode 100644 src/main/java/study/view/ResultView.java rename src/test/java/study/{ => model}/BaseballScoreTest.java (50%) create mode 100644 src/test/java/study/view/ResultViewTest.java diff --git a/src/main/java/study/BaseballScore.java b/src/main/java/study/BaseballScore.java deleted file mode 100644 index dd5cc29..0000000 --- a/src/main/java/study/BaseballScore.java +++ /dev/null @@ -1,51 +0,0 @@ -package study; - -public class BaseballScore { - private int strike; - private int ball; - - public BaseballScore(){ - this.strike = 0; - this.ball = 0; - } - - public void addStrike(){ - this.strike++; - } - - public void addBall() { - this.ball++; - } - - public int getStrike() { - return strike; - } - - public int getBall() { - return ball; - } - - public String printScore() { - StringBuilder sb = new StringBuilder(); - - if (ball != 0){ - sb.append(this.ball).append("볼"); - } - if (ball != 0 && strike != 0){ - sb.append(" "); - } - if (strike != 0){ - sb.append(this.strike).append("스트라이크"); - } - if (ball == 0 && strike == 0){ - sb.append("낫싱"); - } - - return sb.toString(); - } - - - public boolean isThreeStrike() { - return strike == 3; - } -} diff --git a/src/main/java/study/model/BaseballScore.java b/src/main/java/study/model/BaseballScore.java new file mode 100644 index 0000000..5c2d79f --- /dev/null +++ b/src/main/java/study/model/BaseballScore.java @@ -0,0 +1,31 @@ +package study.model; + +public class BaseballScore { + private int strike; + private int ball; + + public BaseballScore(){ + this.strike = 0; + this.ball = 0; + } + + public void addStrike(){ + this.strike++; + } + + public void addBall() { + this.ball++; + } + + public int getStrike() { + return strike; + } + + public int getBall() { + return ball; + } + + public boolean isThreeStrike() { + return strike == 3; + } +} diff --git a/src/main/java/study/view/ResultView.java b/src/main/java/study/view/ResultView.java new file mode 100644 index 0000000..572c08a --- /dev/null +++ b/src/main/java/study/view/ResultView.java @@ -0,0 +1,27 @@ +package study.view; + +import study.model.BaseballScore; + +public class ResultView { + public String printScore(BaseballScore score) { + StringBuilder sb = new StringBuilder(); + + int ball = score.getBall(); + int strike = score.getStrike(); + + if (ball != 0){ + sb.append(ball).append("볼"); + } + if (ball != 0 && strike != 0){ + sb.append(" "); + } + if (strike != 0){ + sb.append(strike).append("스트라이크"); + } + if (ball == 0 && strike == 0){ + sb.append("낫싱"); + } + + return sb.toString(); + } +} diff --git a/src/test/java/study/BaseballScoreTest.java b/src/test/java/study/model/BaseballScoreTest.java similarity index 50% rename from src/test/java/study/BaseballScoreTest.java rename to src/test/java/study/model/BaseballScoreTest.java index 67085b0..b20d23e 100644 --- a/src/test/java/study/BaseballScoreTest.java +++ b/src/test/java/study/model/BaseballScoreTest.java @@ -1,4 +1,4 @@ -package study; +package study.model; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -33,69 +33,6 @@ void addBall() { assertEquals(score.getBall(), 1); } - @Test - @DisplayName("점수 출력 : 3스트라이크") - void printScore3Strike(){ - // given - BaseballScore score = new BaseballScore(); - score.addStrike(); - score.addStrike(); - score.addStrike(); - - // when - String response = score.printScore(); - - // then - assertEquals(response, "3스트라이크"); - - } - - @Test - @DisplayName("점수 출력 : 1볼 2스트라이크") - void printScore1Ball2Strike(){ - // given - BaseballScore score = new BaseballScore(); - score.addBall(); - score.addStrike(); - score.addStrike(); - - // when - String response = score.printScore(); - - // then - assertEquals(response, "1볼 2스트라이크"); - - } - - @Test - @DisplayName("점수 출력 : 3볼") - void printScore3Ball(){ - // given - BaseballScore score = new BaseballScore(); - score.addBall(); - score.addBall(); - score.addBall(); - - // when - String response = score.printScore(); - - // then - assertEquals(response, "3볼"); - } - - @Test - @DisplayName("점수 출력 : 낫싱") - void printScoreNothing(){ - // given - BaseballScore score = new BaseballScore(); - - // when - String response = score.printScore(); - - // then - assertEquals(response, "낫싱"); - } - @Test @DisplayName("3스트라이크인 경우 true 반환") void isThreeStrikeTrue(){ diff --git a/src/test/java/study/view/ResultViewTest.java b/src/test/java/study/view/ResultViewTest.java new file mode 100644 index 0000000..ddaaffb --- /dev/null +++ b/src/test/java/study/view/ResultViewTest.java @@ -0,0 +1,79 @@ +package study.view; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import study.model.BaseballScore; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class ResultViewTest { + @Test + @DisplayName("점수 출력 : 3스트라이크") + void printScore3Strike(){ + // given + BaseballScore score = new BaseballScore(); + score.addStrike(); + score.addStrike(); + score.addStrike(); + + ResultView resultView = new ResultView(); + + // when + String response = resultView.printScore(score); + + // then + assertEquals(response, "3스트라이크"); + + } + + @Test + @DisplayName("점수 출력 : 1볼 2스트라이크") + void printScore1Ball2Strike(){ + // given + BaseballScore score = new BaseballScore(); + score.addBall(); + score.addStrike(); + score.addStrike(); + + ResultView resultView = new ResultView(); + + // when + String response = resultView.printScore(score); + + // then + assertEquals(response, "1볼 2스트라이크"); + + } + + @Test + @DisplayName("점수 출력 : 3볼") + void printScore3Ball(){ + // given + BaseballScore score = new BaseballScore(); + score.addBall(); + score.addBall(); + score.addBall(); + + ResultView resultView = new ResultView(); + + // when + String response = resultView.printScore(score); + + // then + assertEquals(response, "3볼"); + } + + @Test + @DisplayName("점수 출력 : 낫싱") + void printScoreNothing(){ + // given + BaseballScore score = new BaseballScore(); + ResultView resultView = new ResultView(); + + // when + String response = resultView.printScore(score); + + // then + assertEquals(response, "낫싱"); + } +} From 4fd0f35c12afed7769f551709b337813fd86b7a4 Mon Sep 17 00:00:00 2001 From: Heesoo Date: Fri, 25 Oct 2024 15:31:07 +0900 Subject: [PATCH 6/7] =?UTF-8?q?[update]=20=EB=8B=A8=EC=9D=BC=20=EC=9B=90?= =?UTF-8?q?=EC=B9=99=EC=97=90=20=EB=94=B0=EB=A5=B8=20=EB=A9=94=EC=86=8C?= =?UTF-8?q?=EB=93=9C=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/study/{ => utils}/BaseballUtils.java | 33 ++++-- .../study/{ => utils}/BaseballUtilsTest.java | 112 +++++++++++------- 2 files changed, 90 insertions(+), 55 deletions(-) rename src/main/java/study/{ => utils}/BaseballUtils.java (63%) rename src/test/java/study/{ => utils}/BaseballUtilsTest.java (77%) diff --git a/src/main/java/study/BaseballUtils.java b/src/main/java/study/utils/BaseballUtils.java similarity index 63% rename from src/main/java/study/BaseballUtils.java rename to src/main/java/study/utils/BaseballUtils.java index 7af3091..cf88b6c 100644 --- a/src/main/java/study/BaseballUtils.java +++ b/src/main/java/study/utils/BaseballUtils.java @@ -1,4 +1,4 @@ -package study; +package study.utils; import study.message.ErrorMessages; @@ -8,21 +8,35 @@ public class BaseballUtils { - private static final int LIST_SIZE = 3; - private static final int MAX_NUMBER = 9; + private static final int MAX_LIST_SIZE = 3; + private static final int MAX_RANDOM_NUMBER = 9; private static final Random random = new Random(); public static int getRandomNumber() { - return random.nextInt(MAX_NUMBER)+1; + return random.nextInt(MAX_RANDOM_NUMBER)+1; + } + + public static String removeSpaces(String input){ + return input.replaceAll(" ", ""); + } + + public static void checkLength(String input) { + if (input.length() != MAX_LIST_SIZE){ + throw new IllegalArgumentException(ErrorMessages.INVALID_INPUT_LENGTH); + } + } + + public static String[] stringToArrays(String input){ + return input.split(""); } public static List stringToIntegerList(String input) { List list = new ArrayList<>(); - String[] strArr = input.split(""); + String[] strArr = stringToArrays(input); for (String s : strArr) { - list.add(stringParseInt(s)); + list.add(BaseballUtils.stringParseInt(s)); } return list; @@ -41,11 +55,4 @@ public static int stringParseInt(String str){ return value; } - public static void checkLength(String input) { - input = input.replaceAll(" ", ""); - - if (input.length() != LIST_SIZE){ - throw new IllegalArgumentException(ErrorMessages.INVALID_INPUT_LENGTH); - } - } } diff --git a/src/test/java/study/BaseballUtilsTest.java b/src/test/java/study/utils/BaseballUtilsTest.java similarity index 77% rename from src/test/java/study/BaseballUtilsTest.java rename to src/test/java/study/utils/BaseballUtilsTest.java index 404847a..a3a9273 100644 --- a/src/test/java/study/BaseballUtilsTest.java +++ b/src/test/java/study/utils/BaseballUtilsTest.java @@ -1,4 +1,4 @@ -package study; +package study.utils; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.RepeatedTest; @@ -24,10 +24,77 @@ void getRandomNumber() { } + @Test + @DisplayName("공백 제거") + void removeSpaces(){ + // given + String input = " 1 2 3"; + + // when + String response = BaseballUtils.removeSpaces(input); + + // then + assertEquals(response, "123"); + } + + + @Test + @DisplayName("공백 제거 시 공백이없는 경우") + void removeSpacesNotContainsSpaces(){ + // given + String input = "123"; + + // when + String response = BaseballUtils.removeSpaces(input); + + // then + assertEquals(response, "123"); + } + + + @Test + @DisplayName("문자열 길이 체크") + void checkLength(){ + // given + String input = "123"; + + // when + // then + assertDoesNotThrow(() -> BaseballUtils.checkLength(input)); + } + + @Test + @DisplayName("문자열 길이 체크로 3자리 이상 문자열 입력 예외 발생") + void checkLengthFalse(){ + // given + String input = "12345"; + + // when + IllegalArgumentException fail = assertThrows(IllegalArgumentException.class, () -> BaseballUtils.checkLength(input)); + + // then + assertEquals(fail.getMessage(), ErrorMessages.INVALID_INPUT_LENGTH); + } + + @Test + @DisplayName("String 을 String 배열로 변경") + void stringToArrays(){ + // given + String input = "123"; + + // when + String[] response = BaseballUtils.stringToArrays(input); + + // then + assertEquals(response[0], "1"); + assertEquals(response[1], "2"); + assertEquals(response[2], "3"); + } + @Test @DisplayName("String 을 List로 변경") - void convertIntToList(){ + void stringToIntegerList(){ // given String input = "123"; @@ -43,11 +110,9 @@ void convertIntToList(){ - - @Test @DisplayName("String 을 List로 변경 시 숫자가 아닌 값 입력으로 예외 발생") - void convertIntToList_exception(){ + void stringToIntegerList_exception(){ // given String input = "문자열"; @@ -99,42 +164,5 @@ void stringParseIntInputNotNumber2_exception(){ assertEquals(fail.getMessage(), ErrorMessages.INVALID_INPUT_NUMBER); } - @Test - @DisplayName("문자열 길이 체크") - void checkLength(){ - // given - String input = "123"; - - // when - // then - assertDoesNotThrow(() -> BaseballUtils.checkLength(input)); - } - - @Test - @DisplayName("공백이 포함된 문자열 길이 체크") - void checkLengthContainBlank(){ - // given - String input = "12 3 "; - - // when - assertDoesNotThrow(() -> BaseballUtils.checkLength(input)); - } - - @Test - @DisplayName("문자열 길이 체크로 3자리 이상 문자열 입력 예외 발생") - void checkLengthFalse(){ - // given - String input = "12345"; - - // when - IllegalArgumentException fail = assertThrows(IllegalArgumentException.class, () -> BaseballUtils.checkLength(input)); - - // then - assertEquals(fail.getMessage(), ErrorMessages.INVALID_INPUT_LENGTH); - } - - - - } From f6155bf8f30f4abf22a779f5f13c1494ac1c9cd2 Mon Sep 17 00:00:00 2001 From: Heesoo Date: Fri, 25 Oct 2024 15:32:06 +0900 Subject: [PATCH 7/7] =?UTF-8?q?[refactor]=20=EB=B6=88=ED=95=84=EC=9A=94?= =?UTF-8?q?=ED=95=9C=20=EC=BD=94=EB=93=9C=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/BaseballApplication.java | 15 ++++++++---- src/main/java/study/BaseballGame.java | 15 ------------ src/main/java/study/BaseballGenerator.java | 15 ------------ .../study/{ => model}/BaseballAnswer.java | 8 ++++--- src/test/java/study/BaseballGameTest.java | 23 ------------------ .../java/study/BaseballGeneratorTest.java | 16 ------------- .../study/{ => model}/BaseballAnswerTest.java | 24 ++----------------- 7 files changed, 17 insertions(+), 99 deletions(-) delete mode 100644 src/main/java/study/BaseballGame.java delete mode 100644 src/main/java/study/BaseballGenerator.java rename src/main/java/study/{ => model}/BaseballAnswer.java (78%) delete mode 100644 src/test/java/study/BaseballGameTest.java delete mode 100644 src/test/java/study/BaseballGeneratorTest.java rename src/test/java/study/{ => model}/BaseballAnswerTest.java (54%) diff --git a/src/main/java/BaseballApplication.java b/src/main/java/BaseballApplication.java index 3b0d8ae..bf1427c 100644 --- a/src/main/java/BaseballApplication.java +++ b/src/main/java/BaseballApplication.java @@ -1,11 +1,16 @@ -import study.BaseballGame; -import study.BaseballScore; +import study.model.Baseball; +import study.model.BaseballAnswer; +import study.model.BaseballScore; +import study.model.BaseballUser; +import study.view.ResultView; public class BaseballApplication { public static void main(String[] args) { - BaseballGame baseballGame = new BaseballGame(); - BaseballScore score = baseballGame.playGame("123"); + BaseballUser user = new BaseballUser("123"); + Baseball baseball = new Baseball(new BaseballScore(), new BaseballAnswer()); + BaseballScore score = baseball.play(user); - System.out.println(score.printScore()); + ResultView resultView = new ResultView(); + System.out.println(resultView.printScore(score)); } } diff --git a/src/main/java/study/BaseballGame.java b/src/main/java/study/BaseballGame.java deleted file mode 100644 index cce9f40..0000000 --- a/src/main/java/study/BaseballGame.java +++ /dev/null @@ -1,15 +0,0 @@ -package study; - -import java.util.List; - -public class BaseballGame { - - public BaseballScore playGame(String input) { - BaseballUtils.checkLength(input); - List userInput = BaseballUtils.stringToIntegerList(input); - - Baseball baseball = new Baseball(new BaseballScore(), new BaseballAnswer()); - return baseball.play(userInput); - - } -} diff --git a/src/main/java/study/BaseballGenerator.java b/src/main/java/study/BaseballGenerator.java deleted file mode 100644 index 0c866d4..0000000 --- a/src/main/java/study/BaseballGenerator.java +++ /dev/null @@ -1,15 +0,0 @@ -//package study; -// -//import study.message.ErrorMessages; -// -//import java.util.List; -// -//public class BaseballGenerator { -// -// private static final int LIST_SIZE = 3; -// -// public List createUserInputList(String input){ -// -// } -// -//} diff --git a/src/main/java/study/BaseballAnswer.java b/src/main/java/study/model/BaseballAnswer.java similarity index 78% rename from src/main/java/study/BaseballAnswer.java rename to src/main/java/study/model/BaseballAnswer.java index e5a9aa1..8841993 100644 --- a/src/main/java/study/BaseballAnswer.java +++ b/src/main/java/study/model/BaseballAnswer.java @@ -1,10 +1,12 @@ -package study; +package study.model; + +import study.utils.BaseballUtils; import java.util.ArrayList; import java.util.List; public class BaseballAnswer { - private static final int LIST_SIZE = 3; + private static final int NUMBER_OF_ANSWER = 3; private final List answer; public BaseballAnswer(){ @@ -17,7 +19,7 @@ public List getAnswer() { } public void createAnswer() { - while(answer.size() < LIST_SIZE){ + while(answer.size() < NUMBER_OF_ANSWER){ addUniqueNumber(answer); } diff --git a/src/test/java/study/BaseballGameTest.java b/src/test/java/study/BaseballGameTest.java deleted file mode 100644 index 04d35b3..0000000 --- a/src/test/java/study/BaseballGameTest.java +++ /dev/null @@ -1,23 +0,0 @@ -package study; - -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.assertNotNull; - -public class BaseballGameTest { - - @Test - @DisplayName("게임 진행을 위한 필요 데이터 생성") - void plaGame(){ - // given - String userInput = "123"; - - // when - BaseballGame game = new BaseballGame(); - BaseballScore score = game.playGame(userInput); - - // then - assertNotNull(score); - } -} diff --git a/src/test/java/study/BaseballGeneratorTest.java b/src/test/java/study/BaseballGeneratorTest.java deleted file mode 100644 index b32e365..0000000 --- a/src/test/java/study/BaseballGeneratorTest.java +++ /dev/null @@ -1,16 +0,0 @@ -package study; - -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; -import study.message.ErrorMessages; - -import java.util.List; - -import static org.junit.jupiter.api.Assertions.*; - -public class BaseballGeneratorTest { - - - - -} diff --git a/src/test/java/study/BaseballAnswerTest.java b/src/test/java/study/model/BaseballAnswerTest.java similarity index 54% rename from src/test/java/study/BaseballAnswerTest.java rename to src/test/java/study/model/BaseballAnswerTest.java index 4ea24a8..2dd37a8 100644 --- a/src/test/java/study/BaseballAnswerTest.java +++ b/src/test/java/study/model/BaseballAnswerTest.java @@ -1,4 +1,4 @@ -package study; +package study.model; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -12,7 +12,7 @@ public class BaseballAnswerTest { @Test @DisplayName("정답 생성") - void getAnswer(){ + void createAnswer(){ // given BaseballAnswer baseballAnswer = new BaseballAnswer(); @@ -27,24 +27,4 @@ void getAnswer(){ assertNotEquals(response.get(1), response.get(2)); } - @Test - @DisplayName("리스트 내 중복 체크 후 중복값 없어 리스트에 추가") - void addUniqueNumber() { - // given - List answer = new ArrayList<>(); - answer.add(1); - answer.add(2); - - BaseballAnswer baseballAnswer = new BaseballAnswer(); - - // when - baseballAnswer.addUniqueNumber(answer); - - // then - assertEquals(answer.size(), 3); - assertEquals(answer.get(0), 1); - assertEquals(answer.get(1), 2); - assertNotNull(answer.get(2)); - - } }