diff --git a/docs/README.md b/docs/README.md index e69de29bb2d..0f2e4419402 100644 --- a/docs/README.md +++ b/docs/README.md @@ -0,0 +1,28 @@ +# πŸš—Racing Car Game κ΅¬ν˜„ κΈ°λŠ₯ λͺ©λ‘ # + +### μ‚¬μš©μžκ°€ κ²½μ£Όν•  μžλ™μ°¨ 이름 μž…λ ₯ ### +- μžλ™μ°¨ 이름을 ","으둜 κ΅¬λΆ„ν•˜μ—¬ μž…λ ₯ν•œλ‹€
+- κΈ€μž μˆ˜κ°€ 5자 μ΄ν•˜μ—¬μ•Όν•œλ‹€. 아닐 μ‹œ μ˜ˆμ™Έμ²˜λ¦¬
+### μ‚¬μš©μžκ°€ μ‹œλ„ν•  횟수λ₯Ό μž…λ ₯ ### + +### 전진 할지 멈좀 할지 κ΅¬ν˜„ ### +- 0-9의 λ¬΄μž‘μœ„ 값을 κ΅¬ν•œλ‹€.
+- κ·Έ 값이 4이상이라면 전진
+- μ•„λ‹ˆλΌλ©΄ λ©ˆμΆ€ν•œλ‹€.
+### 1회 끝날 λ•Œλ§ˆλ‹€ μ‹€ν–‰ κ²°κ³Όλ₯Ό λ³΄μ—¬μ€Œ(μ°¨ 이름과 ν•¨κ»˜) ### +- 각 μžλ™μ°¨ λ§ˆλ‹€ "-" 갯수λ₯Ό 좜λ ₯함
+### 우승자(곡동가λŠ₯)을 좜λ ₯함 ### +- 곡동 우승일 경우 "," 으둜 κ΅¬λΆ„ν•˜μ—¬ 좜λ ₯ν•΄μ€€λ‹€
+- λˆ„κ°€ κ°€μž₯λ§Žμ€ 전진을 ν–ˆλŠ”μ§€ 확인
+## TEST CODE κ΅¬ν˜„ ## +- μ •μƒμ μœΌλ‘œ μ°¨ 이름을 μž‘μ„±ν•  경우 +- 차이름이 5자 초과인 경우 Test +- 차이름이 곡백인 경우 Test +- μ‹œλ„ 횟수 정상적인 값을 μž…λ ₯ν•  경우 +- μ‹œλ„ 횟수 μ •μˆ˜ 값이 μž…λ ₯λ˜μ—ˆλŠ”μ§€ Test +- μ‹œλ„ 횟수 1 μ΄μƒμ˜ 값이 μž…λ ₯λ˜μ—ˆλŠ”μ§€ Test + + + + + diff --git a/src/main/java/racingcar/Application.java b/src/main/java/racingcar/Application.java index a17a52e7242..53f77d56e09 100644 --- a/src/main/java/racingcar/Application.java +++ b/src/main/java/racingcar/Application.java @@ -1,7 +1,10 @@ package racingcar; +import racingcar.Controller.RacingCarController; + public class Application { - public static void main(String[] args) { - // TODO: ν”„λ‘œκ·Έλž¨ κ΅¬ν˜„ + public static void main(String[] args) throws IllegalAccessException { + RacingCarController racingCarController = new RacingCarController(); + racingCarController.run(); } } diff --git a/src/main/java/racingcar/Controller/RacingCarController.java b/src/main/java/racingcar/Controller/RacingCarController.java new file mode 100644 index 00000000000..7f4ff13b5f4 --- /dev/null +++ b/src/main/java/racingcar/Controller/RacingCarController.java @@ -0,0 +1,36 @@ +package racingcar.Controller; + +import racingcar.Model.Car; +import racingcar.Service.JudgeWinnerResult; +import racingcar.Service.UserInputTryNumber; +import racingcar.Util.RandomAdvanceOrStop; +import racingcar.Service.UserInputCarName; +import racingcar.View.OutputView; + +public class RacingCarController { + Car cars = new Car(); + UserInputCarName userInputCarName = new UserInputCarName(); + UserInputTryNumber userInputTryNumber = new UserInputTryNumber(); + OutputView outputView = new OutputView(); + JudgeWinnerResult judgeWinnerResult = new JudgeWinnerResult(); + RandomAdvanceOrStop randomAdvanceOrstop = new RandomAdvanceOrStop(); + + public void run() throws IllegalAccessException { + startCar(); //μž…λ ₯ + executionResult(); //μ‹€ν–‰κ²°κ³Ό + winnerResult(); //μ΅œμ’… 우승자 κ²°κ³Ό + } + + private void startCar() throws IllegalAccessException { + userInputCarName.userInputCarName(); + userInputTryNumber.userInputTryNumber(); + } + private void executionResult() throws IllegalAccessException { //μ‹€ν–‰κ²°κ³Ό + randomAdvanceOrstop.judgeAdvanceOrStop(); + } + + public void winnerResult() throws IllegalAccessException { //μ΅œμ’… 우승자 κ²°κ³Ό + outputView.printWinnerResult(); + judgeWinnerResult.judgeWinner(); + } +} diff --git a/src/main/java/racingcar/Infomation/ErrorInfomation.java b/src/main/java/racingcar/Infomation/ErrorInfomation.java new file mode 100644 index 00000000000..29498ff858f --- /dev/null +++ b/src/main/java/racingcar/Infomation/ErrorInfomation.java @@ -0,0 +1,7 @@ +package racingcar.Infomation; + +public class ErrorInfomation { + public static final String CAR_NAME_LIMIT_ERROR_MESSAGE = "μžλ™μ°¨ 이름은 5κΈ€μžλ₯Ό λ„˜κΈΈ 수 μ—†μŠ΅λ‹ˆλ‹€."; + public static final String CAR_NAME_DUPLICATED_ERROR_MESSAGE = "μ€‘λ³΅λœ μžλ™μ°¨ 이름이 μ‘΄μž¬ν•©λ‹ˆλ‹€."; + public static final String TRY_NUMBER_COUNT_ERROR_MESSAGE = "μ‹œλ„ νšŸμˆ˜λŠ” 1λΆ€ν„° 9κΉŒμ§€μž…λ‹ˆλ‹€."; +} diff --git a/src/main/java/racingcar/Infomation/NumberConstant.java b/src/main/java/racingcar/Infomation/NumberConstant.java new file mode 100644 index 00000000000..b26449625ac --- /dev/null +++ b/src/main/java/racingcar/Infomation/NumberConstant.java @@ -0,0 +1,9 @@ +package racingcar.Infomation; + +public class NumberConstant { //λ§€μ§λ„˜λ²„ λͺ¨μ•„λ‘  + public final static int MIN_RANDOM_NUMBER = 0; + public final static int MAX_RANDOM_NUMBER = 9; + public final static int CAR_ADVANCE_NUMBER = 4; + public final static int MAX_CAR_NAME_SIZE = 5; + +} diff --git a/src/main/java/racingcar/Infomation/PrintInputMessage.java b/src/main/java/racingcar/Infomation/PrintInputMessage.java new file mode 100644 index 00000000000..bef9f436d23 --- /dev/null +++ b/src/main/java/racingcar/Infomation/PrintInputMessage.java @@ -0,0 +1,11 @@ +package racingcar.Infomation; + +public class PrintInputMessage { + public static void printInputCarName(){ + System.out.println("κ²½μ£Όν•  μžλ™μ°¨ 이름을 μž…λ ₯ν•˜μ„Έμš”.(이름은 μ‰Όν‘œ(,) κΈ°μ€€μœΌλ‘œ ꡬ뢄"); + } + + public static void printTryNumber(){ + System.out.println("μ‹œλ„ν•  νšŒμˆ˜λŠ” λͺ‡νšŒμΈκ°€μš”?"); + } +} diff --git a/src/main/java/racingcar/Model/Car.java b/src/main/java/racingcar/Model/Car.java new file mode 100644 index 00000000000..9e52fa7839b --- /dev/null +++ b/src/main/java/racingcar/Model/Car.java @@ -0,0 +1,37 @@ +package racingcar.Model; + +public class Car { + public int getTryNumber() { + return tryNumber; + } + + public void setTryNumber(int tryNumber) { + this.tryNumber = tryNumber; + } + + public int tryNumber; + + public String getCarName() { + return carName; + } + + public void setCarName(String carName) { + this.carName = carName; + } + + public String carName; //carSize 크기의 λ¬Έμžμ—΄μ„ 담을 수 μžˆλŠ” λ°°μ—΄ μ„ μ–Έ 및 생성 + + + + public String[] getCarList() { + return carList; + } + + public void setCarList(String[] carList) { + this.carList = carList; + } + + public String[] carList; //μ‚¬μš©μžκ°€ μž…λ ₯ν•œ μ°¨ 이름 리슀트 + + +} diff --git a/src/main/java/racingcar/Service/JudgeWinnerResult.java b/src/main/java/racingcar/Service/JudgeWinnerResult.java new file mode 100644 index 00000000000..bfa67602d9e --- /dev/null +++ b/src/main/java/racingcar/Service/JudgeWinnerResult.java @@ -0,0 +1,18 @@ +package racingcar.Service; + +import racingcar.Model.Car; + +import java.util.ArrayList; +import java.util.List; + +public class JudgeWinnerResult { + Car cars = new Car(); + + public String judgeWinner(){ + List winnerList = new ArrayList(); + + String[] WinnerList = new String[5]; + return String.join(", ",winnerList); + } + +} diff --git a/src/main/java/racingcar/Service/UserInputCarName.java b/src/main/java/racingcar/Service/UserInputCarName.java new file mode 100644 index 00000000000..b09360e0bda --- /dev/null +++ b/src/main/java/racingcar/Service/UserInputCarName.java @@ -0,0 +1,43 @@ +package racingcar.Service; + +import racingcar.Model.Car; + +import static camp.nextstep.edu.missionutils.Console.readLine; +import static racingcar.Infomation.ErrorInfomation.CAR_NAME_LIMIT_ERROR_MESSAGE; +import static racingcar.Infomation.NumberConstant.*; +import static racingcar.Infomation.PrintInputMessage.printInputCarName; + +public class UserInputCarName { //μ‚¬μš©μžκ°€ κ²½μ£Όν•  μžλ™μ°¨ 이름 μž…λ ₯ + + Car car = new Car(); + + public void userInputCarName() throws IllegalAccessException { + try { + printInputCarName(); //"κ²½μ£Όν•  μžλ™μ°¨ 이름을 μž…λ ₯ν•˜μ„Έμš”.(이름은 μ‰Όν‘œ(,) κΈ°μ€€μœΌλ‘œ ꡬ뢄" + car.setCarName(readLine()); + car.carList = car.getCarName().split(","); //","으둜 κ΅¬λΆ„ν•˜μ—¬ μž…λ ₯ + + if (!judgeCarNameLength()){ + throw new IllegalArgumentException(CAR_NAME_LIMIT_ERROR_MESSAGE); //"μžλ™μ°¨ 이름은 5κΈ€μžλ₯Ό λ„˜κΈΈ 수 μ—†μŠ΅λ‹ˆλ‹€." + } + + } catch (IllegalArgumentException e) { + System.out.println(car.carName); + } + } + + private boolean judgeCarNameLength() { + for (int i = 0; i < (car.carList).length; i++) { + if ((car.carList[i]).length() > MAX_CAR_NAME_SIZE) { //MAX_CAR_NAME_SIZE = 5; + return false; + } + } + return true; + } + /* + private boolean judgeCarNameLength() { + return List.of(car.carList).stream().allMatch(name -> name.length() <= MAX_CAR_NAME_SIZE); + } + μœ„μ˜ λ©”μ†Œλ“œλ₯Ό μ΄λ ‡κ²Œ κ°„κ²°ν•˜κ²Œλ„ μ‚¬μš©ν•  수 있음. + */ +} diff --git a/src/main/java/racingcar/Service/UserInputTryNumber.java b/src/main/java/racingcar/Service/UserInputTryNumber.java new file mode 100644 index 00000000000..509476c26a5 --- /dev/null +++ b/src/main/java/racingcar/Service/UserInputTryNumber.java @@ -0,0 +1,28 @@ +package racingcar.Service; + +import racingcar.Model.Car; + +import static camp.nextstep.edu.missionutils.Console.readLine; +import static racingcar.Infomation.ErrorInfomation.TRY_NUMBER_COUNT_ERROR_MESSAGE; +import static racingcar.Infomation.PrintInputMessage.printTryNumber; + +public class UserInputTryNumber { //μ‚¬μš©μžκ°€ μ‹œλ„ν•  횟수λ₯Ό μž…λ ₯ + Car car = new Car(); + public void userInputTryNumber() throws IllegalAccessException { + try { + printTryNumber(); //"μ‹œλ„ν•  νšŸμˆ˜λŠ” λͺ‡ νšŒμΈκ°€μš”?" + car.tryNumber = Integer.parseInt(readLine()); + + if (!judgeTryNumberLength()){ + throw new IllegalArgumentException(TRY_NUMBER_COUNT_ERROR_MESSAGE); + } + } catch (IllegalArgumentException e) { + System.out.println(car.tryNumber); + } + } + + private boolean judgeTryNumberLength() { + return car.tryNumber >= 1 && car.tryNumber <= 9; + } + +} diff --git a/src/main/java/racingcar/Util/RandomAdvanceOrStop.java b/src/main/java/racingcar/Util/RandomAdvanceOrStop.java new file mode 100644 index 00000000000..a345b763621 --- /dev/null +++ b/src/main/java/racingcar/Util/RandomAdvanceOrStop.java @@ -0,0 +1,42 @@ +package racingcar.Util; +import camp.nextstep.edu.missionutils.Randoms; +import racingcar.Model.Car; +import racingcar.Service.UserInputCarName; +import static racingcar.Infomation.NumberConstant.*; + +public class RandomAdvanceOrStop { + + public static final String HYPHEN = "-"; + UserInputCarName userInput = new UserInputCarName(); + Car car; + + public void judgeAdvanceOrStop() { //직진인지 λ©ˆμΆ€μΈμ§€ νŒλ‹¨ + for (int i = 0; i < car.getTryNumber(); i++) { + if (judgeadvanceOrStopNumber() >= CAR_ADVANCE_NUMBER){ + Move(); + } + printCarList(); + /* + pobi : + hol : + mol : + */ + } + } + + private int judgeadvanceOrStopNumber() { //1λΆ€ν„° 9κΉŒμ§€ λžœλ€ν•œ 수 κ΅¬ν•˜κΈ° + return Randoms.pickNumberInRange(MIN_RANDOM_NUMBER, MAX_RANDOM_NUMBER); + } + + private void Move() { + for (int i = 0; i < car.carList.length; i++) { + car.carList[i] = car.carList[i].concat(HYPHEN); + } + } + + private void printCarList() { + for (int i = 0; i < car.getCarList().length; i++) { + System.out.println(car.carList[i] + ":"); + } + } +} diff --git a/src/main/java/racingcar/View/InputView.java b/src/main/java/racingcar/View/InputView.java new file mode 100644 index 00000000000..73fdc50823c --- /dev/null +++ b/src/main/java/racingcar/View/InputView.java @@ -0,0 +1,8 @@ +package racingcar.View; + + + +public class InputView { + + +} diff --git a/src/main/java/racingcar/View/OutputView.java b/src/main/java/racingcar/View/OutputView.java new file mode 100644 index 00000000000..357f1a4c5e8 --- /dev/null +++ b/src/main/java/racingcar/View/OutputView.java @@ -0,0 +1,27 @@ +package racingcar.View; + +import racingcar.Model.Car; +import racingcar.Service.UserInputCarName; +import racingcar.Service.UserInputTryNumber; + +public class OutputView { //μ΅œμ’… κ²°κ³Ό 좜λ ₯ + + UserInputTryNumber userInputTryNumber = new UserInputTryNumber(); + public void printWinnerResult() throws IllegalAccessException { + for (int i = 0; i < car.tryNumber; i++) { + + } + System.out.println(); + } + + + public static void printCarPosition(int carPosition) { + for (int i = 0; i < carPosition; i++) { + System.out.print("-"); + } + } + + + + +} diff --git a/src/test/java/racingcar/ApplicationTest.java b/src/test/java/racingcar/ApplicationTest.java index 764ba4c627e..3769684b378 100644 --- a/src/test/java/racingcar/ApplicationTest.java +++ b/src/test/java/racingcar/ApplicationTest.java @@ -33,6 +33,10 @@ class ApplicationTest extends NsTest { @Override public void runMain() { - Application.main(new String[]{}); + try { + Application.main(new String[]{}); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } } } diff --git a/src/test/java/racingcar/Service/UserInputTest.java b/src/test/java/racingcar/Service/UserInputTest.java new file mode 100644 index 00000000000..813a56c89e5 --- /dev/null +++ b/src/test/java/racingcar/Service/UserInputTest.java @@ -0,0 +1,20 @@ +package racingcar.Service; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; + +class UserInputTest { + @DisplayName("μ‚¬μš©μžκ°€ μž…λ ₯") + @Test + void μžλ™μ°¨_이름에_λŒ€ν•œ_μ˜ˆμ™Έ_좜λ ₯ν…ŒμŠ€νŠΈ() { + //when + UserInputCarName userInput = new UserInputCarName(); + assertThatThrownBy(() -> { + userInput.userInputCarName(); + }) + .isInstanceOf(IllegalArgumentException.class); + } + + +} \ No newline at end of file diff --git a/src/test/java/racingcar/Util/RandomAdvanceOrstopTest.java b/src/test/java/racingcar/Util/RandomAdvanceOrstopTest.java new file mode 100644 index 00000000000..2c5c9939859 --- /dev/null +++ b/src/test/java/racingcar/Util/RandomAdvanceOrstopTest.java @@ -0,0 +1,17 @@ +package racingcar.Util; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import racingcar.Model.Car; + +class RandomAdvanceOrstopTest { + @DisplayName("λžœλ€μˆ«μžκ°€ 4이상일 λ•Œλ§Œ 전진, μ•„λ‹ˆλ©΄ 멈좀") + @Test + public void advanceOrStop() { + Car car = new Car(); + + assertFalse(car.); + + } + +} \ No newline at end of file