-
Notifications
You must be signed in to change notification settings - Fork 33
[스프링 목요일 2팀] (배성현) 미션 제출합니다. #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package controller; | ||
|
|
||
| import view.InputView; | ||
| import view.ResultView; | ||
| import domain.RacingGame; | ||
|
|
||
| public class RacingMain { | ||
| public static void main(final String... args) { | ||
| final var carNames = InputView.getCarNames(); | ||
| final var tryCount = InputView.getTryCount(); | ||
|
|
||
| final var racingGame = new RacingGame(carNames, tryCount); | ||
| System.out.print("\n실행 결과"); | ||
| racingGame.race(); | ||
|
|
||
| ResultView.printWinners(racingGame.getWinners()); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 컨트롤러로 분리할수 있다면 좋을 것 같습니다 |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package domain; | ||
|
|
||
|
|
||
| import org.kokodak.Randoms; | ||
| import view.ExceptionMessage; | ||
| import view.ResultView; | ||
|
|
||
| import java.util.*; | ||
|
|
||
| public class RacingGame { | ||
| private final int tryCount; | ||
| List<String> carList; | ||
| public Map<String, Integer> moveCnt; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Map 과 같은 객체는 따로 dto나 랩핑 클래스를 만들어서 사용하시는게 더 편리합니다. |
||
|
|
||
| public RacingGame(List<String> carNames, int tryCount) { | ||
| this.carList = carNames; | ||
| this.tryCount = tryCount; | ||
| } | ||
|
|
||
| public void race() { | ||
| moveCnt = setMoveCnt(carList); | ||
| for (int i = 0; i < tryCount; i++) { | ||
| System.out.println(); | ||
| setRandomMove(); | ||
| ResultView.printCarMove(moveCnt); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| private Map<String, Integer> setMoveCnt(List<String> carList) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cnt대신 count 로 길게 작성하셔도 됩니다. |
||
| Map<String, Integer> moveCnt = new HashMap<>(); | ||
| for (String s : carList) { | ||
| moveCnt.put(s, 0); | ||
| } | ||
| return moveCnt; | ||
| } | ||
|
|
||
| private void setRandomMove() { | ||
| for (int i = 0; i < carList.size(); i++) { | ||
| int move = Randoms.pickNumberInRange(0, 9); | ||
| canMove(move, i); | ||
| } | ||
| } | ||
|
|
||
| private void canMove(int move, int i) { | ||
| if (move >= 4) { | ||
| moveCnt.put(carList.get(i), moveCnt.get(carList.get(i)) + 1); | ||
| } | ||
| } | ||
|
|
||
| public List<String> getWinners() { | ||
| System.out.println(); | ||
| for (String s : moveCnt.keySet()) { | ||
| String bars = "-".repeat(moveCnt.get(s)); | ||
| System.out.println(s + " : " + bars); | ||
| } | ||
| return findWinners(moveCnt); | ||
| } | ||
|
|
||
| private List<String> findWinners(Map<String, Integer> moveCnt) { | ||
| int max = Collections.max(moveCnt.values()); | ||
| return moveCnt.entrySet().stream() | ||
| .filter(m -> m.getValue() == max) | ||
| .map(Map.Entry::getKey) | ||
| .toList(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package view; | ||
|
|
||
| public class ExceptionMessage { | ||
|
|
||
| // 1. 자동차 이름 5자 이상 | ||
| public static void isInvalidNameLength() { | ||
| System.out.println("[ERROR] 자동차의 이름은 1자 이상 5자 이하여야 한다."); | ||
| } | ||
|
|
||
| // 2. 시도 횟수 음수 혹은 0 | ||
| public static void isInvalidTryCount() { | ||
| System.out.println("[ERROR] 시도 횟수는 양수여야 한다."); | ||
| } | ||
|
|
||
| // 3. 자동차 이름 중복 확인 | ||
| public static void isDuplicateCarName() { | ||
| System.out.println("[ERROR] 자동차 이름은 중복되지 않아야 한다."); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package view; | ||
|
|
||
| import java.util.HashSet; | ||
| import java.util.List; | ||
|
|
||
| public class InputValidate { | ||
|
|
||
|
|
||
| public static void isValidCarName(List<String> carList) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is 접두사는 bool 객체를 반환하는 상황에서 작성되는것이 네이밍 컨벤션입니다 |
||
| for (String s : carList) { | ||
| isValidCarNameLength(s); | ||
| } | ||
| } | ||
|
|
||
| public static void isValidCarNameLength(String carName) { | ||
| if (carName.length() < 1 || carName.length() > 5) { | ||
| ExceptionMessage.isInvalidNameLength(); | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| } | ||
|
|
||
| public static void isDuplicateCarName(List<String> carList) { | ||
| HashSet<String> names = new HashSet<>(carList); | ||
| System.out.println(); | ||
| if (names.size() != carList.size()) { | ||
| ExceptionMessage.isDuplicateCarName(); | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| package view; | ||
|
|
||
| import org.kokodak.Console; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
|
|
||
| import static view.InputValidate.isDuplicateCarName; | ||
| import static view.InputValidate.isValidCarName; | ||
|
|
||
| public class InputView { | ||
|
|
||
| public static List<String> getCarNames() { | ||
| System.out.println("경주할 자동차 이름을 입력하세요(이름은 쉼표(,)를 기준으로 구분)."); | ||
| return splitCarNames(Console.readLine()); | ||
| } | ||
|
|
||
| public static List<String> splitCarNames(String carNames) { | ||
| List<String> list; | ||
| list = Arrays.stream(carNames.split(",")) | ||
| .map(String::trim) | ||
| .toList(); | ||
| isValidCarName(list); | ||
| isDuplicateCarName(list); | ||
| return list; | ||
| } | ||
| // | ||
| // private static void isValidCarName(List<String> carList) { | ||
| // for (String s : carList) { | ||
| // isValidCarNameLength(s); | ||
| // } | ||
| // } | ||
| // | ||
| // private static void isValidCarNameLength(String carName) { | ||
| // if (carName.length() < 1 || carName.length() > 5) { | ||
| // ExceptionMessage.isInvalidNameLength(); | ||
| // throw new IllegalArgumentException(); | ||
| // } | ||
| // } | ||
| // | ||
| // private static void isDuplicateCarName(List<String> carList) { | ||
| // HashSet<String> names = new HashSet<>(carList); | ||
| // System.out.println("set size = " + names.size()); | ||
| // System.out.println("list size = " + carList.size()); | ||
| // System.out.println(); | ||
| // if (names.size() != carList.size()) { | ||
| // ExceptionMessage.isDuplicateCarName(); | ||
| // throw new IllegalArgumentException(); | ||
| // } | ||
| // } | ||
|
|
||
|
|
||
| public static int getTryCount() { | ||
| System.out.println("시도할 회수는 몇회인가요?"); | ||
| String str = Console.readLine(); | ||
| int tryCount = Integer.parseInt(str); | ||
| isValidTryCount(tryCount); | ||
| return tryCount; | ||
| } | ||
|
|
||
| private static void isValidTryCount(int tryCount) { | ||
| if (tryCount < 1) { | ||
| ExceptionMessage.isInvalidTryCount(); | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package view; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| public class ResultView { | ||
|
|
||
| public static void printCarMove(Map<String, Integer> moveCnt) { | ||
| for (String s : moveCnt.keySet()) { | ||
| String bars = "-".repeat(moveCnt.get(s)); | ||
| System.out.println(s + " : " + bars); | ||
| } | ||
| } | ||
| public static void printWinners(List<String> winners) { | ||
| System.out.println(); | ||
| for (int i = 0; i < winners.size() - 1; i++) { | ||
| System.out.print(winners.get(i) + ", "); | ||
| } | ||
| System.out.println(winners.get(winners.size() - 1) + "가 최종 우승했습니다."); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package domain; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| class RacingGameTest { | ||
|
|
||
|
|
||
|
|
||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
좋습니다
전체적으로 구조를 잘 짜시는 것 같아요
화이팅입니다