From d59f99a748d0647540ba18a3c0cd522c6d617949 Mon Sep 17 00:00:00 2001 From: sunghyuki <62830487+sunghyuki@users.noreply.github.com> Date: Wed, 19 Aug 2020 11:53:57 +0900 Subject: [PATCH 1/5] =?UTF-8?q?[=EB=AF=B8=EC=85=981]=20=EA=B5=AC=ED=98=84?= =?UTF-8?q?=20=EC=99=84=EB=A3=8C=ED=96=88=EC=8A=B5=EB=8B=88=EB=8B=A4.=20?= =?UTF-8?q?=EB=A6=AC=EB=B7=B0=20=EC=9A=94=EC=B2=AD=EB=93=9C=EB=A6=BD?= =?UTF-8?q?=EB=8B=88=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Car.java | 30 +++++++++++++++++ src/InputView.java | 61 ++++++++++++++++++++++++++++++++++ src/OutputView.java | 20 +++++++++++ src/Race.java | 81 +++++++++++++++++++++++++++++++++++++++++++++ src/RacingGame.java | 8 +++++ 5 files changed, 200 insertions(+) create mode 100644 src/Car.java create mode 100644 src/InputView.java create mode 100644 src/OutputView.java create mode 100644 src/Race.java create mode 100644 src/RacingGame.java diff --git a/src/Car.java b/src/Car.java new file mode 100644 index 0000000..89fea14 --- /dev/null +++ b/src/Car.java @@ -0,0 +1,30 @@ +import java.util.Random; + +public class Car { + private String name; + private int position; + + Car(String name) { + this.name = name; + this.position = 0; + } + + public String getName() { + return name; + } + + public int getPosition() { + return position; + } + + public void setPosition(int position) { + this.position = position; + } + + public void goForward(){ + Random random = new Random(); + if(random.nextInt(10) >= 4){ + this.setPosition(this.getPosition()+1); + } + } +} diff --git a/src/InputView.java b/src/InputView.java new file mode 100644 index 0000000..55fa2b7 --- /dev/null +++ b/src/InputView.java @@ -0,0 +1,61 @@ +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +public class InputView { + private static final Integer MAX_LENGTH = 6; + Scanner scanner = new Scanner(System.in); + public List input() { + String[] name = inputCarName(); + //int count = inputCount(); + + List cars = new ArrayList<>(); + for(int i = 0; i= MAX_LENGTH){ + throw new IllegalArgumentException("이름은 5자 이하만 가능합니다."); + } + } + + public int inputCount(){ + int inputCount; + inputCount = scanner.nextInt(); + return inputCount; + } + +} + + + + + diff --git a/src/OutputView.java b/src/OutputView.java new file mode 100644 index 0000000..7a01555 --- /dev/null +++ b/src/OutputView.java @@ -0,0 +1,20 @@ +import java.util.List; + +public class OutputView { + public void inputCarNamesMessage(){ + System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); + } + public void inputCountMessage(){ + System.out.println("시도할 횟수는 몇회인가요?"); + } + public void ResultMessage(){ + System.out.println("\n"); + System.out.println("실행 결과"); + } + public void oneTrialMessage(Car car,StringBuilder goSignal){ + System.out.println(car.getName()+": "+goSignal); + } + public void getWinnerMessage(List winnernames) { + System.out.println(String.join(",", winnernames + "가 최종 우승했습니다.")); + } +} diff --git a/src/Race.java b/src/Race.java new file mode 100644 index 0000000..75c4329 --- /dev/null +++ b/src/Race.java @@ -0,0 +1,81 @@ +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; + +public class Race { + final static String drive = "-"; + private List carLineUp = new ArrayList<>(); + private int trial; + InputView inputView = new InputView(); + OutputView outputView = new OutputView(); + + public void play() { + outputView.inputCarNamesMessage(); + carLineUp = inputView.input(); + outputView.inputCountMessage(); + trial = inputView.inputCount(); + for(int i = 0; i < trial; i++) { + outputView.ResultMessage(); + race(carLineUp); + } + getWinner(carLineUp); + } + + private void race(List carLineUp){ + for(Car car: carLineUp){ + car.goForward(); + raceOneTrial(car); + } + } + + private void raceOneTrial(Car car){ + StringBuilder goSignal = new StringBuilder(); + for(int i = 0; i carLineUp) { + /* + StringBuilder winner = new StringBuilder(); + int positionOfWinner; + positionOfWinner = findPositionOfWinner(carLineUp); + for(Car car: carLineUp){ + winner.append(winnerBuilder(car,positionOfWinner)); + } + System.out.println(String.join(",",winner)+"가 최종 우승했습니다"); + } + */ + List winner = new ArrayList<>(); + int positionOfWinner; + positionOfWinner = findPositionOfWinner(carLineUp); + winner = carLineUp.stream().filter(car -> (car.getPosition() == positionOfWinner)).collect(Collectors.toList()); + List winnernames = new ArrayList<>(); + for(int i = 0; i < winner.size(); i++){ + winnernames.add(winner.get(i).getName()); + } + outputView.getWinnerMessage(winnernames); + } + + /* + private StringBuilder winnerBuilder(Car car, int positionOfWinner){ + StringBuilder winner = new StringBuilder(); + if(positionOfWinner == car.getPosition()) { + winner.append(car.getName()); + } + return winner; + } + */ + + private int findPositionOfWinner(List carLineUp){ + List position = new ArrayList<>(); + int positionOfWinner; + for(Car car: carLineUp){ + position.add(car.getPosition()); + } + positionOfWinner = Collections.max(position); + return positionOfWinner; + } +} \ No newline at end of file diff --git a/src/RacingGame.java b/src/RacingGame.java new file mode 100644 index 0000000..fdf3c77 --- /dev/null +++ b/src/RacingGame.java @@ -0,0 +1,8 @@ +public class RacingGame { + public static void main(String[] args) { + Race race = new Race(); + race.play(); + } +} + + From 6ab9666227125db2b6d4b507e97cb42b16e62a02 Mon Sep 17 00:00:00 2001 From: sunghyuki <62830487+sunghyuki@users.noreply.github.com> Date: Wed, 26 Aug 2020 09:57:27 +0900 Subject: [PATCH 2/5] =?UTF-8?q?refactoring:=20=ED=94=BC=EB=93=9C=EB=B0=B1?= =?UTF-8?q?=20=EC=9D=BC=EB=B6=80=20=EB=B0=98=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 35 ++++++++++++++++++++++ src/Car.java | 13 ++------ src/InputView.java | 72 +++++++++++++++++++++++++-------------------- src/OutputView.java | 11 ++----- src/Race.java | 68 ++++++++++++++++++------------------------ src/Rule.java | 14 +++++++++ 6 files changed, 121 insertions(+), 92 deletions(-) create mode 100644 src/Rule.java diff --git a/README.md b/README.md index 93afe8b..d343e25 100644 --- a/README.md +++ b/README.md @@ -1 +1,36 @@ ECONOVATION HEROES 2기 2기 Java Racing Game Practice + +#####step1 +- 접근제어자 접근 범위를 생각하고 사용하기 +- CamelCase 따르기 +- 스터디 규칙 : 모든 변수명은 줄여쓰지 않는다 +- 불필요한 공백 포함하지 않기 +- Java 는 메모리를 직접 접근하여 해제하지 않고 Garbage Collector 를 사용하여 + 메모리 해제함. + + #####step2 + - primitive 타입과 wrapper 타입의 차이점 + - 게임의 룰은 어떤 클래스에 적용할 것인가? + - code formatting 습관 들이기 + - magic number 상수로 추출(상수는 네이밍은 대문자로 하는 것이 룰) + - 의존성 측면 고려하고 코드짜기 + + ######refactoring: + - InputView 클래스 상수 타입 변환 + - Race 클래스 상수 네이밍 대문자 처리 + - CamelCase에 따라 네이밍 고치기 + - 게임의 룰을 관리할 Rule 클래스 생성 + - setter 사용 지양 + - 불필요한 코드 주석 및 공백 제거 + - stream api 적용 + - 게임 시작을 위한 input 과 관련된 print 는 inputview로 옮기기 + + ######해야할 일 + - 테스트 코드 작성 + - View 에 대한 책임을 Main(RacingGame) 으로 위임 + - InputView static 메소드로 구성하여 객체 생성 비용을 절감하기 + + + + + diff --git a/src/Car.java b/src/Car.java index 89fea14..366cefc 100644 --- a/src/Car.java +++ b/src/Car.java @@ -1,5 +1,3 @@ -import java.util.Random; - public class Car { private String name; private int position; @@ -17,14 +15,7 @@ public int getPosition() { return position; } - public void setPosition(int position) { - this.position = position; - } - - public void goForward(){ - Random random = new Random(); - if(random.nextInt(10) >= 4){ - this.setPosition(this.getPosition()+1); - } + public void goForward() { + this.position++; } } diff --git a/src/InputView.java b/src/InputView.java index 55fa2b7..e1c9f2d 100644 --- a/src/InputView.java +++ b/src/InputView.java @@ -1,58 +1,66 @@ import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Scanner; +import java.util.stream.Stream; public class InputView { - private static final Integer MAX_LENGTH = 6; - Scanner scanner = new Scanner(System.in); - public List input() { - String[] name = inputCarName(); - //int count = inputCount(); + private static final int MAX_LENGTH = 6; + private Scanner scanner = new Scanner(System.in); + + public List inputCarName() { + String name = scanner.nextLine(); + String[] splittedInputCarName = splitInputCarName(name); + String[] trimmedInputCarName = trimInputCarName(splittedInputCarName); + + try { + processStreamAboutCheckName(trimmedInputCarName); + } catch(IllegalArgumentException e) { + System.out.println(e.getMessage()); + inputCarName(); + } List cars = new ArrayList<>(); - for(int i = 0; i= MAX_LENGTH){ + private void checkNameLength(String name) { + if(name.length() >= MAX_LENGTH) { throw new IllegalArgumentException("이름은 5자 이하만 가능합니다."); } } - public int inputCount(){ - int inputCount; - inputCount = scanner.nextInt(); - return inputCount; + private void processStreamAboutCheckName(String[] trimmedInputCarName) { + Stream stream = Arrays.stream(trimmedInputCarName); + stream.forEach(e -> checkNameLength(e)); + } + + public int inputTrial() { + return scanner.nextInt(); } + public void inputCarNamesMessage() { + System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); + } + + public void inputCountMessage() { + System.out.println("시도할 횟수는 몇회인가요?"); + } } diff --git a/src/OutputView.java b/src/OutputView.java index 7a01555..0ff3e5a 100644 --- a/src/OutputView.java +++ b/src/OutputView.java @@ -1,15 +1,8 @@ import java.util.List; public class OutputView { - public void inputCarNamesMessage(){ - System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); - } - public void inputCountMessage(){ - System.out.println("시도할 횟수는 몇회인가요?"); - } - public void ResultMessage(){ - System.out.println("\n"); - System.out.println("실행 결과"); + public void resultMessage() { + System.out.println("\n실행 결과"); } public void oneTrialMessage(Car car,StringBuilder goSignal){ System.out.println(car.getName()+": "+goSignal); diff --git a/src/Race.java b/src/Race.java index 75c4329..b5454d0 100644 --- a/src/Race.java +++ b/src/Race.java @@ -4,75 +4,63 @@ import java.util.stream.Collectors; public class Race { - final static String drive = "-"; - private List carLineUp = new ArrayList<>(); + private static final String DRIVE = "-"; + private List cars = new ArrayList<>(); private int trial; InputView inputView = new InputView(); OutputView outputView = new OutputView(); + Rule rule = new Rule(); public void play() { - outputView.inputCarNamesMessage(); - carLineUp = inputView.input(); - outputView.inputCountMessage(); - trial = inputView.inputCount(); + inputView.inputCarNamesMessage(); + cars = inputView.inputCarName(); + inputView.inputCountMessage(); + trial = inputView.inputTrial(); + for(int i = 0; i < trial; i++) { - outputView.ResultMessage(); - race(carLineUp); + outputView.resultMessage(); + race(cars); } - getWinner(carLineUp); + getWinner(cars); } - private void race(List carLineUp){ - for(Car car: carLineUp){ - car.goForward(); + private void race(List cars) { + for(Car car: cars) { + goOrStay(car); raceOneTrial(car); } } - private void raceOneTrial(Car car){ + private void goOrStay(Car car) { + if(rule.isGoForward()) { + car.goForward(); + } + } + + private void raceOneTrial(Car car) { StringBuilder goSignal = new StringBuilder(); - for(int i = 0; i carLineUp) { - /* - StringBuilder winner = new StringBuilder(); - int positionOfWinner; - positionOfWinner = findPositionOfWinner(carLineUp); - for(Car car: carLineUp){ - winner.append(winnerBuilder(car,positionOfWinner)); - } - System.out.println(String.join(",",winner)+"가 최종 우승했습니다"); - } - */ + private void getWinner(List cars) { List winner = new ArrayList<>(); int positionOfWinner; - positionOfWinner = findPositionOfWinner(carLineUp); - winner = carLineUp.stream().filter(car -> (car.getPosition() == positionOfWinner)).collect(Collectors.toList()); + positionOfWinner = findPositionOfWinner(cars); + winner = cars.stream().filter(car -> (car.getPosition() == positionOfWinner)).collect(Collectors.toList()); List winnernames = new ArrayList<>(); - for(int i = 0; i < winner.size(); i++){ + for(int i = 0; i < winner.size(); i++) { winnernames.add(winner.get(i).getName()); } outputView.getWinnerMessage(winnernames); } - /* - private StringBuilder winnerBuilder(Car car, int positionOfWinner){ - StringBuilder winner = new StringBuilder(); - if(positionOfWinner == car.getPosition()) { - winner.append(car.getName()); - } - return winner; - } - */ - - private int findPositionOfWinner(List carLineUp){ + private int findPositionOfWinner(List cars) { List position = new ArrayList<>(); int positionOfWinner; - for(Car car: carLineUp){ + for(Car car: cars) { position.add(car.getPosition()); } positionOfWinner = Collections.max(position); diff --git a/src/Rule.java b/src/Rule.java new file mode 100644 index 0000000..e4e551b --- /dev/null +++ b/src/Rule.java @@ -0,0 +1,14 @@ +import java.util.Random; + +public class Rule { + private static final int BOUNDARYNUMBER = 10; + private static final int GOFORWARDCONDITION = 4; + + public boolean isGoForward() { + Random random = new Random(); + if(random.nextInt(BOUNDARYNUMBER) >= GOFORWARDCONDITION) { + return true; + } + return false; + } +} From 55a5ff1c1996e8ab96323a4bdc38a6c901db6636 Mon Sep 17 00:00:00 2001 From: sunghyuki <62830487+sunghyuki@users.noreply.github.com> Date: Thu, 27 Aug 2020 01:36:54 +0900 Subject: [PATCH 3/5] =?UTF-8?q?refactoring:=20=ED=94=BC=EB=93=9C=EB=B0=B1?= =?UTF-8?q?=20=EB=B0=98=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 ++++-- src/InputView.java | 21 +++++++++--------- src/OutputView.java | 18 ++++++++++++++-- src/Race.java | 33 +++------------------------- src/RacingGame.java | 21 +++++++++++++++++- test/RacingGameTest.java | 46 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 99 insertions(+), 46 deletions(-) create mode 100644 test/RacingGameTest.java diff --git a/README.md b/README.md index d343e25..4bb1c98 100644 --- a/README.md +++ b/README.md @@ -24,11 +24,13 @@ ECONOVATION HEROES 2기 2기 Java Racing Game Practice - 불필요한 코드 주석 및 공백 제거 - stream api 적용 - 게임 시작을 위한 input 과 관련된 print 는 inputview로 옮기기 + - View 에 대한 책임을 Main(RacingGame) 으로 위임 + - InputView static 메소드로 구성하여 객체 생성 비용을 절감하기 ######해야할 일 - 테스트 코드 작성 - - View 에 대한 책임을 Main(RacingGame) 으로 위임 - - InputView static 메소드로 구성하여 객체 생성 비용을 절감하기 + 테스트 코드를 작성하다보니 public으로만 테스트가 안된다는 점에서 코드가 잘 짜여지지 않다고 느꼈다. + 테스트 코드를 먼저 작성하고 그에 맞추어 코드를 작성해야할까? diff --git a/src/InputView.java b/src/InputView.java index e1c9f2d..f06dca1 100644 --- a/src/InputView.java +++ b/src/InputView.java @@ -6,9 +6,9 @@ public class InputView { private static final int MAX_LENGTH = 6; - private Scanner scanner = new Scanner(System.in); + private static final Scanner scanner = new Scanner(System.in); - public List inputCarName() { + public static List inputCarName() { String name = scanner.nextLine(); String[] splittedInputCarName = splitInputCarName(name); String[] trimmedInputCarName = trimInputCarName(splittedInputCarName); @@ -27,38 +27,37 @@ public List inputCarName() { return cars; } - private String[] splitInputCarName(String name) { - String[] splittedInputCarName = name.split(","); - return splittedInputCarName; + private static String[] splitInputCarName(String name) { + return name.split(","); } - private String[] trimInputCarName(String[] name) { + private static String[] trimInputCarName(String[] name) { for(int i = 0; i= MAX_LENGTH) { throw new IllegalArgumentException("이름은 5자 이하만 가능합니다."); } } - private void processStreamAboutCheckName(String[] trimmedInputCarName) { + private static void processStreamAboutCheckName(String[] trimmedInputCarName) { Stream stream = Arrays.stream(trimmedInputCarName); stream.forEach(e -> checkNameLength(e)); } - public int inputTrial() { + public static int inputTrial() { return scanner.nextInt(); } - public void inputCarNamesMessage() { + public static void inputCarNamesMessage() { System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); } - public void inputCountMessage() { + public static void inputCountMessage() { System.out.println("시도할 횟수는 몇회인가요?"); } } diff --git a/src/OutputView.java b/src/OutputView.java index 0ff3e5a..f1038e2 100644 --- a/src/OutputView.java +++ b/src/OutputView.java @@ -1,12 +1,26 @@ import java.util.List; public class OutputView { + private static final String DRIVE = "-"; + public void resultMessage() { System.out.println("\n실행 결과"); } - public void oneTrialMessage(Car car,StringBuilder goSignal){ - System.out.println(car.getName()+": "+goSignal); + + public void oneTrialMessage(List cars) { + for(Car car: cars) { + System.out.println(car.getName() + ": " + raceOneTrial(car)); + } } + + private StringBuilder raceOneTrial(Car car) { + StringBuilder goSignal = new StringBuilder(); + for(int i = 0; i winnernames) { System.out.println(String.join(",", winnernames + "가 최종 우승했습니다.")); } diff --git a/src/Race.java b/src/Race.java index b5454d0..db491df 100644 --- a/src/Race.java +++ b/src/Race.java @@ -4,30 +4,11 @@ import java.util.stream.Collectors; public class Race { - private static final String DRIVE = "-"; - private List cars = new ArrayList<>(); - private int trial; - InputView inputView = new InputView(); - OutputView outputView = new OutputView(); Rule rule = new Rule(); - public void play() { - inputView.inputCarNamesMessage(); - cars = inputView.inputCarName(); - inputView.inputCountMessage(); - trial = inputView.inputTrial(); - - for(int i = 0; i < trial; i++) { - outputView.resultMessage(); - race(cars); - } - getWinner(cars); - } - - private void race(List cars) { + public void race(List cars) { for(Car car: cars) { goOrStay(car); - raceOneTrial(car); } } @@ -37,15 +18,7 @@ private void goOrStay(Car car) { } } - private void raceOneTrial(Car car) { - StringBuilder goSignal = new StringBuilder(); - for(int i = 0; i cars) { + public List getWinner(List cars) { List winner = new ArrayList<>(); int positionOfWinner; positionOfWinner = findPositionOfWinner(cars); @@ -54,7 +27,7 @@ private void getWinner(List cars) { for(int i = 0; i < winner.size(); i++) { winnernames.add(winner.get(i).getName()); } - outputView.getWinnerMessage(winnernames); + return winnernames; } private int findPositionOfWinner(List cars) { diff --git a/src/RacingGame.java b/src/RacingGame.java index fdf3c77..534bea8 100644 --- a/src/RacingGame.java +++ b/src/RacingGame.java @@ -1,7 +1,26 @@ +import java.util.ArrayList; +import java.util.List; + public class RacingGame { public static void main(String[] args) { + OutputView outputView = new OutputView(); Race race = new Race(); - race.play(); + List cars = new ArrayList<>(); + int trial; + + InputView.inputCarNamesMessage(); + cars = InputView.inputCarName(); + InputView.inputCountMessage(); + trial = InputView.inputTrial(); + + for(int i = 0; i < trial; i++) { + outputView.resultMessage(); + race.race(cars); + outputView.oneTrialMessage(cars); + } + + List winnernames = race.getWinner(cars); + outputView.getWinnerMessage(winnernames); } } diff --git a/test/RacingGameTest.java b/test/RacingGameTest.java new file mode 100644 index 0000000..6f7df1c --- /dev/null +++ b/test/RacingGameTest.java @@ -0,0 +1,46 @@ +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +public class RacingGameTest { + List cars; + @Before + public void setUp() throws Exception { + cars = new ArrayList<>(); + cars.add(new Car("애플")); + cars.add(new Car("삼성")); + cars.add(new Car("전남대")); + } + + @Test + public void inputCarName() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + String carName = "애플, 삼성, 전남대"; + InputView inputView = new InputView(); + try { + Method methodsplit = inputView.getClass().getDeclaredMethod("splitInputCarName", String.class); + methodsplit.setAccessible(true); + Method methodtrim = inputView.getClass().getDeclaredMethod("trimInputCarName", String[].class); + methodtrim.setAccessible(true); + String[] name = (String[]) methodsplit.invoke(inputView, carName); + String[] name1 = (String[]) methodtrim.invoke(inputView, (Object) name); + assertThat("애플", is(name1[0])); + assertThat("삼성", is(name1[1])); + assertThat("전남대", is(name1[2])); + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { + e.printStackTrace(); + } + } + + @After + public void tearDown() throws Exception { + cars = null; + } +} \ No newline at end of file From 211f0a3a5d43be4bb20a0b9cdde5b4d0ae87326a Mon Sep 17 00:00:00 2001 From: sunghyuki <62830487+sunghyuki@users.noreply.github.com> Date: Sun, 6 Sep 2020 15:44:04 +0900 Subject: [PATCH 4/5] =?UTF-8?q?refactoring:=202=EC=B0=A8=20=ED=94=BC?= =?UTF-8?q?=EB=93=9C=EB=B0=B1=20=EC=9D=BC=EB=B6=80=20=EB=B0=98=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 7 ++++++- src/Race.java | 15 ++++++--------- src/RacingGame.java | 3 +-- src/Rule.java | 2 +- test/RaceTest.java | 12 ++++++++++++ test/RacingGameTest.java | 7 ++++++- test/RuleTest.java | 12 ++++++++++++ 7 files changed, 44 insertions(+), 14 deletions(-) create mode 100644 test/RaceTest.java create mode 100644 test/RuleTest.java diff --git a/README.md b/README.md index 4bb1c98..9f1a32d 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,12 @@ ECONOVATION HEROES 2기 2기 Java Racing Game Practice - 게임 시작을 위한 input 과 관련된 print 는 inputview로 옮기기 - View 에 대한 책임을 Main(RacingGame) 으로 위임 - InputView static 메소드로 구성하여 객체 생성 비용을 절감하기 - + (추가) + - 메서드 체이닝을 적용해서 Arrays.stream(trimmedInputCarName).forEach(~~) + 로 이어 붙이기 + - util 목적으로 사용하실 클래스 객체 생성 비용 줄이기 + - 의미없는 메모리 낭비 없애기 + - 가독성에 유리하게 선언과 할당 같은 곳에서 해주기 ######해야할 일 - 테스트 코드 작성 테스트 코드를 작성하다보니 public으로만 테스트가 안된다는 점에서 코드가 잘 짜여지지 않다고 느꼈다. diff --git a/src/Race.java b/src/Race.java index db491df..e82e087 100644 --- a/src/Race.java +++ b/src/Race.java @@ -4,7 +4,7 @@ import java.util.stream.Collectors; public class Race { - Rule rule = new Rule(); + private List winnernames = new ArrayList<>(); public void race(List cars) { for(Car car: cars) { @@ -13,19 +13,16 @@ public void race(List cars) { } private void goOrStay(Car car) { - if(rule.isGoForward()) { + if(Rule.isGoForward()) { car.goForward(); } } public List getWinner(List cars) { - List winner = new ArrayList<>(); - int positionOfWinner; - positionOfWinner = findPositionOfWinner(cars); - winner = cars.stream().filter(car -> (car.getPosition() == positionOfWinner)).collect(Collectors.toList()); - List winnernames = new ArrayList<>(); - for(int i = 0; i < winner.size(); i++) { - winnernames.add(winner.get(i).getName()); + int positionOfWinner = findPositionOfWinner(cars); + cars = cars.stream().filter(car -> (car.getPosition() == positionOfWinner)).collect(Collectors.toList()); + for (Car car : cars) { + this.winnernames.add(car.getName()); } return winnernames; } diff --git a/src/RacingGame.java b/src/RacingGame.java index 534bea8..6b9b39e 100644 --- a/src/RacingGame.java +++ b/src/RacingGame.java @@ -19,8 +19,7 @@ public static void main(String[] args) { outputView.oneTrialMessage(cars); } - List winnernames = race.getWinner(cars); - outputView.getWinnerMessage(winnernames); + outputView.getWinnerMessage(race.getWinner(cars)); } } diff --git a/src/Rule.java b/src/Rule.java index e4e551b..04deb34 100644 --- a/src/Rule.java +++ b/src/Rule.java @@ -4,7 +4,7 @@ public class Rule { private static final int BOUNDARYNUMBER = 10; private static final int GOFORWARDCONDITION = 4; - public boolean isGoForward() { + public static boolean isGoForward() { Random random = new Random(); if(random.nextInt(BOUNDARYNUMBER) >= GOFORWARDCONDITION) { return true; diff --git a/test/RaceTest.java b/test/RaceTest.java new file mode 100644 index 0000000..5d39304 --- /dev/null +++ b/test/RaceTest.java @@ -0,0 +1,12 @@ +import org.junit.Test; + +import static org.junit.Assert.*; + +public class RaceTest { + @Test + public void getWinner() { + + } + + +} \ No newline at end of file diff --git a/test/RacingGameTest.java b/test/RacingGameTest.java index 6f7df1c..92eefb0 100644 --- a/test/RacingGameTest.java +++ b/test/RacingGameTest.java @@ -7,7 +7,7 @@ import java.util.ArrayList; import java.util.List; -import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.*; import static org.junit.Assert.assertThat; public class RacingGameTest { @@ -39,6 +39,11 @@ public void inputCarName() throws NoSuchMethodException, InvocationTargetExcepti } } + @Test + public void inputTrial() { + + } + @After public void tearDown() throws Exception { cars = null; diff --git a/test/RuleTest.java b/test/RuleTest.java new file mode 100644 index 0000000..c807a2a --- /dev/null +++ b/test/RuleTest.java @@ -0,0 +1,12 @@ +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.either; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.*; + +public class RuleTest { + @Test + public void isGoForward() { + assertThat(Rule.isGoForward(),either(is(true)).or(is(false)) ); + } +} \ No newline at end of file From fc33cc45a153a1a362a5429564327c1d436c646a Mon Sep 17 00:00:00 2001 From: sunghyuki <62830487+sunghyuki@users.noreply.github.com> Date: Mon, 7 Sep 2020 22:55:42 +0900 Subject: [PATCH 5/5] =?UTF-8?q?add:=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 5 ++-- src/Race.java | 39 ------------------------------ src/RacingGame.java | 45 ++++++++++++++++++++++------------- src/RacingMain.java | 26 ++++++++++++++++++++ test/InputViewTest.java | 51 ++++++++++++++++++++++++++++++++++++++++ test/RacingGameTest.java | 36 ++++++++++++++-------------- 6 files changed, 126 insertions(+), 76 deletions(-) delete mode 100644 src/Race.java create mode 100644 src/RacingMain.java create mode 100644 test/InputViewTest.java diff --git a/README.md b/README.md index 9f1a32d..b93d9e4 100644 --- a/README.md +++ b/README.md @@ -32,8 +32,9 @@ ECONOVATION HEROES 2기 2기 Java Racing Game Practice - util 목적으로 사용하실 클래스 객체 생성 비용 줄이기 - 의미없는 메모리 낭비 없애기 - 가독성에 유리하게 선언과 할당 같은 곳에서 해주기 - ######해야할 일 - - 테스트 코드 작성 + - 테스트 코드 + ###### 해결할 문제 + 테스트 코드를 작성하다보니 public으로만 테스트가 안된다는 점에서 코드가 잘 짜여지지 않다고 느꼈다. 테스트 코드를 먼저 작성하고 그에 맞추어 코드를 작성해야할까? diff --git a/src/Race.java b/src/Race.java deleted file mode 100644 index e82e087..0000000 --- a/src/Race.java +++ /dev/null @@ -1,39 +0,0 @@ -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.stream.Collectors; - -public class Race { - private List winnernames = new ArrayList<>(); - - public void race(List cars) { - for(Car car: cars) { - goOrStay(car); - } - } - - private void goOrStay(Car car) { - if(Rule.isGoForward()) { - car.goForward(); - } - } - - public List getWinner(List cars) { - int positionOfWinner = findPositionOfWinner(cars); - cars = cars.stream().filter(car -> (car.getPosition() == positionOfWinner)).collect(Collectors.toList()); - for (Car car : cars) { - this.winnernames.add(car.getName()); - } - return winnernames; - } - - private int findPositionOfWinner(List cars) { - List position = new ArrayList<>(); - int positionOfWinner; - for(Car car: cars) { - position.add(car.getPosition()); - } - positionOfWinner = Collections.max(position); - return positionOfWinner; - } -} \ No newline at end of file diff --git a/src/RacingGame.java b/src/RacingGame.java index 6b9b39e..8025f11 100644 --- a/src/RacingGame.java +++ b/src/RacingGame.java @@ -1,26 +1,39 @@ import java.util.ArrayList; +import java.util.Collections; import java.util.List; +import java.util.stream.Collectors; public class RacingGame { - public static void main(String[] args) { - OutputView outputView = new OutputView(); - Race race = new Race(); - List cars = new ArrayList<>(); - int trial; + private List winnernames = new ArrayList<>(); - InputView.inputCarNamesMessage(); - cars = InputView.inputCarName(); - InputView.inputCountMessage(); - trial = InputView.inputTrial(); - - for(int i = 0; i < trial; i++) { - outputView.resultMessage(); - race.race(cars); - outputView.oneTrialMessage(cars); + public void race(List cars) { + for(Car car: cars) { + goOrStay(car); } + } - outputView.getWinnerMessage(race.getWinner(cars)); + private void goOrStay(Car car) { + if(Rule.isGoForward()) { + car.goForward(); + } } -} + public List getWinner(List cars) { + int positionOfWinner = findPositionOfWinner(cars); + cars = cars.stream().filter(car -> (car.getPosition() == positionOfWinner)).collect(Collectors.toList()); + for (Car car : cars) { + this.winnernames.add(car.getName()); + } + return winnernames; + } + private int findPositionOfWinner(List cars) { + List position = new ArrayList<>(); + int positionOfWinner; + for(Car car: cars) { + position.add(car.getPosition()); + } + positionOfWinner = Collections.max(position); + return positionOfWinner; + } +} \ No newline at end of file diff --git a/src/RacingMain.java b/src/RacingMain.java new file mode 100644 index 0000000..7d4c83f --- /dev/null +++ b/src/RacingMain.java @@ -0,0 +1,26 @@ +import java.util.ArrayList; +import java.util.List; + +public class RacingMain { + public static void main(String[] args) { + OutputView outputView = new OutputView(); + RacingGame racingGame = new RacingGame(); + List cars = new ArrayList<>(); + int trial; + + InputView.inputCarNamesMessage(); + cars = InputView.inputCarName(); + InputView.inputCountMessage(); + trial = InputView.inputTrial(); + + for(int i = 0; i < trial; i++) { + outputView.resultMessage(); + racingGame.race(cars); + outputView.oneTrialMessage(cars); + } + + outputView.getWinnerMessage(racingGame.getWinner(cars)); + } +} + + diff --git a/test/InputViewTest.java b/test/InputViewTest.java new file mode 100644 index 0000000..6a939c2 --- /dev/null +++ b/test/InputViewTest.java @@ -0,0 +1,51 @@ +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.assertThat; + +public class InputViewTest { + List cars; + @Before + public void setUp() throws Exception { + cars = new ArrayList<>(); + cars.add(new Car("애플")); + cars.add(new Car("삼성")); + cars.add(new Car("전남대")); + } + + @Test + public void inputCarName() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + String carName = "애플, 삼성, 전남대"; + InputView inputView = new InputView(); + try { + Method methodsplit = inputView.getClass().getDeclaredMethod("splitInputCarName", String.class); + methodsplit.setAccessible(true); + Method methodtrim = inputView.getClass().getDeclaredMethod("trimInputCarName", String[].class); + methodtrim.setAccessible(true); + String[] name = (String[]) methodsplit.invoke(inputView, carName); + String[] name1 = (String[]) methodtrim.invoke(inputView, (Object) name); + assertThat("애플", is(name1[0])); + assertThat("삼성", is(name1[1])); + assertThat("전남대", is(name1[2])); + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { + e.printStackTrace(); + } + } + + @Test + public void inputTrial() { + + } + + @After + public void tearDown() throws Exception { + cars = null; + } +} \ No newline at end of file diff --git a/test/RacingGameTest.java b/test/RacingGameTest.java index 92eefb0..08ebf3c 100644 --- a/test/RacingGameTest.java +++ b/test/RacingGameTest.java @@ -7,41 +7,39 @@ import java.util.ArrayList; import java.util.List; -import static org.hamcrest.CoreMatchers.*; +import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; public class RacingGameTest { - List cars; + List cars; + RacingGame racingGame; @Before public void setUp() throws Exception { cars = new ArrayList<>(); cars.add(new Car("애플")); cars.add(new Car("삼성")); cars.add(new Car("전남대")); + racingGame = new RacingGame(); } @Test - public void inputCarName() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { - String carName = "애플, 삼성, 전남대"; - InputView inputView = new InputView(); - try { - Method methodsplit = inputView.getClass().getDeclaredMethod("splitInputCarName", String.class); - methodsplit.setAccessible(true); - Method methodtrim = inputView.getClass().getDeclaredMethod("trimInputCarName", String[].class); - methodtrim.setAccessible(true); - String[] name = (String[]) methodsplit.invoke(inputView, carName); - String[] name1 = (String[]) methodtrim.invoke(inputView, (Object) name); - assertThat("애플", is(name1[0])); - assertThat("삼성", is(name1[1])); - assertThat("전남대", is(name1[2])); - } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { - e.printStackTrace(); + public void race() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + Method goOrStay = racingGame.getClass().getDeclaredMethod("goOrStay", Car.class); + goOrStay.setAccessible(true); + for(Car car: cars) { + goOrStay.invoke(racingGame, car); } } @Test - public void inputTrial() { - + public void getWinner() { + cars.get(0).goForward(); + cars.get(1).goForward(); + List expectedWinnerNames = new ArrayList<>(); + expectedWinnerNames.add("애플"); + expectedWinnerNames.add("삼성"); + List actualWinnerNames = racingGame.getWinner(cars); + assertThat(actualWinnerNames,is(expectedWinnerNames)); } @After