diff --git a/src/main/java/BaseballApplication.java b/src/main/java/BaseballApplication.java new file mode 100644 index 0000000..bf1427c --- /dev/null +++ b/src/main/java/BaseballApplication.java @@ -0,0 +1,16 @@ +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) { + BaseballUser user = new BaseballUser("123"); + Baseball baseball = new Baseball(new BaseballScore(), new BaseballAnswer()); + BaseballScore score = baseball.play(user); + + ResultView resultView = new ResultView(); + System.out.println(resultView.printScore(score)); + } +} 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/main/java/study/model/Baseball.java b/src/main/java/study/model/Baseball.java new file mode 100644 index 0000000..2e4f1d2 --- /dev/null +++ b/src/main/java/study/model/Baseball.java @@ -0,0 +1,35 @@ +package study.model; + +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, BaseballAnswer baseballAnswer){ + this.score = score; + this.answer = baseballAnswer.getAnswer(); + } + + public BaseballScore play(BaseballUser user) { + System.out.println(answer.toString()); + for(int i=0; i answer; + + public BaseballAnswer(){ + this.answer = new ArrayList<>(); + createAnswer(); + } + + public List getAnswer() { + return answer; + } + + public void createAnswer() { + while(answer.size() < NUMBER_OF_ANSWER){ + 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/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/model/BaseballUser.java b/src/main/java/study/model/BaseballUser.java new file mode 100644 index 0000000..3635167 --- /dev/null +++ b/src/main/java/study/model/BaseballUser.java @@ -0,0 +1,35 @@ +package study.model; + +import study.utils.BaseballUtils; + +import java.util.List; + +public class BaseballUser { + + private final List 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/main/java/study/utils/BaseballUtils.java b/src/main/java/study/utils/BaseballUtils.java new file mode 100644 index 0000000..cf88b6c --- /dev/null +++ b/src/main/java/study/utils/BaseballUtils.java @@ -0,0 +1,58 @@ +package study.utils; + +import study.message.ErrorMessages; + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +public class BaseballUtils { + + 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_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 = stringToArrays(input); + + for (String s : strArr) { + list.add(BaseballUtils.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; + } + +} 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/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"); - } -} diff --git a/src/test/java/study/model/BaseballAnswerTest.java b/src/test/java/study/model/BaseballAnswerTest.java new file mode 100644 index 0000000..2dd37a8 --- /dev/null +++ b/src/test/java/study/model/BaseballAnswerTest.java @@ -0,0 +1,30 @@ +package study.model; + +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 createAnswer(){ + // 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)); + } + +} diff --git a/src/test/java/study/model/BaseballScoreTest.java b/src/test/java/study/model/BaseballScoreTest.java new file mode 100644 index 0000000..b20d23e --- /dev/null +++ b/src/test/java/study/model/BaseballScoreTest.java @@ -0,0 +1,69 @@ +package study.model; + +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 + BaseballScore score = new BaseballScore(); + + // when + score.addStrike(); + + // then + assertEquals(score.getStrike(), 1); + } + + @Test + @DisplayName("볼 점수 증가") + void addBall() { + // given + BaseballScore score = new BaseballScore(); + + // when + score.addBall(); + + // then + assertEquals(score.getBall(), 1); + } + + @Test + @DisplayName("3스트라이크인 경우 true 반환") + 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스트라이크가 아닌 경우 false 반환") + 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/model/BaseballTest.java b/src/test/java/study/model/BaseballTest.java new file mode 100644 index 0000000..29d008a --- /dev/null +++ b/src/test/java/study/model/BaseballTest.java @@ -0,0 +1,135 @@ +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; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class BaseballTest { + + @Test + @DisplayName("3스트라이크") + void playStrike(){ + // given + 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(user); + + // then + assertEquals(response.getStrike(), 3); + } + + @Test + @DisplayName("1스트라이크 2볼") + void play1Strike2Ball(){ + // given + BaseballAnswer baseballAnswer = new BaseballAnswer(); + List answer = baseballAnswer.getAnswer(); + + 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(user); + + // then + assertEquals(response.getStrike(), 1); + assertEquals(response.getBall(), 2); + } + + @Test + @DisplayName("3볼") + void play3Ball(){ + // given + BaseballAnswer baseballAnswer = new BaseballAnswer(); + List answer = baseballAnswer.getAnswer(); + + 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(user); + + // then + assertEquals(response.getBall(), 3); + } + + + @Test + @DisplayName("답 확인 : 스트라이크") + void calculateScoreStrike(){ + // given + BaseballAnswer baseballAnswer = new BaseballAnswer(); + List answer = baseballAnswer.getAnswer(); + + BaseballScore score = new BaseballScore(); + Baseball baseball = new Baseball(score, baseballAnswer); + + int index = 2; + int input = answer.get(2); + + + // when + baseball.calculateScore(index, input); + + // then + assertEquals(score.getStrike(), 1); + assertEquals(score.getBall(), 0); + } + + @Test + @DisplayName("답 확인 : 볼") + void calculateScoreBall(){ + // given + BaseballAnswer baseballAnswer = new BaseballAnswer(); + List answer = baseballAnswer.getAnswer(); + + BaseballScore score = new BaseballScore(); + Baseball baseball = new Baseball(score, baseballAnswer); + + int index = 1; + int input = answer.get(2); + + // when + baseball.calculateScore(index, input); + + // then + assertEquals(score.getStrike(), 0); + assertEquals(score.getBall(), 1); + } + + @Test + @DisplayName("답 확인 : 낫싱") + void calculateScoreNothing(){ + // given + BaseballScore score = new BaseballScore(); + Baseball baseball = new Baseball(score, new BaseballAnswer()); + + int index = 1; + int input = 10; + + // when + baseball.calculateScore(index, input); + + // then + assertEquals(score.getStrike(), 0); + 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); + + } + +} diff --git a/src/test/java/study/utils/BaseballUtilsTest.java b/src/test/java/study/utils/BaseballUtilsTest.java new file mode 100644 index 0000000..a3a9273 --- /dev/null +++ b/src/test/java/study/utils/BaseballUtilsTest.java @@ -0,0 +1,168 @@ +package study.utils; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.RepeatedTest; +import org.junit.jupiter.api.Test; +import study.message.ErrorMessages; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +public class BaseballUtilsTest { + + @RepeatedTest(10) + @DisplayName("랜덤 숫자 생성") + void getRandomNumber() { + // given + // when + int response = BaseballUtils.getRandomNumber(); + + // then + assertTrue(response > 0); + assertTrue(response < 10); + + } + + @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 stringToIntegerList(){ + // 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 stringToIntegerList_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); + } + + +} 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, "낫싱"); + } +}