-
Notifications
You must be signed in to change notification settings - Fork 7
[10주차] ysy #59
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?
[10주차] ysy #59
Changes from all commits
8655208
23a3ddf
3e7c230
b0bccbe
1949d37
0b1547d
9921187
1ba67c4
adc0553
860e5b4
741700c
d3a549d
743df51
91229be
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
|
|
||
| # Created by .ignore support plugin (hsz.mobi) | ||
| ### Java template | ||
| # Compiled class file | ||
| *.class | ||
|
|
||
| # Log file | ||
| *.log | ||
|
|
||
| # BlueJ files | ||
| *.ctxt | ||
|
|
||
| # Mobile Tools for Java (J2ME) | ||
| .mtj.tmp/ | ||
|
|
||
| # Package Files # | ||
| *.jar | ||
| *.war | ||
| *.nar | ||
| *.ear | ||
| *.zip | ||
| *.tar.gz | ||
| *.rar | ||
|
|
||
| # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml | ||
| hs_err_pid* | ||
|
|
||
| .DS_Store | ||
| .gradle | ||
| /build/ | ||
| !gradle/wrapper/gradle-wrapper.jar | ||
| /out/ | ||
| /target/ | ||
|
|
||
| ### STS ### | ||
| .apt_generated | ||
| .classpath | ||
| .factorypath | ||
| .project | ||
| .settings | ||
| .springBeans | ||
| bin/ | ||
|
|
||
| ### IntelliJ IDEA ### | ||
| .idea | ||
| *.iws | ||
| *.iml | ||
| *.ipr |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import utils.GameUtils; | ||
|
|
||
| public class Application { | ||
| public static void main(String[] args) { | ||
| GameUtils.run(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package inpututils; | ||
|
|
||
| public class InputValidator { | ||
| private static final int MAX_NAME = 5; | ||
|
|
||
| public static void nameValidator(String name) { | ||
| nullCheck(name); | ||
| lengthCheck(name); | ||
| } | ||
|
|
||
| public static void nullCheck(String name) { | ||
| if ("".equals(name)) { | ||
| throw new IllegalArgumentException("[ERROR]자동차 이름을 입력해주세요."); | ||
| } | ||
| } | ||
|
|
||
| public static void lengthCheck(String name) { | ||
| if (name.length() > MAX_NAME) { | ||
| throw new IllegalArgumentException("[ERROR]자동차 이름은 5자 이내여야합니다."); | ||
| } | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package inpututils; | ||
|
|
||
| import java.util.Scanner; | ||
|
|
||
| public class InputView { | ||
| private static final Scanner SCANNER = new Scanner(System.in); | ||
|
|
||
| public static String getCarName() { | ||
| System.out.print("경주 할 자동차 이름 : "); | ||
| return SCANNER.nextLine(); | ||
| } | ||
|
|
||
| public static int getTryNumber() { | ||
| System.out.print("시도할 횟수 : "); | ||
| try { | ||
| return SCANNER.nextInt(); | ||
| } catch (NumberFormatException e) { | ||
| throw new NumberFormatException("[ERROR] 시도 횟수는 숫자여야 한다."); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package outpututils; | ||
|
|
||
| import java.util.ArrayList; | ||
|
|
||
| public class OutputView { | ||
| private OutputView() { | ||
| } | ||
| public static void printCarName(String name) { | ||
| System.out.print(name + " : "); | ||
| } | ||
|
|
||
| public static void printMove() { | ||
| System.out.print("-"); | ||
| } | ||
|
|
||
| public static void printWinner(ArrayList<String> winner) { | ||
| //TODO:마지막에 , 나오지 않도록 수정 필요 | ||
| winner.stream() | ||
| .forEach(w -> System.out.print(w + ",")); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package racingcar; | ||
|
|
||
| import utils.GameUtils; | ||
|
|
||
| public class Car { | ||
| private final String name; | ||
| private int position = 0; | ||
|
|
||
| public Car(String name) { | ||
| InputValidator.nameValidator(name); | ||
| this.name = name; | ||
dustndus8 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| public String getName(){ | ||
| return name; | ||
| } | ||
|
|
||
| public int plusPosition(){ | ||
| return position++; | ||
| } | ||
|
|
||
| public int getPosition() { | ||
| return position; | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package racingcar; | ||
|
|
||
| import outpututils.OutputView; | ||
| import utils.RandomUtils; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class Cars { | ||
| private static final int START_NUMBER = 0; | ||
| private static final int END_NUMBER = 9; | ||
| private static final int BOUNDARY_NUMBER = 4; | ||
| private static List<Car> carList; | ||
| private static ArrayList<String> winner; | ||
|
|
||
dustndus8 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public Cars(List<Car> carList) { | ||
| this.carList = carList; | ||
| } | ||
|
|
||
| public static boolean isMove(int randomNumber) { | ||
| if (randomNumber >= BOUNDARY_NUMBER) { | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| public static void playGame() { | ||
| for (int j = 0; j < carList.size(); j++) { | ||
| OutputView.printCarName(carList.get(j).getName()); | ||
|
|
||
| if (isMove(RandomUtils.nextInt(START_NUMBER, END_NUMBER))) { | ||
| carList.get(j).plusPosition(); | ||
| } | ||
| for (int k = 0; k < carList.get(j).getPosition(); k++) { | ||
| OutputView.printMove(); | ||
| } | ||
| System.out.println(); | ||
| } | ||
| } | ||
|
|
||
| public static int getMax() { | ||
| int max = carList.stream() | ||
| .mapToInt(Car::getPosition) | ||
| .max() | ||
| .getAsInt(); | ||
| return max; | ||
| } | ||
|
|
||
| public static void setWinnerList(int max) { | ||
| winner = new ArrayList<String>(); | ||
| carList.stream() | ||
| .filter(s -> s.getPosition() == max) | ||
| .filter(s -> winner.add(s.getName())) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| public static ArrayList<String> getWinnerList() { | ||
| return winner; | ||
| } | ||
| public static void playGames(int inputCount){ | ||
| for (int i = 0; i < inputCount; i++) { | ||
| playGame(); | ||
| System.out.println(); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package utils; | ||
|
|
||
| import inpututils.InputView; | ||
| import outpututils.OutputView; | ||
| import racingcar.Car; | ||
| import racingcar.Cars; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class GameUtils { | ||
|
|
||
| public static List<Car> makeCarList(String[] splitResult) { | ||
| return Arrays.stream(splitResult) | ||
| .map(Car::new) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| public static void run() { | ||
dustndus8 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| String inputName = InputView.getCarName(); | ||
| int inputCount = InputView.getTryNumber(); | ||
| String[] splitResult = SplitString.splitString(inputName); | ||
PandaHun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| Cars cars = new Cars(makeCarList(splitResult)); | ||
|
|
||
| Cars.playGames(inputCount); | ||
| Cars.setWinnerList(Cars.getMax()); | ||
| OutputView.printWinner(Cars.getWinnerList()); | ||
|
|
||
| } | ||
| } | ||
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.
객체의 생성 부분에 들어가는게 좋지않을까요?