-
Notifications
You must be signed in to change notification settings - Fork 5
[미션1]구현 완료했습니다. 리뷰 요청드립니다. #9
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: asong
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| public class Car { | ||
| private String name; | ||
| private int position; | ||
|
|
||
| public Car(String name, int position) { | ||
| this.name = name; | ||
| this.position = position; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return this.name; | ||
| } | ||
|
|
||
| public int getPosition() { | ||
| return this.position; | ||
| } | ||
|
|
||
| public int move() { | ||
| return this.position++; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class InputView { | ||
| public static String setNames() { | ||
| System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); | ||
| Scanner scanner = new Scanner(System.in); | ||
| return scanner.nextLine(); | ||
| } | ||
|
|
||
| public static String overNames() { | ||
| System.out.println("자동차의 이름들을 5자 이하로 다시 작성하십시오"); | ||
| Scanner scanner = new Scanner(System.in); | ||
| return scanner.nextLine(); | ||
| } | ||
|
|
||
| public static int inputNumber() { | ||
| System.out.println("시도할 회수는 몇회인가요?"); | ||
| Scanner scanner = new Scanner(System.in); | ||
| return scanner.nextInt(); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import java.util.ArrayList; | ||
|
|
||
| public class Main { | ||
| public static void main(String[] args) { | ||
| RacingGame racing = new RacingGame(); | ||
| ArrayList<Car> cars = new ArrayList<>(); | ||
| cars = racing.registerCarNames(cars, racing.nameSet(InputView.setNames())); | ||
| int number = InputView.inputNumber(); | ||
| ResultView result = new ResultView(); | ||
| result.resultRacing(number, cars); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| import java.util.Random; | ||
| import java.util.ArrayList; | ||
|
|
||
| public class RacingGame { | ||
|
Contributor
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. RacingGame 에서 Cars 에 대한 상태를 들고 있으면 어떨까요? |
||
| static final int MAX_LENGTH = 6; | ||
| static final int CAN_GO_NUMBER = 4; | ||
| static final int ORIGINAL_POSITION = 0; | ||
|
|
||
| public String[] nameSet(String inputNames) { | ||
| String[] names = inputNames.split(","); | ||
| for (int i = 0; i < names.length; i++) { | ||
| names = nameLengthCheck(names, i); | ||
| } | ||
| return names; | ||
| } | ||
|
|
||
| public ArrayList<Car> registerCarNames(ArrayList<Car> cars, String[] nameSet) { | ||
| for (int i = 0; i < nameSet.length; i++) { | ||
| cars.add(new Car(nameSet[i], ORIGINAL_POSITION)); | ||
| } | ||
| return cars; | ||
| } | ||
|
|
||
| public String[] nameLengthCheck(String[] names, int i) { | ||
| if (names[i].length() >= MAX_LENGTH) { | ||
| String inputNames = InputView.overNames(); | ||
| names = inputNames.split(","); | ||
| } | ||
| return names; | ||
| } | ||
|
|
||
| public String getInputNames(String names) { | ||
| String[] carNames = nameSet(names); | ||
| String cars = ""; | ||
| for (int i = 0; i < carNames.length; i++) { | ||
| cars += carNames[i] + ""; | ||
| } | ||
| return cars; | ||
| } | ||
|
|
||
| public static int randomValue() { | ||
|
Contributor
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. 모든 메서드가 public 으로 공개되어있는 이유가 있을까요? |
||
| Random random = new Random(); | ||
| return random.nextInt(10); | ||
| } | ||
|
|
||
| public static ArrayList<Car> valueCheck(ArrayList<Car> cars, int randomValue, int i) { | ||
|
Contributor
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. 메서드들이 static 인 이유가 있을까요? |
||
| if (randomValue >= CAN_GO_NUMBER) { | ||
| cars.get(i).move(); | ||
| } | ||
| return cars; | ||
| } | ||
|
|
||
| public static ArrayList<Car> randomToNumber(ArrayList<Car> cars) { | ||
| for (int i = 0; i < cars.size(); i++) { | ||
| valueCheck(cars, randomValue(), i); | ||
| } | ||
| return cars; | ||
| } | ||
|
|
||
| public static Car winnerJudge(ArrayList<Car> cars, int max, int i, String winner) { | ||
| if (max < cars.get(i).getPosition()) { | ||
| max = cars.get(i).getPosition(); | ||
| winner = cars.get(i).getName(); | ||
| } | ||
| Car passInfo = new Car(winner, max); | ||
| return passInfo; | ||
| } | ||
|
|
||
| public static int sameScoreCount(int cnt, int max, ArrayList<Car> cars, int i) { | ||
| if (max == cars.get(i).getPosition()) { | ||
| cnt += 1; | ||
| } | ||
| return cnt; | ||
| } | ||
|
|
||
| public static ArrayList<String> winnersNameSet(ArrayList<String> winners, int max, ArrayList<Car> cars) { | ||
| for (int i = 0; i < cars.size(); i++) { | ||
| winners = sameScoreWinners(max, cars, winners, i); | ||
| } | ||
| return winners; | ||
| } | ||
|
|
||
| public static ArrayList<String> sameScoreWinners(int max, ArrayList<Car> cars, ArrayList<String> winners, int i) { | ||
| if (max == cars.get(i).getPosition()) { | ||
| winners.add(cars.get(i).getName()); | ||
| } | ||
| return winners; | ||
| } | ||
|
|
||
| public static ArrayList<String> winnerNameReturn(int cnt, String winner) { | ||
| ArrayList<String> winners = new ArrayList<>(); | ||
| if (cnt == 0) { | ||
| winners.add(winner); | ||
| } | ||
| return winners; | ||
| } | ||
|
|
||
| public static ArrayList<String> winnersNameReturn(int cnt, int max, ArrayList<Car> cars) { | ||
|
Contributor
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. 메서드명이 대부분 명사인 것 같은데 동사형으로 바꿔보면 어떨까요~ |
||
| ArrayList<String> winners = new ArrayList<>(cnt); | ||
| winners = winnersNameSet(winners, max, cars); | ||
| return winners; | ||
| } | ||
|
|
||
| public static ArrayList<String> findWinner(ArrayList<Car> cars) { | ||
| int max = cars.get(0).getPosition(); | ||
| String winner = cars.get(0).getName(); | ||
| for (int i = 1; i < cars.size(); i++) { | ||
| max = winnerJudge(cars, max, i, winner).getPosition(); | ||
| winner = winnerJudge(cars, max, i, winner).getName(); | ||
| } | ||
| int cnt = 0; | ||
| for (int i = 0; i < cars.size(); i++) { | ||
| cnt = sameScoreCount(cnt, max, cars, i); | ||
| } | ||
| ArrayList<String> winners; | ||
| winners = winnerNameReturn(cnt, winner); | ||
| if (cnt > 0) { | ||
| winners = winnersNameReturn(cnt, max, cars); | ||
| } | ||
| return winners; | ||
| } | ||
|
|
||
| public String getWinner(ArrayList<Car> cars, int[] randomNumbers) { | ||
|
Contributor
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. 한 클래스에서 책임을 많이 들고 있기 때문에 클래스 길이가 늘어난 것 같은데요, |
||
| ResultView v = new ResultView(); | ||
|
|
||
| for (int i = 0; i < cars.size(); i++) { | ||
| cars = valueCheck(cars, randomNumbers[i], i); | ||
| } | ||
| ArrayList<String> winners = findWinner(cars); | ||
| String winner = ""; | ||
| for (int i = 0; i < winners.size(); i++) { | ||
| winner += winners.get(i) + " "; | ||
| } | ||
| return winner; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import org.junit.After; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| import java.util.ArrayList; | ||
|
|
||
| public class RacingGameTest { | ||
| RacingGame r; | ||
| ResultView v; | ||
|
Contributor
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. 스터디 규칙 : 변수명을 줄여쓰지 않는다 |
||
|
|
||
| @Before | ||
| public void setUp() { | ||
| r = new RacingGame(); | ||
| v = new ResultView(); | ||
| } | ||
|
|
||
| @Test | ||
| public void carNamesInputTest() { | ||
| String input = "아송, 윈도우, 구름, 그리즈, 키캣"; | ||
| assertEquals("아송 윈도우 구름 그리즈 키캣", r.getInputNames(input)); | ||
| } | ||
|
|
||
| @Test | ||
| public void winnerTest() { | ||
| ArrayList<Car> cars = new ArrayList<>(); | ||
| String input = "아송, 윈도우, 구름, 그리즈, 키캣"; | ||
| int number = 1; | ||
| int[] randomNumbers = {1, 3, 5, 8, 2}; | ||
| cars = r.registerCarNames(cars, r.nameSet(input)); | ||
| assertEquals(" 구름 그리즈 ", r.getWinner(cars, randomNumbers)); | ||
| } | ||
|
|
||
| @After | ||
| public void tearDown() { | ||
| r = null; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import java.util.ArrayList; | ||
|
|
||
| public class ResultView { | ||
| public static void resultRacing(int number, ArrayList<Car> cars) { | ||
| System.out.println("실행 결과"); | ||
| for (int i = 0; i < number; i++) { | ||
| RacingGame.randomToNumber(cars); | ||
| resultRacing(cars); | ||
| System.out.println(""); | ||
| } | ||
| ArrayList<String> winners = RacingGame.findWinner(cars); | ||
| finalResultView(winners); | ||
|
Contributor
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. ResultView 는 어플리케이션의 흐름이나 로직을 알 필요 없이 그냥 들어온 input 에 대해서 출력만 해주는 정도의 역할을 하도록 변경해보면 어떨까요? |
||
| } | ||
|
|
||
| public static void resultRacing(ArrayList<Car> cars) { | ||
| for (int i = 0; i < cars.size(); i++) { | ||
| System.out.println(cars.get(i).getName() + ":" + racingView(cars.get(i).getPosition())); | ||
| } | ||
| } | ||
|
|
||
| public static String racingView(int n) { | ||
| String car = ""; | ||
| for (int i = 0; i < n; i++) { | ||
| car += "-"; | ||
| } | ||
| return car; | ||
| } | ||
|
|
||
| public static void finalResultView(ArrayList<String> winners) { | ||
| if (winners.size() == 1) { | ||
| System.out.println(winners.get(0) + "가 최종 우승했습니다."); | ||
| } | ||
| if (winners.size() > 1) { | ||
| for (int i = 0; i < winners.size() - 1; i++) { | ||
| System.out.print(winners.get(i) + ","); | ||
| } | ||
| System.out.println(winners.get(winners.size() - 1) + "가 최종 우승했습니다."); | ||
| } | ||
| } | ||
| } | ||
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.
조슈아 블로크의 effective java 에는 '객체는 인터페이스를 사용해 참조하라.'
라는 부분이 있습니다.
한번 읽어보시고 개선해보시면 좋을 것 같아요 ~
jaehun2841.github.io/2019/03/01/effective-java-item64/#%EC%9C%A0%EC%97%B0%ED%95%9C-%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%A8%EC%9D%84-%EC%83%9D%EC%84%B1%ED%95%98%EB%8A%94-%EC%9D%B8%ED%84%B0%ED%8E%98%EC%9D%B4%EC%8A%A4-%ED%83%80%EC%9E%85-%EB%B3%80%EC%88%98