From 8d9603fc8e8635b5955cc679c0fb3afd2185c74d Mon Sep 17 00:00:00 2001 From: KIMJUNGMIN Date: Wed, 8 May 2019 12:11:32 +0900 Subject: [PATCH 01/45] =?UTF-8?q?=EC=88=AB=EC=9E=90=20=EB=BD=91=EC=95=84?= =?UTF-8?q?=EB=82=B4=EA=B8=B0=20test=20case=20=EB=A7=8C=EB=93=A4=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Calculator.java | 8 +++++ src/main/java/Calculator/Calculator.java | 37 ++++++++++++++++++++ src/test/java/Calculator/CalculatorTest.java | 31 ++++++++++++++++ src/test/java/CalculatorTest.java | 23 ++++++++++++ 4 files changed, 99 insertions(+) create mode 100644 src/main/java/Calculator.java create mode 100644 src/main/java/Calculator/Calculator.java create mode 100644 src/test/java/Calculator/CalculatorTest.java create mode 100644 src/test/java/CalculatorTest.java diff --git a/src/main/java/Calculator.java b/src/main/java/Calculator.java new file mode 100644 index 0000000000..b17a845909 --- /dev/null +++ b/src/main/java/Calculator.java @@ -0,0 +1,8 @@ +public class Calculator { + int addition(int i, int j) { + return i+j; + } + int subtraction(int i, int j) { + return i-j; + } +} diff --git a/src/main/java/Calculator/Calculator.java b/src/main/java/Calculator/Calculator.java new file mode 100644 index 0000000000..19fd242daa --- /dev/null +++ b/src/main/java/Calculator/Calculator.java @@ -0,0 +1,37 @@ +package Calculator; + + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Scanner; + +public class Calculator { + public static void main(String[] args) { + Calculator cal = new Calculator(); + System.out.println("숫자를 입력해 주세요: "); + String userInput = cal.readInput(); + List numbers = new ArrayList<>(); + numbers = Arrays.asList(userInput.split(" ")); + + } + + public String readInput() { + Scanner reader = new Scanner(System.in); + String userInput = reader.nextLine(); + return userInput; + } + + public List extractNumbers(List numbers) { + List listOfNumbers= new ArrayList<>(); + for (int i=0; i testList = new ArrayList<>(); + testList.add("2"); + testList.add("+"); + testList.add("3"); + List checkList = new ArrayList<>(); + checkList.add(2); + checkList.add(3); + assertThat(cal.extractNumbers(testList)).isEqualTo(checkList); + } + +} diff --git a/src/test/java/CalculatorTest.java b/src/test/java/CalculatorTest.java new file mode 100644 index 0000000000..65005a01f8 --- /dev/null +++ b/src/test/java/CalculatorTest.java @@ -0,0 +1,23 @@ +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class CalculatorTest { + Calculator cal; + + @BeforeEach + public void setUp() { + cal = new Calculator(); + } + + @Test + void addTest() { + assertThat(cal.addition(1,2)).isEqualTo(3); + } + + @Test + void subTest() { + assertThat(cal.subtraction(2,2)).isEqualTo(0); + } +} From 741d9e2a6d4d542a14c5a3342464256658b566c0 Mon Sep 17 00:00:00 2001 From: KIMJUNGMIN Date: Wed, 8 May 2019 12:16:00 +0900 Subject: [PATCH 02/45] =?UTF-8?q?=EB=AC=B8=EC=9E=90=20=EB=BD=91=EC=95=84?= =?UTF-8?q?=EB=82=B4=EA=B8=B0=20test=20case=20=EB=A7=8C=EB=93=A4=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Calculator/Calculator.java | 9 +++++++++ src/test/java/Calculator/CalculatorTest.java | 11 +++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/main/java/Calculator/Calculator.java b/src/main/java/Calculator/Calculator.java index 19fd242daa..ba5544795f 100644 --- a/src/main/java/Calculator/Calculator.java +++ b/src/main/java/Calculator/Calculator.java @@ -1,6 +1,7 @@ package Calculator; +import java.awt.image.AreaAveragingScaleFilter; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -31,6 +32,14 @@ public List extractNumbers(List numbers) { return listOfNumbers; } + public List extractSymbols(List symbols){ + List listOfSymbols = new ArrayList<>(); + for(int i=1; i testList = new ArrayList<>(); + testList.add("2"); + testList.add("+"); + testList.add("3"); + List checkList = new ArrayList<>(); + checkList.add("+"); + assertThat(cal.extractSymbols(testList)).isEqualTo(checkList); + } + } From 36eace4ae2dab07f24428fcd06f63ec93b0bc381 Mon Sep 17 00:00:00 2001 From: KIMJUNGMIN Date: Wed, 8 May 2019 13:57:40 +0900 Subject: [PATCH 03/45] =?UTF-8?q?=EC=82=AC=EC=B9=99=20=EC=97=B0=EC=82=B0?= =?UTF-8?q?=20=EC=A0=81=EC=9A=A9=20test=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Calculator/Calculator.java | 24 ++++++++++++++++++++ src/test/java/Calculator/CalculatorTest.java | 5 ++++ 2 files changed, 29 insertions(+) diff --git a/src/main/java/Calculator/Calculator.java b/src/main/java/Calculator/Calculator.java index ba5544795f..d8d87832de 100644 --- a/src/main/java/Calculator/Calculator.java +++ b/src/main/java/Calculator/Calculator.java @@ -40,6 +40,30 @@ public List extractSymbols(List symbols){ return listOfSymbols; } + public int calculate(ListlistOfNumbers, ListlistOfSymbols) { + int result = listOfNumbers.get(0); + for (int i=0; i Date: Wed, 8 May 2019 14:00:43 +0900 Subject: [PATCH 04/45] =?UTF-8?q?=EC=A0=84=EC=B2=B4=20=EA=B3=84=EC=82=B0?= =?UTF-8?q?=ED=95=98=EA=B8=B0=20test=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/Calculator/CalculatorTest.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/test/java/Calculator/CalculatorTest.java b/src/test/java/Calculator/CalculatorTest.java index 0dad3d952d..a312cf4804 100644 --- a/src/test/java/Calculator/CalculatorTest.java +++ b/src/test/java/Calculator/CalculatorTest.java @@ -39,6 +39,16 @@ void extractSymbolsTest() { assertThat(cal.extractSymbols(testList)).isEqualTo(checkList); } + @Test + void calculateTest(){ + List testList = new ArrayList<>(); + testList.add(2); + testList.add(3); + List checkList = new ArrayList<>(); + checkList.add("+"); + assertThat(cal.calculate(testList, checkList)).isEqualTo(5); + } + @Test void innerCalculateTest(){ assertThat(cal.innerCalculate(2, 3, "+")).isEqualTo(5); From 355fa4a7879555f0eb4de3d4d3cb3d7e4bcdee0e Mon Sep 17 00:00:00 2001 From: KIMJUNGMIN Date: Wed, 8 May 2019 15:51:51 +0900 Subject: [PATCH 05/45] =?UTF-8?q?=EC=98=88=EC=99=B8=EC=B2=98=EB=A6=AC:?= =?UTF-8?q?=EC=88=AB=EC=9E=90=EA=B0=80=20=EC=95=84=EB=8B=90=EA=B2=BD?= =?UTF-8?q?=EC=9A=B0=EC=97=90=20=EB=8C=80=ED=95=9C=20=EC=98=88=EC=99=B8?= =?UTF-8?q?=EC=B2=98=EB=A6=AC.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Calculator/Calculator.java | 29 ++++++----- .../java/Exceptions/CalculatorException.java | 49 +++++++++++++++++++ .../Exceptions/CalculatorExceptionTest.java | 13 +++++ 3 files changed, 79 insertions(+), 12 deletions(-) create mode 100644 src/main/java/Exceptions/CalculatorException.java create mode 100644 src/test/java/Exceptions/CalculatorExceptionTest.java diff --git a/src/main/java/Calculator/Calculator.java b/src/main/java/Calculator/Calculator.java index d8d87832de..dc4bc1aa7b 100644 --- a/src/main/java/Calculator/Calculator.java +++ b/src/main/java/Calculator/Calculator.java @@ -1,6 +1,8 @@ package Calculator; +import Exceptions.CalculatorException; + import java.awt.image.AreaAveragingScaleFilter; import java.util.ArrayList; import java.util.Arrays; @@ -9,25 +11,28 @@ public class Calculator { public static void main(String[] args) { - Calculator cal = new Calculator(); - System.out.println("숫자를 입력해 주세요: "); - String userInput = cal.readInput(); - List numbers = new ArrayList<>(); - numbers = Arrays.asList(userInput.split(" ")); - + doCalculate(); } - public String readInput() { - Scanner reader = new Scanner(System.in); - String userInput = reader.nextLine(); - return userInput; + public static void doCalculate() { + Calculator cal = new Calculator(); + String userInput = CalculatorException.readAndReceiveInput(); + List numbers = Arrays.asList(userInput.split(" ")); + List numberList = cal.extractNumbers(numbers); + List symbolList = cal.extractSymbols(numbers); + System.out.println(cal.calculate(numberList, symbolList)); + } public List extractNumbers(List numbers) { List listOfNumbers= new ArrayList<>(); for (int i=0; i numbers = new ArrayList<>(Arrays.asList(userInput.split(" "))); + boolean notAllNumber = false; + while (!notAllNumber && !numbers.isEmpty()) { + notAllNumber = checkNotAllNumber(numbers.get(0)); + numbers.remove(0); + numbers.remove(0); + } + return notAllNumber; + } + + public static boolean checkNotAllNumber(String number) { + if (number.matches("[0-9]+")){ + return false; + } + System.out.println("잘못된 입력값입니다!"); + return true; + } + +} diff --git a/src/test/java/Exceptions/CalculatorExceptionTest.java b/src/test/java/Exceptions/CalculatorExceptionTest.java new file mode 100644 index 0000000000..37d9c93737 --- /dev/null +++ b/src/test/java/Exceptions/CalculatorExceptionTest.java @@ -0,0 +1,13 @@ +package Exceptions; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class CalculatorExceptionTest { + @Test + void isNotAllNumberTest(){ + String data = "2 + 3 + t"; + assertThat(CalculatorException.isNotAllNumber(data)).isEqualTo(false); + } +} From dd953dbf42e58532e9f1a6686ee6f709ef34bde0 Mon Sep 17 00:00:00 2001 From: KIMJUNGMIN Date: Wed, 8 May 2019 16:19:47 +0900 Subject: [PATCH 06/45] =?UTF-8?q?java=20convention=EC=97=90=20=EB=A7=9E?= =?UTF-8?q?=EC=B6=94=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/Exceptions/CalculatorException.java | 49 ------------------- .../Calculator.java | 11 +++-- .../java/exceptions/CalculatorException.java | 22 +++++++++ .../Exceptions/CalculatorExceptionTest.java | 13 ----- .../CalculatorTest.java | 27 +++------- 5 files changed, 36 insertions(+), 86 deletions(-) delete mode 100644 src/main/java/Exceptions/CalculatorException.java rename src/main/java/{Calculator => calculator}/Calculator.java (92%) create mode 100644 src/main/java/exceptions/CalculatorException.java delete mode 100644 src/test/java/Exceptions/CalculatorExceptionTest.java rename src/test/java/{Calculator => calculator}/CalculatorTest.java (54%) diff --git a/src/main/java/Exceptions/CalculatorException.java b/src/main/java/Exceptions/CalculatorException.java deleted file mode 100644 index 3c37f115ed..0000000000 --- a/src/main/java/Exceptions/CalculatorException.java +++ /dev/null @@ -1,49 +0,0 @@ -package Exceptions; - -import Calculator.Calculator; -import edu.princeton.cs.algs4.StdOut; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Scanner; - -public class CalculatorException { - public static String readInputException(){ - try { - String userInput = readAndReceiveInput(); - if (isNotAllNumber(userInput)) { - throw new IllegalAccessException(); - } - return userInput; - }catch (Exception e){ - return readInputException(); - } - } - - public static String readAndReceiveInput() { - System.out.println("숫자를 입력해 주세요!"); - Scanner reader = new Scanner(System.in); - return reader.nextLine(); - } - - public static boolean isNotAllNumber(String userInput){ - List numbers = new ArrayList<>(Arrays.asList(userInput.split(" "))); - boolean notAllNumber = false; - while (!notAllNumber && !numbers.isEmpty()) { - notAllNumber = checkNotAllNumber(numbers.get(0)); - numbers.remove(0); - numbers.remove(0); - } - return notAllNumber; - } - - public static boolean checkNotAllNumber(String number) { - if (number.matches("[0-9]+")){ - return false; - } - System.out.println("잘못된 입력값입니다!"); - return true; - } - -} diff --git a/src/main/java/Calculator/Calculator.java b/src/main/java/calculator/Calculator.java similarity index 92% rename from src/main/java/Calculator/Calculator.java rename to src/main/java/calculator/Calculator.java index dc4bc1aa7b..52d6151e32 100644 --- a/src/main/java/Calculator/Calculator.java +++ b/src/main/java/calculator/Calculator.java @@ -1,13 +1,11 @@ -package Calculator; +package calculator; -import Exceptions.CalculatorException; +import exceptions.CalculatorException; -import java.awt.image.AreaAveragingScaleFilter; import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.Scanner; public class Calculator { public static void main(String[] args) { @@ -61,15 +59,18 @@ public int innerCalculate(int result, int number, String symbol) { return result - number; } if (symbol.equals("/")) { - return result/number; + return CalculatorException.divisionException(result, number); } if (symbol.equals("*")) { return result*number; } + doCalculate(); return -1; } + + } diff --git a/src/main/java/exceptions/CalculatorException.java b/src/main/java/exceptions/CalculatorException.java new file mode 100644 index 0000000000..ab2ab78f06 --- /dev/null +++ b/src/main/java/exceptions/CalculatorException.java @@ -0,0 +1,22 @@ +package exceptions; + +import calculator.Calculator; + +import java.util.Scanner; + +public class CalculatorException { + + public static String readAndReceiveInput() { + System.out.println("숫자를 입력해 주세요!"); + Scanner reader = new Scanner(System.in); + return reader.nextLine(); + } + + public static int divisionException(int result, int number){ + if (number == 0) { + Calculator.doCalculate(); + } + return result / number; + } + +} diff --git a/src/test/java/Exceptions/CalculatorExceptionTest.java b/src/test/java/Exceptions/CalculatorExceptionTest.java deleted file mode 100644 index 37d9c93737..0000000000 --- a/src/test/java/Exceptions/CalculatorExceptionTest.java +++ /dev/null @@ -1,13 +0,0 @@ -package Exceptions; - -import org.junit.jupiter.api.Test; - -import static org.assertj.core.api.Assertions.assertThat; - -public class CalculatorExceptionTest { - @Test - void isNotAllNumberTest(){ - String data = "2 + 3 + t"; - assertThat(CalculatorException.isNotAllNumber(data)).isEqualTo(false); - } -} diff --git a/src/test/java/Calculator/CalculatorTest.java b/src/test/java/calculator/CalculatorTest.java similarity index 54% rename from src/test/java/Calculator/CalculatorTest.java rename to src/test/java/calculator/CalculatorTest.java index a312cf4804..6e53e1443c 100644 --- a/src/test/java/Calculator/CalculatorTest.java +++ b/src/test/java/calculator/CalculatorTest.java @@ -1,9 +1,10 @@ -package Calculator; +package calculator; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import static org.assertj.core.api.Assertions.assertThat; @@ -18,34 +19,22 @@ void setup(){ @Test void extractNumbersTest() { - List testList = new ArrayList<>(); - testList.add("2"); - testList.add("+"); - testList.add("3"); - List checkList = new ArrayList<>(); - checkList.add(2); - checkList.add(3); + List testList = Arrays.asList("2", "+", "3"); + List checkList = Arrays.asList(2,3); assertThat(cal.extractNumbers(testList)).isEqualTo(checkList); } @Test void extractSymbolsTest() { - List testList = new ArrayList<>(); - testList.add("2"); - testList.add("+"); - testList.add("3"); - List checkList = new ArrayList<>(); - checkList.add("+"); + List testList = Arrays.asList("2", "+", "3"); + List checkList = Arrays.asList("+"); assertThat(cal.extractSymbols(testList)).isEqualTo(checkList); } @Test void calculateTest(){ - List testList = new ArrayList<>(); - testList.add(2); - testList.add(3); - List checkList = new ArrayList<>(); - checkList.add("+"); + List testList = Arrays.asList(2,3); + List checkList = Arrays.asList("+"); assertThat(cal.calculate(testList, checkList)).isEqualTo(5); } From a5f3c03f1c837faff34acd197af50ea5623b4c24 Mon Sep 17 00:00:00 2001 From: KIMJUNGMIN Date: Wed, 8 May 2019 16:55:20 +0900 Subject: [PATCH 07/45] =?UTF-8?q?=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81=20?= =?UTF-8?q?=EC=99=84=EB=A3=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/calculator/Calculator.java | 45 +++++++------------ .../java/exceptions/CalculatorException.java | 9 ++++ src/test/java/calculator/CalculatorTest.java | 3 +- 3 files changed, 26 insertions(+), 31 deletions(-) diff --git a/src/main/java/calculator/Calculator.java b/src/main/java/calculator/Calculator.java index 52d6151e32..17435120c4 100644 --- a/src/main/java/calculator/Calculator.java +++ b/src/main/java/calculator/Calculator.java @@ -13,29 +13,23 @@ public static void main(String[] args) { } public static void doCalculate() { - Calculator cal = new Calculator(); String userInput = CalculatorException.readAndReceiveInput(); - List numbers = Arrays.asList(userInput.split(" ")); - List numberList = cal.extractNumbers(numbers); - List symbolList = cal.extractSymbols(numbers); - System.out.println(cal.calculate(numberList, symbolList)); - + List numbers = new ArrayList<>(Arrays.asList(userInput.split(" "))); + List numberList = extractNumbers(numbers); + List symbolList = extractSymbols(numbers); + System.out.println(calculate(numberList, symbolList)); } - public List extractNumbers(List numbers) { + public static List extractNumbers(List numbers) { List listOfNumbers= new ArrayList<>(); for (int i=0; i extractSymbols(List symbols){ + public static List extractSymbols(List symbols){ List listOfSymbols = new ArrayList<>(); for(int i=1; i extractSymbols(List symbols){ return listOfSymbols; } - public int calculate(ListlistOfNumbers, ListlistOfSymbols) { + public static int calculate(ListlistOfNumbers, ListlistOfSymbols) { int result = listOfNumbers.get(0); for (int i=0; i Date: Wed, 8 May 2019 17:11:42 +0900 Subject: [PATCH 08/45] =?UTF-8?q?=EC=B5=9C=EC=A2=85=20=EB=A6=AC=ED=8C=A9?= =?UTF-8?q?=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 9 +++++++++ src/main/java/calculator/Calculator.java | 3 +-- src/main/java/exceptions/CalculatorException.java | 8 ++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 91744e0bd0..37a946c49e 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,12 @@ +# 1번째 미션 - 문자열 계산기 +## 기능 구현 목록 +1. [입력] 유저에게 인풋을 받는다. +2. [처리] 유저의 인풋을 숫자와 기호로 나눈다. +* [예외처리] 유저의 인풋이 숫자가 아닌 경우를 확인한다. +* [예외처리] 유저의 인풋이 기호가 아닌 경우를 확인한다. +* [예외처리] 유저의 인풋이 Zero-Division인지 확인한다. +3. [결과] 해당식의 결과를 출력한다. + # java-racingcar 자동차 경주 게임 미션 저장소 diff --git a/src/main/java/calculator/Calculator.java b/src/main/java/calculator/Calculator.java index 17435120c4..c95fcab23f 100644 --- a/src/main/java/calculator/Calculator.java +++ b/src/main/java/calculator/Calculator.java @@ -51,8 +51,7 @@ public static int applyCalculation(int result, int number, String symbol) { if (symbol.equals("/")) return CalculatorException.divisionException(result, number); if (symbol.equals("*")) return result*number; - doCalculate(); - return -1; + return CalculatorException.applyCalculationException(); } diff --git a/src/main/java/exceptions/CalculatorException.java b/src/main/java/exceptions/CalculatorException.java index e47b9538a7..d9b41dcb9a 100644 --- a/src/main/java/exceptions/CalculatorException.java +++ b/src/main/java/exceptions/CalculatorException.java @@ -14,6 +14,7 @@ public static String readAndReceiveInput() { public static int divisionException(int result, int number){ if (number == 0) { + System.out.println("0이 분모에 있습니다."); Calculator.doCalculate(); } return result / number; @@ -23,9 +24,16 @@ public static int extractNumbersException(String stringNumber) { try{ return Integer.parseInt(stringNumber); }catch (Exception e){ + System.out.println("잘못된 입력값입니다! 숫자가 아닙니다!"); Calculator.doCalculate(); } return -1; } + public static int applyCalculationException(){ + System.out.println("잘못된 입력값입니다. 기호가 아닙니다!"); + Calculator.doCalculate(); + return -1; + } + } From 7e795e8ca841f68eb829503c9316d2af7ec6d49a Mon Sep 17 00:00:00 2001 From: KIMJUNGMIN Date: Wed, 8 May 2019 18:54:34 +0900 Subject: [PATCH 09/45] =?UTF-8?q?=EC=9E=90=EB=8F=99=EC=B0=A8=20=EC=9D=B4?= =?UTF-8?q?=EB=A6=84=20=EC=9E=85=EB=A0=A5=20=EB=B0=8F=20=EC=98=88=EC=99=B8?= =?UTF-8?q?=20=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 11 ++++ src/main/java/calculator/Calculator.java | 2 +- .../CalculatorException.java | 2 +- src/main/java/racingcar/Car.java | 25 +++++++++ src/main/java/racingcar/CarException.java | 53 +++++++++++++++++++ src/main/java/racingcar/CarGameLauncher.java | 7 +++ src/test/java/racingcar/CarExceptionTest.java | 35 ++++++++++++ src/test/java/racingcar/CarTest.java | 4 ++ 8 files changed, 137 insertions(+), 2 deletions(-) rename src/main/java/{exceptions => exceptionsCalculator}/CalculatorException.java (97%) create mode 100644 src/main/java/racingcar/Car.java create mode 100644 src/main/java/racingcar/CarException.java create mode 100644 src/main/java/racingcar/CarGameLauncher.java create mode 100644 src/test/java/racingcar/CarExceptionTest.java create mode 100644 src/test/java/racingcar/CarTest.java diff --git a/README.md b/README.md index 37a946c49e..50639cc136 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,17 @@ * [예외처리] 유저의 인풋이 Zero-Division인지 확인한다. 3. [결과] 해당식의 결과를 출력한다. +#2번째 미션 - java-racingcar +## 기능 구현 목록 +1. [입력] 유저에게 자동차의 이름을 입력받는다. +* [예외처리] 이름의 공백이 경우를 확인한다. +* [예외처리] 중복된 이름이 있는 경우 +* [예외처리] 이름이의 길이가 다섯자 이하인지 확인한다. +2. [입력] 유저에게 시도학 횟수를 입력받는다. +* [예외처리] 0이하의 숫자인지 확인한다. +3. [계산/출력] 자동차가 간 거리를 계산한다. +4. [결과출력] 우승자를 출력한다. + # java-racingcar 자동차 경주 게임 미션 저장소 diff --git a/src/main/java/calculator/Calculator.java b/src/main/java/calculator/Calculator.java index c95fcab23f..87b3a969f7 100644 --- a/src/main/java/calculator/Calculator.java +++ b/src/main/java/calculator/Calculator.java @@ -1,7 +1,7 @@ package calculator; -import exceptions.CalculatorException; +import exceptionsCalculator.CalculatorException; import java.util.ArrayList; import java.util.Arrays; diff --git a/src/main/java/exceptions/CalculatorException.java b/src/main/java/exceptionsCalculator/CalculatorException.java similarity index 97% rename from src/main/java/exceptions/CalculatorException.java rename to src/main/java/exceptionsCalculator/CalculatorException.java index d9b41dcb9a..e302f4864d 100644 --- a/src/main/java/exceptions/CalculatorException.java +++ b/src/main/java/exceptionsCalculator/CalculatorException.java @@ -1,4 +1,4 @@ -package exceptions; +package exceptionsCalculator; import calculator.Calculator; diff --git a/src/main/java/racingcar/Car.java b/src/main/java/racingcar/Car.java new file mode 100644 index 0000000000..ef9556dc31 --- /dev/null +++ b/src/main/java/racingcar/Car.java @@ -0,0 +1,25 @@ +package racingcar; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Scanner; + +public class Car { + private final String name; + private int position; + + Car(String name, int position) { + this.name = name; + this.position = position; + } + + public static List askCarNames(){ + Scanner scanner = new Scanner(System.in); + String names = scanner.nextLine(); + names = names.replaceAll("\\s+",""); + List nameList = new ArrayList<>(Arrays.asList(names.split(","))); + return CarException.askCarNamesException(nameList); + } + +} diff --git a/src/main/java/racingcar/CarException.java b/src/main/java/racingcar/CarException.java new file mode 100644 index 0000000000..219fda3292 --- /dev/null +++ b/src/main/java/racingcar/CarException.java @@ -0,0 +1,53 @@ +package racingcar; + +import racingcar.Car; + +import java.util.*; + +public class CarException { + public static List askCarNamesException(List names){ + try { + if (isWhiteSpaceOnly(names) || isDuplicate(names) || isOverLimit(names)){ + throw new IllegalArgumentException(); + } + return names; + } catch(Exception e) { + return Car.askCarNames(); + } + } + + public static boolean isWhiteSpaceOnly(List names) { + boolean result = false; + for(int i=0; i names) { + Set nameSet = new HashSet<>(names); + if (names.size() != nameSet.size()) { + System.out.println("이름에 중복이 있습니다!"); + return true; + } + return false; + } + + public static boolean isOverLimit(List names) { + boolean overLimit = false; + while (!overLimit && !names.isEmpty()) { + overLimit = checkLimit(names.get(0)); + names.remove(0); + } + return overLimit; + } + + public static boolean checkLimit(String name) { + if (name.length() > 5 || name.length() <= 0) { + System.out.println("이름은 0글자 이상 5글자 이하만 입력해 주세요!"); + return true; + } + return false; + } +} diff --git a/src/main/java/racingcar/CarGameLauncher.java b/src/main/java/racingcar/CarGameLauncher.java new file mode 100644 index 0000000000..17fba81fc5 --- /dev/null +++ b/src/main/java/racingcar/CarGameLauncher.java @@ -0,0 +1,7 @@ +package racingcar; + +public class CarGameLauncher { + public static void main(String[] args) { + + } +} diff --git a/src/test/java/racingcar/CarExceptionTest.java b/src/test/java/racingcar/CarExceptionTest.java new file mode 100644 index 0000000000..d13bf539d6 --- /dev/null +++ b/src/test/java/racingcar/CarExceptionTest.java @@ -0,0 +1,35 @@ +package racingcar; + +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +public class CarExceptionTest { + @Test + void checkLimit(){ + String name = "asdas"; + assertThat(CarException.checkLimit(name)).isEqualTo(false); + } + + @Test + void isOverLimit(){ + List nameList = Arrays.asList("asd", "asd"); + assertThat(CarException.isOverLimit(nameList)).isEqualTo(true); + } + + @Test + void isWhiteSpaceOnly(){ + List nameList = Arrays.asList("asd", "", "asd"); + assertThat(CarException.isWhiteSpaceOnly(nameList)).isEqualTo(true); + } + + @Test + void isDuplicate() { + List nameList = Arrays.asList("asd", "ddd", "asd"); + assertThat(CarException.isDuplicate(nameList)).isEqualTo(true); + } + +} diff --git a/src/test/java/racingcar/CarTest.java b/src/test/java/racingcar/CarTest.java new file mode 100644 index 0000000000..a9bd0d9f00 --- /dev/null +++ b/src/test/java/racingcar/CarTest.java @@ -0,0 +1,4 @@ +package racingcar; + +public class CarTest { +} From 04d5e48cfdb57f18739a038f048cc1a77763d618 Mon Sep 17 00:00:00 2001 From: KIMJUNGMIN Date: Wed, 8 May 2019 19:06:57 +0900 Subject: [PATCH 10/45] =?UTF-8?q?=EC=9E=90=EB=8F=99=EC=B0=A8=20=EA=B2=8C?= =?UTF-8?q?=EC=9E=84=EC=9D=98=20=ED=9A=9F=EC=88=98=EB=A5=BC=20=EC=9E=85?= =?UTF-8?q?=EB=A0=A5=EB=B0=9B=EA=B3=A0=20=EC=98=88=EC=99=B8=EC=B2=98?= =?UTF-8?q?=EB=A6=AC=EB=A5=BC=20=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/Car.java | 4 ++++ src/main/java/racingcar/CarException.java | 17 +++++++++++++++++ src/test/java/racingcar/CarExceptionTest.java | 1 + 3 files changed, 22 insertions(+) diff --git a/src/main/java/racingcar/Car.java b/src/main/java/racingcar/Car.java index ef9556dc31..ba5da78417 100644 --- a/src/main/java/racingcar/Car.java +++ b/src/main/java/racingcar/Car.java @@ -22,4 +22,8 @@ public static List askCarNames(){ return CarException.askCarNamesException(nameList); } + public static int askTotalTurns() { + return CarException.askTotalTurnsException(); + } + } diff --git a/src/main/java/racingcar/CarException.java b/src/main/java/racingcar/CarException.java index 219fda3292..70424657c9 100644 --- a/src/main/java/racingcar/CarException.java +++ b/src/main/java/racingcar/CarException.java @@ -50,4 +50,21 @@ public static boolean checkLimit(String name) { } return false; } + + public static int askTotalTurnsException() { + try { + int userInput = askAndReceiveTurns(); + if (userInput <= 0) {throw new IllegalArgumentException();} + return userInput; + }catch(Exception e) { + return askTotalTurnsException(); + } + } + + public static int askAndReceiveTurns() { + System.out.println("시도할 횟수는 몇회인가요?: "); + Scanner reader = new Scanner(System.in); + int userInput = reader.nextInt(); + return userInput; + } } diff --git a/src/test/java/racingcar/CarExceptionTest.java b/src/test/java/racingcar/CarExceptionTest.java index d13bf539d6..68f7f210e7 100644 --- a/src/test/java/racingcar/CarExceptionTest.java +++ b/src/test/java/racingcar/CarExceptionTest.java @@ -32,4 +32,5 @@ void isDuplicate() { assertThat(CarException.isDuplicate(nameList)).isEqualTo(true); } + } From 9bf8ef59ff0ccec76423bf732761908f4f9e6a15 Mon Sep 17 00:00:00 2001 From: KIMJUNGMIN Date: Wed, 8 May 2019 19:11:07 +0900 Subject: [PATCH 11/45] =?UTF-8?q?=EC=9E=90=EB=8F=99=EC=B0=A8=20=EC=9D=B4?= =?UTF-8?q?=EB=A6=84=20=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/Car.java | 6 +----- src/main/java/racingcar/CarException.java | 11 ++++++++++- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/main/java/racingcar/Car.java b/src/main/java/racingcar/Car.java index ba5da78417..4bec1c090a 100644 --- a/src/main/java/racingcar/Car.java +++ b/src/main/java/racingcar/Car.java @@ -15,11 +15,7 @@ public class Car { } public static List askCarNames(){ - Scanner scanner = new Scanner(System.in); - String names = scanner.nextLine(); - names = names.replaceAll("\\s+",""); - List nameList = new ArrayList<>(Arrays.asList(names.split(","))); - return CarException.askCarNamesException(nameList); + return CarException.askCarNamesException(); } public static int askTotalTurns() { diff --git a/src/main/java/racingcar/CarException.java b/src/main/java/racingcar/CarException.java index 70424657c9..98f245b935 100644 --- a/src/main/java/racingcar/CarException.java +++ b/src/main/java/racingcar/CarException.java @@ -5,8 +5,9 @@ import java.util.*; public class CarException { - public static List askCarNamesException(List names){ + public static List askCarNamesException(){ try { + List names = askAndReceiveCarNames(); if (isWhiteSpaceOnly(names) || isDuplicate(names) || isOverLimit(names)){ throw new IllegalArgumentException(); } @@ -16,6 +17,14 @@ public static List askCarNamesException(List names){ } } + public static List askAndReceiveCarNames(){ + Scanner scanner = new Scanner(System.in); + String names = scanner.nextLine(); + names = names.replaceAll("\\s+",""); + List nameList = new ArrayList<>(Arrays.asList(names.split(","))); + return nameList; + } + public static boolean isWhiteSpaceOnly(List names) { boolean result = false; for(int i=0; i Date: Thu, 9 May 2019 10:17:04 +0900 Subject: [PATCH 12/45] =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=88=98=EC=A0=95.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/CarGameLauncher.java | 5 +++++ src/test/java/racingcar/CarExceptionTest.java | 11 ++++++----- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/main/java/racingcar/CarGameLauncher.java b/src/main/java/racingcar/CarGameLauncher.java index 17fba81fc5..42da536554 100644 --- a/src/main/java/racingcar/CarGameLauncher.java +++ b/src/main/java/racingcar/CarGameLauncher.java @@ -1,7 +1,12 @@ package racingcar; +import java.util.*; + public class CarGameLauncher { public static void main(String[] args) { + List nameList = new ArrayList<>(Arrays.asList("asddfdfd", "asd")); + boolean test = CarException.isOverLimit(nameList); + System.out.println(test); } } diff --git a/src/test/java/racingcar/CarExceptionTest.java b/src/test/java/racingcar/CarExceptionTest.java index 68f7f210e7..03e5725787 100644 --- a/src/test/java/racingcar/CarExceptionTest.java +++ b/src/test/java/racingcar/CarExceptionTest.java @@ -4,30 +4,31 @@ import java.util.Arrays; import java.util.List; +import java.util.*; import static org.assertj.core.api.Assertions.assertThat; public class CarExceptionTest { @Test - void checkLimit(){ + void checkLimitTest(){ String name = "asdas"; assertThat(CarException.checkLimit(name)).isEqualTo(false); } @Test - void isOverLimit(){ - List nameList = Arrays.asList("asd", "asd"); + void isOverLimitTest(){ + List nameList = new ArrayList<>(Arrays.asList("asddfdfd", "asd")); assertThat(CarException.isOverLimit(nameList)).isEqualTo(true); } @Test - void isWhiteSpaceOnly(){ + void isWhiteSpaceOnlyTest(){ List nameList = Arrays.asList("asd", "", "asd"); assertThat(CarException.isWhiteSpaceOnly(nameList)).isEqualTo(true); } @Test - void isDuplicate() { + void isDuplicateTest() { List nameList = Arrays.asList("asd", "ddd", "asd"); assertThat(CarException.isDuplicate(nameList)).isEqualTo(true); } From 00b3923bd580bb9e4fdf4d06e2bcf16e39d22bbc Mon Sep 17 00:00:00 2001 From: KIMJUNGMIN Date: Thu, 9 May 2019 11:28:35 +0900 Subject: [PATCH 13/45] =?UTF-8?q?=EC=9E=90=EB=8F=99=EC=B0=A8=20=EC=9D=B4?= =?UTF-8?q?=EB=A6=84=20=EB=93=B1=EB=A1=9D=ED=95=98=EA=B8=B0=20=EB=B0=8F=20?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/Car.java | 8 ++++---- src/main/java/racingcar/CarGameLauncher.java | 9 ++++++--- src/main/java/racingcar/Cars.java | 21 ++++++++++++++++++++ src/test/java/racingcar/CarTest.java | 4 ---- src/test/java/racingcar/CarsTest.java | 16 +++++++++++++++ 5 files changed, 47 insertions(+), 11 deletions(-) create mode 100644 src/main/java/racingcar/Cars.java delete mode 100644 src/test/java/racingcar/CarTest.java create mode 100644 src/test/java/racingcar/CarsTest.java diff --git a/src/main/java/racingcar/Car.java b/src/main/java/racingcar/Car.java index 4bec1c090a..aee2817793 100644 --- a/src/main/java/racingcar/Car.java +++ b/src/main/java/racingcar/Car.java @@ -5,13 +5,12 @@ import java.util.List; import java.util.Scanner; -public class Car { +public class Car{ private final String name; - private int position; + private int position = 0; - Car(String name, int position) { + Car(String name) { this.name = name; - this.position = position; } public static List askCarNames(){ @@ -22,4 +21,5 @@ public static int askTotalTurns() { return CarException.askTotalTurnsException(); } + } diff --git a/src/main/java/racingcar/CarGameLauncher.java b/src/main/java/racingcar/CarGameLauncher.java index 42da536554..55aab53ef2 100644 --- a/src/main/java/racingcar/CarGameLauncher.java +++ b/src/main/java/racingcar/CarGameLauncher.java @@ -4,9 +4,12 @@ public class CarGameLauncher { public static void main(String[] args) { - List nameList = new ArrayList<>(Arrays.asList("asddfdfd", "asd")); - boolean test = CarException.isOverLimit(nameList); - System.out.println(test); + doCarGame(); + } + private static void doCarGame() { + List listOfCarNames = Car.askCarNames(); + int totalTurns = Car.askTotalTurns(); + Cars cars = new Cars(listOfCarNames); } } diff --git a/src/main/java/racingcar/Cars.java b/src/main/java/racingcar/Cars.java new file mode 100644 index 0000000000..343b904a9e --- /dev/null +++ b/src/main/java/racingcar/Cars.java @@ -0,0 +1,21 @@ +package racingcar; + +import java.util.*; + +public class Cars { + List carList = new ArrayList<>(); + + Cars(List carNames){ + isMakeCar(carNames); + } + + public boolean isMakeCar(List carNames){ + boolean result = false; + for(int i=0; i nameList = Arrays.asList("a", "b"); + Cars cars = new Cars(nameList); + assertThat(cars.isMakeCar(nameList)).isEqualTo(true); + } +} From ee55775448678de34abe8e913b07659a76bcc96a Mon Sep 17 00:00:00 2001 From: KIMJUNGMIN Date: Thu, 9 May 2019 14:09:16 +0900 Subject: [PATCH 14/45] =?UTF-8?q?=EC=9E=90=EB=8F=99=EC=B0=A8=20=EC=9D=B4?= =?UTF-8?q?=EB=8F=99=EA=B1=B0=EB=A6=AC=20=EA=B3=84=EC=82=B0=20=EB=B0=8F=20?= =?UTF-8?q?=EC=B6=9C=EB=A0=A5=EA=B3=BC=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EC=9E=91=EC=84=B1.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/Car.java | 18 +++++++++++++++ src/main/java/racingcar/CarGameLauncher.java | 1 + src/main/java/racingcar/Cars.java | 24 +++++++++++++------- src/test/java/racingcar/CarsTest.java | 16 ++++++++----- 4 files changed, 45 insertions(+), 14 deletions(-) diff --git a/src/main/java/racingcar/Car.java b/src/main/java/racingcar/Car.java index aee2817793..f3ca6fc982 100644 --- a/src/main/java/racingcar/Car.java +++ b/src/main/java/racingcar/Car.java @@ -21,5 +21,23 @@ public static int askTotalTurns() { return CarException.askTotalTurnsException(); } + public void moveCar(){ + int randomNumber = (int)(Math.random() * 10); + if(randomNumber >= 4){ + position++; + } + } + + public static int randomNumberGenerator(){ + return (int) (Math.random() * 10); + } + + public void printPosition(){ + System.out.print(this.name + " : "); + for(int i=0; i listOfCarNames = Car.askCarNames(); int totalTurns = Car.askTotalTurns(); Cars cars = new Cars(listOfCarNames); + cars.printPosition(totalTurns); } } diff --git a/src/main/java/racingcar/Cars.java b/src/main/java/racingcar/Cars.java index 343b904a9e..0ef21e3137 100644 --- a/src/main/java/racingcar/Cars.java +++ b/src/main/java/racingcar/Cars.java @@ -3,19 +3,27 @@ import java.util.*; public class Cars { - List carList = new ArrayList<>(); + private List carList = new ArrayList<>(); Cars(List carNames){ - isMakeCar(carNames); - } - - public boolean isMakeCar(List carNames){ - boolean result = false; for(int i=0; i nameList = Arrays.asList("a", "b"); - Cars cars = new Cars(nameList); - assertThat(cars.isMakeCar(nameList)).isEqualTo(true); - } + @Test + void randomNumberGeneratorTest(){ + int random = Car.randomNumberGenerator(); + System.out.println(random); + if(random >= 4){ + assertThat(random >= 4).isEqualTo(true); + }if(random < 4){ + assertThat(random < 4).isEqualTo(true); + } + } } From e9009e27ec96e8e0de353dc34b752ea61129bb80 Mon Sep 17 00:00:00 2001 From: KIMJUNGMIN Date: Thu, 9 May 2019 14:31:47 +0900 Subject: [PATCH 15/45] =?UTF-8?q?=EC=9A=B0=EC=8A=B9=EC=9E=90=20=EC=B5=9C?= =?UTF-8?q?=EB=8C=80=20=EA=B1=B0=EB=A6=AC=20=EC=95=8C=EC=95=84=EB=82=B4?= =?UTF-8?q?=EA=B8=B0=20=EB=B0=8F=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/Car.java | 9 ++++++- src/main/java/racingcar/CarGameLauncher.java | 3 ++- src/main/java/racingcar/Cars.java | 6 ++--- src/main/java/racingcar/Winners.java | 22 +++++++++++++++++ src/test/java/racingcar/CarTest.java | 26 ++++++++++++++++++++ src/test/java/racingcar/CarsTest.java | 11 +-------- 6 files changed, 62 insertions(+), 15 deletions(-) create mode 100644 src/main/java/racingcar/Winners.java create mode 100644 src/test/java/racingcar/CarTest.java diff --git a/src/main/java/racingcar/Car.java b/src/main/java/racingcar/Car.java index f3ca6fc982..efc9ae8d61 100644 --- a/src/main/java/racingcar/Car.java +++ b/src/main/java/racingcar/Car.java @@ -22,7 +22,7 @@ public static int askTotalTurns() { } public void moveCar(){ - int randomNumber = (int)(Math.random() * 10); + int randomNumber = randomNumberGenerator(); if(randomNumber >= 4){ position++; } @@ -39,5 +39,12 @@ public void printPosition(){ } } + public int compare(int max) { + if (position > max) { + max = position; + } + return max; + } + } diff --git a/src/main/java/racingcar/CarGameLauncher.java b/src/main/java/racingcar/CarGameLauncher.java index 20ab1bfb86..9b8ee25bc2 100644 --- a/src/main/java/racingcar/CarGameLauncher.java +++ b/src/main/java/racingcar/CarGameLauncher.java @@ -11,6 +11,7 @@ private static void doCarGame() { List listOfCarNames = Car.askCarNames(); int totalTurns = Car.askTotalTurns(); Cars cars = new Cars(listOfCarNames); - cars.printPosition(totalTurns); + cars.printTheProcess(totalTurns); + } } diff --git a/src/main/java/racingcar/Cars.java b/src/main/java/racingcar/Cars.java index 0ef21e3137..6fba46fdf2 100644 --- a/src/main/java/racingcar/Cars.java +++ b/src/main/java/racingcar/Cars.java @@ -11,13 +11,13 @@ public class Cars { } } - public void printPosition(int totalTurns) { + public void printTheProcess(int totalTurns) { for (int currentTurn = 0; currentTurn Winners = new ArrayList<>(); + + Winners(List listOfCars) { + //listOfCars에서 가장 높은 position탐색 + int maxPosition = decideMaxPosition(listOfCars); + //for loop롤 cars돌면서 가장 높은 position를 갖고 있는 차들 Winners등록 + } + + public static int decideMaxPosition(List listOfCars) { + int max = 0; + for (int i=0, n=listOfCars.size(); i= 4){ + System.out.println("is bigger than 4"); + assertThat(random >= 4).isEqualTo(true); + }if(random < 4){ + System.out.println("is less than 4"); + assertThat(random < 4).isEqualTo(true); + } + } + + @Test + void compareTest() { + Car testCar = new Car("abc"); + assertThat(testCar.compare(101)).isEqualTo(101); + } +} diff --git a/src/test/java/racingcar/CarsTest.java b/src/test/java/racingcar/CarsTest.java index 7ef4857c1a..c112c7ed4a 100644 --- a/src/test/java/racingcar/CarsTest.java +++ b/src/test/java/racingcar/CarsTest.java @@ -7,14 +7,5 @@ import static org.assertj.core.api.Assertions.assertThat; public class CarsTest { - @Test - void randomNumberGeneratorTest(){ - int random = Car.randomNumberGenerator(); - System.out.println(random); - if(random >= 4){ - assertThat(random >= 4).isEqualTo(true); - }if(random < 4){ - assertThat(random < 4).isEqualTo(true); - } - } + } From de9cf4bd665c6b8c67861589d4eda94f35ba0d45 Mon Sep 17 00:00:00 2001 From: KIMJUNGMIN Date: Thu, 9 May 2019 14:38:37 +0900 Subject: [PATCH 16/45] =?UTF-8?q?=EC=9E=90=EB=8F=99=EC=B0=A8=20=EC=B5=9C?= =?UTF-8?q?=EC=A2=85=20=EA=B1=B0=EB=A6=AC=20=ED=99=95=EC=9D=B8=ED=95=98?= =?UTF-8?q?=EB=8A=94=20=EB=A9=94=EC=86=8C=EB=93=9C=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/Car.java | 4 ++++ src/main/java/racingcar/Winners.java | 1 + src/test/java/racingcar/WinnersTest.java | 26 ++++++++++++++++++++++++ 3 files changed, 31 insertions(+) create mode 100644 src/test/java/racingcar/WinnersTest.java diff --git a/src/main/java/racingcar/Car.java b/src/main/java/racingcar/Car.java index efc9ae8d61..7d12d6f3c2 100644 --- a/src/main/java/racingcar/Car.java +++ b/src/main/java/racingcar/Car.java @@ -46,5 +46,9 @@ public int compare(int max) { return max; } + public void setPosition(int position){ + this.position = position; + } + } diff --git a/src/main/java/racingcar/Winners.java b/src/main/java/racingcar/Winners.java index a7c588d8a5..fed16d8fdc 100644 --- a/src/main/java/racingcar/Winners.java +++ b/src/main/java/racingcar/Winners.java @@ -19,4 +19,5 @@ public static int decideMaxPosition(List listOfCars) { return max; } + } diff --git a/src/test/java/racingcar/WinnersTest.java b/src/test/java/racingcar/WinnersTest.java new file mode 100644 index 0000000000..345912ec97 --- /dev/null +++ b/src/test/java/racingcar/WinnersTest.java @@ -0,0 +1,26 @@ +package racingcar; + + +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.*; + +class WinnersTest { + @Test + void decideMaxPositionTest() { + Car testCar1 = new Car("aaa"); + testCar1.setPosition(5); + Car testCar2 = new Car("bbb"); + testCar2.setPosition(2); + Car testCar3 = new Car("ccc"); + testCar3.setPosition(100); + + List carList = Arrays.asList(testCar1, testCar2, testCar3); + assertThat(Winners.decideMaxPosition(carList)).isEqualTo(100); + } + +} \ No newline at end of file From eac0a1176a931fe16c5bc84424b69c048fd93690 Mon Sep 17 00:00:00 2001 From: KIMJUNGMIN Date: Thu, 9 May 2019 15:45:21 +0900 Subject: [PATCH 17/45] =?UTF-8?q?=EC=9E=90=EB=8F=99=EC=B0=A8=20=EC=B5=9C?= =?UTF-8?q?=EC=A2=85=20=EA=B1=B0=EB=A6=AC=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/Car.java | 10 ++++++++- src/main/java/racingcar/CarGameLauncher.java | 2 ++ src/main/java/racingcar/Cars.java | 22 ++++++++++++++++++++ src/main/java/racingcar/Winners.java | 21 +++++++------------ src/test/java/racingcar/CarTest.java | 2 +- src/test/java/racingcar/CarsTest.java | 8 +++++++ src/test/java/racingcar/WinnersTest.java | 5 +---- 7 files changed, 51 insertions(+), 19 deletions(-) diff --git a/src/main/java/racingcar/Car.java b/src/main/java/racingcar/Car.java index 7d12d6f3c2..696911cb8f 100644 --- a/src/main/java/racingcar/Car.java +++ b/src/main/java/racingcar/Car.java @@ -39,14 +39,22 @@ public void printPosition(){ } } - public int compare(int max) { + public int findMax(int max) { if (position > max) { max = position; } return max; } + public void findScoreSameAsMax(int maxPosition, List winners){ + if(maxPosition == this.position){ + winners.add(this); + } + } + public void setPosition(int position){ + + //테스트용 this.position = position; } diff --git a/src/main/java/racingcar/CarGameLauncher.java b/src/main/java/racingcar/CarGameLauncher.java index 9b8ee25bc2..a41f127264 100644 --- a/src/main/java/racingcar/CarGameLauncher.java +++ b/src/main/java/racingcar/CarGameLauncher.java @@ -12,6 +12,8 @@ private static void doCarGame() { int totalTurns = Car.askTotalTurns(); Cars cars = new Cars(listOfCarNames); cars.printTheProcess(totalTurns); + Winners winners = new Winners(); + winners.decideWinners(cars); } } diff --git a/src/main/java/racingcar/Cars.java b/src/main/java/racingcar/Cars.java index 6fba46fdf2..8e2301f400 100644 --- a/src/main/java/racingcar/Cars.java +++ b/src/main/java/racingcar/Cars.java @@ -24,6 +24,28 @@ public void printMovement() { } } + public int decideMaxPosition() { + int max = 0; + for (int i=0, n=this.carList.size(); i decideWinners(int maxPosition) { + List winners = new ArrayList<>(); + for(int i=0; i inputPosition){ + for(int i=0; i Winners = new ArrayList<>(); - - Winners(List listOfCars) { - //listOfCars에서 가장 높은 position탐색 - int maxPosition = decideMaxPosition(listOfCars); - //for loop롤 cars돌면서 가장 높은 position를 갖고 있는 차들 Winners등록 + private List listOfWinners; + /* + public void decideWinners(Cars cars) { + int maxPosition = ; + this.decideWinningCars(cars, maxPosition); } - - public static int decideMaxPosition(List listOfCars) { - int max = 0; - for (int i=0, n=listOfCars.size(); i carNames = Arrays.asList("a", "b", "c"); + List carPositions = Arrays.asList(5, 100, 1000); + Cars testCars = new Cars(carNames); + testCars.setPosition(carPositions); + assertThat(testCars.decideMaxPosition()).isEqualTo(1000); + } } diff --git a/src/test/java/racingcar/WinnersTest.java b/src/test/java/racingcar/WinnersTest.java index 345912ec97..7ffd88fb34 100644 --- a/src/test/java/racingcar/WinnersTest.java +++ b/src/test/java/racingcar/WinnersTest.java @@ -15,12 +15,9 @@ void decideMaxPositionTest() { Car testCar1 = new Car("aaa"); testCar1.setPosition(5); Car testCar2 = new Car("bbb"); - testCar2.setPosition(2); + testCar2.setPosition(100); Car testCar3 = new Car("ccc"); testCar3.setPosition(100); - - List carList = Arrays.asList(testCar1, testCar2, testCar3); - assertThat(Winners.decideMaxPosition(carList)).isEqualTo(100); } } \ No newline at end of file From 40c500d0736af5162ee1d4b4066791767b712605 Mon Sep 17 00:00:00 2001 From: KIMJUNGMIN Date: Thu, 9 May 2019 16:02:44 +0900 Subject: [PATCH 18/45] =?UTF-8?q?=EC=B5=9C=EC=A2=85=EC=9A=B0=EC=8A=B9?= =?UTF-8?q?=EC=9E=90=20=EA=B5=AC=ED=95=98=EB=8A=94=20method=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=EC=BD=94=EB=93=9C=20=EC=9E=91=EC=84=B1.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/Car.java | 4 ++-- src/main/java/racingcar/Cars.java | 8 ++++---- src/main/java/racingcar/Winners.java | 2 +- src/test/java/racingcar/CarsTest.java | 10 ++++++++++ 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/main/java/racingcar/Car.java b/src/main/java/racingcar/Car.java index 696911cb8f..ff9d24451d 100644 --- a/src/main/java/racingcar/Car.java +++ b/src/main/java/racingcar/Car.java @@ -46,9 +46,9 @@ public int findMax(int max) { return max; } - public void findScoreSameAsMax(int maxPosition, List winners){ + public void findScoreSameAsMax(int maxPosition, List winners){ if(maxPosition == this.position){ - winners.add(this); + winners.add(this.name); } } diff --git a/src/main/java/racingcar/Cars.java b/src/main/java/racingcar/Cars.java index 8e2301f400..3190806d2f 100644 --- a/src/main/java/racingcar/Cars.java +++ b/src/main/java/racingcar/Cars.java @@ -32,12 +32,12 @@ public int decideMaxPosition() { return max; } - public List decideWinners(int maxPosition) { - List winners = new ArrayList<>(); + public List decideWinners(int maxPosition) { + List winnersName = new ArrayList<>(); for(int i=0; i inputPosition){ diff --git a/src/main/java/racingcar/Winners.java b/src/main/java/racingcar/Winners.java index 409dc602d8..d1ad1975b3 100644 --- a/src/main/java/racingcar/Winners.java +++ b/src/main/java/racingcar/Winners.java @@ -3,7 +3,7 @@ import java.util.*; public class Winners { - private List listOfWinners; + private List listOfWinners; /* public void decideWinners(Cars cars) { int maxPosition = ; diff --git a/src/test/java/racingcar/CarsTest.java b/src/test/java/racingcar/CarsTest.java index b710847c58..54df34b047 100644 --- a/src/test/java/racingcar/CarsTest.java +++ b/src/test/java/racingcar/CarsTest.java @@ -16,4 +16,14 @@ void decideMaxPositionTest() { assertThat(testCars.decideMaxPosition()).isEqualTo(1000); } + @Test + void decideWinnersTest() { + List carNames = Arrays.asList("a", "b", "c"); + List carPositions = Arrays.asList(101, 101, 99); + Cars testCars = new Cars(carNames); + testCars.setPosition(carPositions); + + List checkList = Arrays.asList("a","b"); + assertThat(testCars.decideWinners(101)).isEqualTo(checkList); + } } From 12f98513c0beb6034fb81e7353ab8e19d6993023 Mon Sep 17 00:00:00 2001 From: KIMJUNGMIN Date: Thu, 9 May 2019 16:07:11 +0900 Subject: [PATCH 19/45] =?UTF-8?q?=EC=B5=9C=EC=A2=85=EC=88=98=EC=9D=91?= =?UTF-8?q?=EC=9E=90=EB=A5=BC=20=EC=B6=9C=EB=A0=A5=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/CarGameLauncher.java | 2 +- src/main/java/racingcar/Winners.java | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/racingcar/CarGameLauncher.java b/src/main/java/racingcar/CarGameLauncher.java index a41f127264..0bed6ac7a1 100644 --- a/src/main/java/racingcar/CarGameLauncher.java +++ b/src/main/java/racingcar/CarGameLauncher.java @@ -14,6 +14,6 @@ private static void doCarGame() { cars.printTheProcess(totalTurns); Winners winners = new Winners(); winners.decideWinners(cars); - + winners.announceWinners(); } } diff --git a/src/main/java/racingcar/Winners.java b/src/main/java/racingcar/Winners.java index d1ad1975b3..85994c8900 100644 --- a/src/main/java/racingcar/Winners.java +++ b/src/main/java/racingcar/Winners.java @@ -14,5 +14,9 @@ public void decideWinners(Cars cars){ this.listOfWinners = cars.decideWinners(cars.decideMaxPosition()); } + public void announceWinners() { + System.out.println(String.join(", ", this.listOfWinners) + " 가 우승했습니다!"); + } + } From e71ab756fd785a66d62fb25ac161d242e9931990 Mon Sep 17 00:00:00 2001 From: KIMJUNGMIN Date: Thu, 9 May 2019 16:20:18 +0900 Subject: [PATCH 20/45] =?UTF-8?q?=EB=B2=84=EA=B7=B8=20=EC=88=98=EC=A0=95.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/Car.java | 4 ++-- src/main/java/racingcar/CarException.java | 6 ++++-- src/main/java/racingcar/CarGameLauncher.java | 2 +- src/main/java/racingcar/Cars.java | 2 ++ src/main/java/racingcar/Winners.java | 2 +- 5 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/main/java/racingcar/Car.java b/src/main/java/racingcar/Car.java index ff9d24451d..d1e067552a 100644 --- a/src/main/java/racingcar/Car.java +++ b/src/main/java/racingcar/Car.java @@ -35,8 +35,9 @@ public static int randomNumberGenerator(){ public void printPosition(){ System.out.print(this.name + " : "); for(int i=0; i winners){ } public void setPosition(int position){ - //테스트용 this.position = position; } diff --git a/src/main/java/racingcar/CarException.java b/src/main/java/racingcar/CarException.java index 98f245b935..7b5bf39f36 100644 --- a/src/main/java/racingcar/CarException.java +++ b/src/main/java/racingcar/CarException.java @@ -18,6 +18,7 @@ public static List askCarNamesException(){ } public static List askAndReceiveCarNames(){ + System.out.println("경주할 자동차 이름을 입력하세요(이름은 쉼표(,)를 기준으로 구분)."); Scanner scanner = new Scanner(System.in); String names = scanner.nextLine(); names = names.replaceAll("\\s+",""); @@ -44,10 +45,11 @@ public static boolean isDuplicate(List names) { } public static boolean isOverLimit(List names) { + List namesCopy = new ArrayList<>(names); boolean overLimit = false; - while (!overLimit && !names.isEmpty()) { + while (!overLimit && !namesCopy.isEmpty()) { overLimit = checkLimit(names.get(0)); - names.remove(0); + namesCopy.remove(0); } return overLimit; } diff --git a/src/main/java/racingcar/CarGameLauncher.java b/src/main/java/racingcar/CarGameLauncher.java index 0bed6ac7a1..5290740702 100644 --- a/src/main/java/racingcar/CarGameLauncher.java +++ b/src/main/java/racingcar/CarGameLauncher.java @@ -14,6 +14,6 @@ private static void doCarGame() { cars.printTheProcess(totalTurns); Winners winners = new Winners(); winners.decideWinners(cars); - winners.announceWinners(); + winners.printWinners(); } } diff --git a/src/main/java/racingcar/Cars.java b/src/main/java/racingcar/Cars.java index 3190806d2f..b40c0ac262 100644 --- a/src/main/java/racingcar/Cars.java +++ b/src/main/java/racingcar/Cars.java @@ -12,8 +12,10 @@ public class Cars { } public void printTheProcess(int totalTurns) { + System.out.println("실행 결과"); for (int currentTurn = 0; currentTurn Date: Thu, 9 May 2019 16:41:25 +0900 Subject: [PATCH 21/45] =?UTF-8?q?=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/Car.java | 7 +----- src/main/java/racingcar/CarException.java | 30 ++++++++++++++--------- src/main/java/racingcar/Cars.java | 3 --- src/main/java/racingcar/Winners.java | 8 +----- src/test/java/racingcar/CarsTest.java | 24 +++++++++--------- 5 files changed, 34 insertions(+), 38 deletions(-) diff --git a/src/main/java/racingcar/Car.java b/src/main/java/racingcar/Car.java index d1e067552a..b0ceb654b2 100644 --- a/src/main/java/racingcar/Car.java +++ b/src/main/java/racingcar/Car.java @@ -1,9 +1,6 @@ package racingcar; -import java.util.ArrayList; -import java.util.Arrays; import java.util.List; -import java.util.Scanner; public class Car{ private final String name; @@ -54,9 +51,7 @@ public void findScoreSameAsMax(int maxPosition, List winners){ } public void setPosition(int position){ - //테스트용 + //테스트코드용 this.position = position; } - - } diff --git a/src/main/java/racingcar/CarException.java b/src/main/java/racingcar/CarException.java index 7b5bf39f36..59636c33c1 100644 --- a/src/main/java/racingcar/CarException.java +++ b/src/main/java/racingcar/CarException.java @@ -1,22 +1,24 @@ package racingcar; -import racingcar.Car; - import java.util.*; public class CarException { public static List askCarNamesException(){ try { List names = askAndReceiveCarNames(); - if (isWhiteSpaceOnly(names) || isDuplicate(names) || isOverLimit(names)){ - throw new IllegalArgumentException(); - } + checkConditionsForNames(names); return names; } catch(Exception e) { return Car.askCarNames(); } } + public static void checkConditionsForNames(List names) { + if (isWhiteSpaceOnly(names) || isDuplicate(names) || isOverLimit(names)){ + throw new IllegalArgumentException(); + } + } + public static List askAndReceiveCarNames(){ System.out.println("경주할 자동차 이름을 입력하세요(이름은 쉼표(,)를 기준으로 구분)."); Scanner scanner = new Scanner(System.in); @@ -27,12 +29,14 @@ public static List askAndReceiveCarNames(){ } public static boolean isWhiteSpaceOnly(List names) { - boolean result = false; - for(int i=0; i names) { @@ -65,13 +69,17 @@ public static boolean checkLimit(String name) { public static int askTotalTurnsException() { try { int userInput = askAndReceiveTurns(); - if (userInput <= 0) {throw new IllegalArgumentException();} + checkConditionsForTurns(userInput); return userInput; }catch(Exception e) { return askTotalTurnsException(); } } + public static void checkConditionsForTurns(int userInput) { + if (userInput <= 0) {throw new IllegalArgumentException();} + } + public static int askAndReceiveTurns() { System.out.println("시도할 횟수는 몇회인가요?: "); Scanner reader = new Scanner(System.in); diff --git a/src/main/java/racingcar/Cars.java b/src/main/java/racingcar/Cars.java index b40c0ac262..0c024ec9cd 100644 --- a/src/main/java/racingcar/Cars.java +++ b/src/main/java/racingcar/Cars.java @@ -47,7 +47,4 @@ public void setPosition(List inputPosition){ carList.get(i).setPosition(inputPosition.get(i)); } } - - - } diff --git a/src/main/java/racingcar/Winners.java b/src/main/java/racingcar/Winners.java index 338ab3d8aa..8d2a3886a8 100644 --- a/src/main/java/racingcar/Winners.java +++ b/src/main/java/racingcar/Winners.java @@ -4,12 +4,7 @@ public class Winners { private List listOfWinners; - /* - public void decideWinners(Cars cars) { - int maxPosition = ; - this.decideWinningCars(cars, maxPosition); - } - */ + public void decideWinners(Cars cars){ this.listOfWinners = cars.decideWinners(cars.decideMaxPosition()); } @@ -18,5 +13,4 @@ public void printWinners() { System.out.println(String.join(", ", this.listOfWinners) + " 가 우승했습니다!"); } - } diff --git a/src/test/java/racingcar/CarsTest.java b/src/test/java/racingcar/CarsTest.java index 54df34b047..a4844d0b3b 100644 --- a/src/test/java/racingcar/CarsTest.java +++ b/src/test/java/racingcar/CarsTest.java @@ -1,5 +1,6 @@ package racingcar; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.util.*; @@ -7,22 +8,23 @@ import static org.assertj.core.api.Assertions.assertThat; public class CarsTest { + List carNames; + List carPositions; + Cars testCars; + + @BeforeEach + void setUp(){ + carNames = Arrays.asList("a", "b", "c"); + carPositions = Arrays.asList(101, 101, 99); + testCars = new Cars(carNames); + testCars.setPosition(carPositions); + } @Test void decideMaxPositionTest() { - List carNames = Arrays.asList("a", "b", "c"); - List carPositions = Arrays.asList(5, 100, 1000); - Cars testCars = new Cars(carNames); - testCars.setPosition(carPositions); - - assertThat(testCars.decideMaxPosition()).isEqualTo(1000); + assertThat(testCars.decideMaxPosition()).isEqualTo(101); } @Test void decideWinnersTest() { - List carNames = Arrays.asList("a", "b", "c"); - List carPositions = Arrays.asList(101, 101, 99); - Cars testCars = new Cars(carNames); - testCars.setPosition(carPositions); - List checkList = Arrays.asList("a","b"); assertThat(testCars.decideWinners(101)).isEqualTo(checkList); } From f15e7a27b48e8b82b39dc5b1abbceccdb905d781 Mon Sep 17 00:00:00 2001 From: KIMJUNGMIN Date: Fri, 10 May 2019 15:30:40 +0900 Subject: [PATCH 22/45] =?UTF-8?q?MVC=20=ED=8C=A8=ED=84=B4=20=EC=A0=81?= =?UTF-8?q?=EC=9A=A9.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/Car.java | 71 +++++++++++---- src/main/java/racingcar/CarException.java | 89 ------------------- src/main/java/racingcar/CarGameLauncher.java | 10 ++- src/main/java/racingcar/Cars.java | 26 ++---- src/main/java/racingcar/view/InputView.java | 38 ++++++++ src/test/java/racingcar/CarExceptionTest.java | 37 -------- src/test/java/racingcar/CarTest.java | 2 + src/test/java/racingcar/WinnersTest.java | 23 ----- 8 files changed, 110 insertions(+), 186 deletions(-) delete mode 100644 src/main/java/racingcar/CarException.java create mode 100644 src/main/java/racingcar/view/InputView.java delete mode 100644 src/test/java/racingcar/CarExceptionTest.java delete mode 100644 src/test/java/racingcar/WinnersTest.java diff --git a/src/main/java/racingcar/Car.java b/src/main/java/racingcar/Car.java index b0ceb654b2..4d8e740997 100644 --- a/src/main/java/racingcar/Car.java +++ b/src/main/java/racingcar/Car.java @@ -1,37 +1,67 @@ package racingcar; -import java.util.List; +import java.util.*; -public class Car{ +public class Car { private final String name; private int position = 0; Car(String name) { + if (isWhiteSpaceOnly(name)) { + throw new IllegalArgumentException(); + } + if (isOverLimit(name)) { + throw new IllegalArgumentException(); + } this.name = name; } - public static List askCarNames(){ - return CarException.askCarNamesException(); + public static void instantiateCar(List carNames, List cars) { + try { + isDuplicate(carNames); + addCarToCars(carNames, cars); + } catch (Exception e) { + CarGameLauncher.doCarGame(); + } } - public static int askTotalTurns() { - return CarException.askTotalTurnsException(); + public static void addCarToCars(List carNames, List cars) { + for (int i = 0, n = carNames.size(); i < n; i++) { + cars.add(new Car(carNames.get(i))); + } } - public void moveCar(){ + public static boolean isWhiteSpaceOnly(String name) { + return name.isEmpty(); + } + + public static boolean isDuplicate(List names) { + Set nameSet = new HashSet<>(names); + if (names.size() != nameSet.size()) { + System.out.println("이름에 중복이 있습니다!"); + return true; + } + return false; + } + + public static boolean isOverLimit(String name) { + return name.length() > 5; + } + + public void moveCar() { int randomNumber = randomNumberGenerator(); - if(randomNumber >= 4){ + if (randomNumber >= 4) { position++; } } - public static int randomNumberGenerator(){ + public static int randomNumberGenerator() { return (int) (Math.random() * 10); } - public void printPosition(){ + public void printPosition() { System.out.print(this.name + " : "); - for(int i=0; i winners){ - if(maxPosition == this.position){ + public void findScoreSameAsMax(int maxPosition, List winners) { + if (maxPosition == this.position) { winners.add(this.name); } } - public void setPosition(int position){ - //테스트코드용 - this.position = position; + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Car car = (Car) o; + return position == car.position && + Objects.equals(name, car.name); + } + + @Override + public int hashCode() { + return Objects.hash(name, position); } } diff --git a/src/main/java/racingcar/CarException.java b/src/main/java/racingcar/CarException.java deleted file mode 100644 index 59636c33c1..0000000000 --- a/src/main/java/racingcar/CarException.java +++ /dev/null @@ -1,89 +0,0 @@ -package racingcar; - -import java.util.*; - -public class CarException { - public static List askCarNamesException(){ - try { - List names = askAndReceiveCarNames(); - checkConditionsForNames(names); - return names; - } catch(Exception e) { - return Car.askCarNames(); - } - } - - public static void checkConditionsForNames(List names) { - if (isWhiteSpaceOnly(names) || isDuplicate(names) || isOverLimit(names)){ - throw new IllegalArgumentException(); - } - } - - public static List askAndReceiveCarNames(){ - System.out.println("경주할 자동차 이름을 입력하세요(이름은 쉼표(,)를 기준으로 구분)."); - Scanner scanner = new Scanner(System.in); - String names = scanner.nextLine(); - names = names.replaceAll("\\s+",""); - List nameList = new ArrayList<>(Arrays.asList(names.split(","))); - return nameList; - } - - public static boolean isWhiteSpaceOnly(List names) { - boolean whiteSpaceOnly = false; - for(int i=0; i names) { - Set nameSet = new HashSet<>(names); - if (names.size() != nameSet.size()) { - System.out.println("이름에 중복이 있습니다!"); - return true; - } - return false; - } - - public static boolean isOverLimit(List names) { - List namesCopy = new ArrayList<>(names); - boolean overLimit = false; - while (!overLimit && !namesCopy.isEmpty()) { - overLimit = checkLimit(names.get(0)); - namesCopy.remove(0); - } - return overLimit; - } - - public static boolean checkLimit(String name) { - if (name.length() > 5 || name.length() <= 0) { - System.out.println("이름은 0글자 이상 5글자 이하만 입력해 주세요!"); - return true; - } - return false; - } - - public static int askTotalTurnsException() { - try { - int userInput = askAndReceiveTurns(); - checkConditionsForTurns(userInput); - return userInput; - }catch(Exception e) { - return askTotalTurnsException(); - } - } - - public static void checkConditionsForTurns(int userInput) { - if (userInput <= 0) {throw new IllegalArgumentException();} - } - - public static int askAndReceiveTurns() { - System.out.println("시도할 횟수는 몇회인가요?: "); - Scanner reader = new Scanner(System.in); - int userInput = reader.nextInt(); - return userInput; - } -} diff --git a/src/main/java/racingcar/CarGameLauncher.java b/src/main/java/racingcar/CarGameLauncher.java index 5290740702..61866375e6 100644 --- a/src/main/java/racingcar/CarGameLauncher.java +++ b/src/main/java/racingcar/CarGameLauncher.java @@ -1,5 +1,7 @@ package racingcar; +import racingcar.view.InputView; + import java.util.*; public class CarGameLauncher { @@ -7,10 +9,10 @@ public static void main(String[] args) { doCarGame(); } - private static void doCarGame() { - List listOfCarNames = Car.askCarNames(); - int totalTurns = Car.askTotalTurns(); - Cars cars = new Cars(listOfCarNames); + public static void doCarGame() { + List carNames = InputView.askAndReceiveCarNames(); + Cars cars = new Cars(carNames); + int totalTurns = InputView.askAndReceiveTotalTurns(); cars.printTheProcess(totalTurns); Winners winners = new Winners(); winners.decideWinners(cars); diff --git a/src/main/java/racingcar/Cars.java b/src/main/java/racingcar/Cars.java index 0c024ec9cd..152d01a291 100644 --- a/src/main/java/racingcar/Cars.java +++ b/src/main/java/racingcar/Cars.java @@ -3,12 +3,10 @@ import java.util.*; public class Cars { - private List carList = new ArrayList<>(); + private List cars = new ArrayList<>(); Cars(List carNames){ - for(int i=0; i decideWinners(int maxPosition) { List winnersName = new ArrayList<>(); - for(int i=0; i inputPosition){ - for(int i=0; i askAndReceiveCarNames(){ + System.out.println("경주할 자동차 이름을 입력하세요(이름은 쉼표(,)를 기준으로 구분)."); + Scanner scanner = new Scanner(System.in); + String names = scanner.nextLine(); + names = names.replaceAll("\\s+",""); + List carNames = new ArrayList<>(Arrays.asList(names.split(","))); + return carNames; + } + + public static int askAndReceiveTotalTurns() { + try { + int userInput = askAndReceiveTurns(); + checkConditionsForTurns(userInput); + return userInput; + }catch(Exception e) { + return askAndReceiveTotalTurns(); + } + } + + public static void checkConditionsForTurns(int userInput) { + if (userInput <= 0) {throw new IllegalArgumentException();} + } + + public static int askAndReceiveTurns() { + System.out.println("시도할 횟수는 몇회인가요?: "); + Scanner reader = new Scanner(System.in); + int userInput = reader.nextInt(); + return userInput; + } +} diff --git a/src/test/java/racingcar/CarExceptionTest.java b/src/test/java/racingcar/CarExceptionTest.java deleted file mode 100644 index 03e5725787..0000000000 --- a/src/test/java/racingcar/CarExceptionTest.java +++ /dev/null @@ -1,37 +0,0 @@ -package racingcar; - -import org.junit.jupiter.api.Test; - -import java.util.Arrays; -import java.util.List; -import java.util.*; - -import static org.assertj.core.api.Assertions.assertThat; - -public class CarExceptionTest { - @Test - void checkLimitTest(){ - String name = "asdas"; - assertThat(CarException.checkLimit(name)).isEqualTo(false); - } - - @Test - void isOverLimitTest(){ - List nameList = new ArrayList<>(Arrays.asList("asddfdfd", "asd")); - assertThat(CarException.isOverLimit(nameList)).isEqualTo(true); - } - - @Test - void isWhiteSpaceOnlyTest(){ - List nameList = Arrays.asList("asd", "", "asd"); - assertThat(CarException.isWhiteSpaceOnly(nameList)).isEqualTo(true); - } - - @Test - void isDuplicateTest() { - List nameList = Arrays.asList("asd", "ddd", "asd"); - assertThat(CarException.isDuplicate(nameList)).isEqualTo(true); - } - - -} diff --git a/src/test/java/racingcar/CarTest.java b/src/test/java/racingcar/CarTest.java index 6a39ea47a5..46a9d12551 100644 --- a/src/test/java/racingcar/CarTest.java +++ b/src/test/java/racingcar/CarTest.java @@ -23,4 +23,6 @@ void compareTest() { Car testCar = new Car("abc"); assertThat(testCar.findMax(101)).isEqualTo(101); } + + } diff --git a/src/test/java/racingcar/WinnersTest.java b/src/test/java/racingcar/WinnersTest.java deleted file mode 100644 index 7ffd88fb34..0000000000 --- a/src/test/java/racingcar/WinnersTest.java +++ /dev/null @@ -1,23 +0,0 @@ -package racingcar; - - -import org.junit.jupiter.api.Test; - -import java.util.Arrays; -import java.util.List; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.*; - -class WinnersTest { - @Test - void decideMaxPositionTest() { - Car testCar1 = new Car("aaa"); - testCar1.setPosition(5); - Car testCar2 = new Car("bbb"); - testCar2.setPosition(100); - Car testCar3 = new Car("ccc"); - testCar3.setPosition(100); - } - -} \ No newline at end of file From d50b0b1cd65a0073d810efe5025dce692f7d1e75 Mon Sep 17 00:00:00 2001 From: KIMJUNGMIN Date: Fri, 10 May 2019 16:18:05 +0900 Subject: [PATCH 23/45] =?UTF-8?q?Cars=20=EA=B0=9D=EC=B2=B4=EA=B0=80=20?= =?UTF-8?q?=EC=A0=9C=EB=8C=80=EB=A1=9C=20=EC=83=9D=EC=84=B1=EB=90=98?= =?UTF-8?q?=EC=97=88=EB=8A=94=EC=A7=80=20=ED=99=95=EC=9D=B8=ED=95=98?= =?UTF-8?q?=EB=8A=94=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/Cars.java | 13 +++++++++++++ src/test/java/racingcar/CarTest.java | 28 +++++++++++++++++++++++++++ src/test/java/racingcar/CarsTest.java | 9 ++++++++- 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/main/java/racingcar/Cars.java b/src/main/java/racingcar/Cars.java index 152d01a291..2788e2b878 100644 --- a/src/main/java/racingcar/Cars.java +++ b/src/main/java/racingcar/Cars.java @@ -39,4 +39,17 @@ public List decideWinners(int maxPosition) { } return winnersName; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Cars cars1 = (Cars) o; + return Objects.equals(cars, cars1.cars); + } + + @Override + public int hashCode() { + return Objects.hash(cars); + } } diff --git a/src/test/java/racingcar/CarTest.java b/src/test/java/racingcar/CarTest.java index 46a9d12551..51d878337d 100644 --- a/src/test/java/racingcar/CarTest.java +++ b/src/test/java/racingcar/CarTest.java @@ -2,6 +2,9 @@ import org.junit.jupiter.api.Test; +import java.util.Arrays; +import java.util.List; + import static org.assertj.core.api.Assertions.assertThat; public class CarTest { @@ -24,5 +27,30 @@ void compareTest() { assertThat(testCar.findMax(101)).isEqualTo(101); } + @Test + void isWhiteSpaceOnlyTest() { + assertThat(Car.isWhiteSpaceOnly("")).isEqualTo(true); + } + + @Test + void isDuplicateTest() { + List names = Arrays.asList("a", "b", "a"); + assertThat(Car.isDuplicate(names)).isEqualTo(true); + } + + @Test + void isOverLimitTest() { + assertThat(Car.isOverLimit("aaaaaa")).isEqualTo(true); + } + /* + + @Test + void addCarToCarsTest() { + List names = Arrays.asList("a", "b", "c"); + List cars = Arrays.asList(new Car(names.get(0)), new Car(names.get(1)), new Car(names.get(2))); + Cars carsTest = new Cars(names); + assertThat(Car.addCarToCars(names, cars)) + } + */ } diff --git a/src/test/java/racingcar/CarsTest.java b/src/test/java/racingcar/CarsTest.java index a4844d0b3b..d92dea3f69 100644 --- a/src/test/java/racingcar/CarsTest.java +++ b/src/test/java/racingcar/CarsTest.java @@ -17,7 +17,6 @@ void setUp(){ carNames = Arrays.asList("a", "b", "c"); carPositions = Arrays.asList(101, 101, 99); testCars = new Cars(carNames); - testCars.setPosition(carPositions); } @Test void decideMaxPositionTest() { @@ -28,4 +27,12 @@ void decideWinnersTest() { List checkList = Arrays.asList("a","b"); assertThat(testCars.decideWinners(101)).isEqualTo(checkList); } + @Test + void carInstantiationTest() { + List testNames = Arrays.asList("a", "b", "c"); + Cars testCars1 = new Cars(testNames); + List checkNames = Arrays.asList("a", "b", "c"); + Cars checkCars2 = new Cars(checkNames); + assertThat(testCars1).isEqualTo(checkCars2); + } } From 3529fd4c6905b05f7a1f3b69132426ed2c57e934 Mon Sep 17 00:00:00 2001 From: KIMJUNGMIN Date: Fri, 10 May 2019 16:40:50 +0900 Subject: [PATCH 24/45] =?UTF-8?q?=EC=B5=9C=EC=A2=85=20=EC=9D=B4=EB=8F=99?= =?UTF-8?q?=20=EA=B1=B0=EB=A6=AC=20=EB=B0=8F=20=EC=9A=B0=EC=8A=B9=EC=9E=90?= =?UTF-8?q?=EC=97=90=20=EB=8C=80=ED=95=9C=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/Car.java | 20 ++++++++++++++++++++ src/main/java/racingcar/Cars.java | 4 ++++ src/test/java/racingcar/CarsTest.java | 5 +++-- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/main/java/racingcar/Car.java b/src/main/java/racingcar/Car.java index 4d8e740997..ab98488fee 100644 --- a/src/main/java/racingcar/Car.java +++ b/src/main/java/racingcar/Car.java @@ -16,6 +16,11 @@ public class Car { this.name = name; } + Car(String name, int position) { + this.name = name; + this.position = position; + } + public static void instantiateCar(List carNames, List cars) { try { isDuplicate(carNames); @@ -25,12 +30,27 @@ public static void instantiateCar(List carNames, List cars) { } } + public static void instantiateCar(List carNames, List cars, List positions) { + try { + isDuplicate(carNames); + addCarToCars(carNames, cars, positions); + } catch (Exception e) { + CarGameLauncher.doCarGame(); + } + } + public static void addCarToCars(List carNames, List cars) { for (int i = 0, n = carNames.size(); i < n; i++) { cars.add(new Car(carNames.get(i))); } } + public static void addCarToCars(List carNames, List cars, List positions) { + for (int i = 0, n = carNames.size(); i < n; i++) { + cars.add(new Car(carNames.get(i), positions.get(i))); + } + } + public static boolean isWhiteSpaceOnly(String name) { return name.isEmpty(); } diff --git a/src/main/java/racingcar/Cars.java b/src/main/java/racingcar/Cars.java index 2788e2b878..8e58d35aa6 100644 --- a/src/main/java/racingcar/Cars.java +++ b/src/main/java/racingcar/Cars.java @@ -9,6 +9,10 @@ public class Cars { Car.instantiateCar(carNames, cars); } + Cars(List carNames, List positions) { + Car.instantiateCar(carNames, cars, positions); + } + public void printTheProcess(int totalTurns) { System.out.println("실행 결과"); for (int currentTurn = 0; currentTurn Date: Fri, 10 May 2019 17:29:20 +0900 Subject: [PATCH 25/45] =?UTF-8?q?MVC=20=EC=BD=94=EB=93=9C=20=EC=BB=A8?= =?UTF-8?q?=EB=B2=A4=EC=85=98=EC=97=90=20=EB=94=B0=EB=A5=B8=20=EC=88=98?= =?UTF-8?q?=EC=A0=95=20=EB=B0=8F=20OutputView=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/Car.java | 16 ++++++------ src/main/java/racingcar/CarGameLauncher.java | 5 ++-- src/main/java/racingcar/Cars.java | 19 ++++++-------- src/main/java/racingcar/Winners.java | 4 +-- src/main/java/racingcar/view/OutputView.java | 26 ++++++++++++++++++++ 5 files changed, 47 insertions(+), 23 deletions(-) create mode 100644 src/main/java/racingcar/view/OutputView.java diff --git a/src/main/java/racingcar/Car.java b/src/main/java/racingcar/Car.java index ab98488fee..a8931baaa7 100644 --- a/src/main/java/racingcar/Car.java +++ b/src/main/java/racingcar/Car.java @@ -79,14 +79,6 @@ public static int randomNumberGenerator() { return (int) (Math.random() * 10); } - public void printPosition() { - System.out.print(this.name + " : "); - for (int i = 0; i < this.position; i++) { - System.out.print("-"); - } - System.out.println(); - } - public int findMax(int max) { if (position > max) { max = position; @@ -100,6 +92,14 @@ public void findScoreSameAsMax(int maxPosition, List winners) { } } + public String getCarState() { + String carState = this.name + " : "; + for(int i=0; i carNames = InputView.askAndReceiveCarNames(); Cars cars = new Cars(carNames); int totalTurns = InputView.askAndReceiveTotalTurns(); - cars.printTheProcess(totalTurns); + OutputView.printTheProcess(cars, totalTurns); Winners winners = new Winners(); winners.decideWinners(cars); - winners.printWinners(); + OutputView.printWinners(winners); } } diff --git a/src/main/java/racingcar/Cars.java b/src/main/java/racingcar/Cars.java index 8e58d35aa6..8b9d42a4c4 100644 --- a/src/main/java/racingcar/Cars.java +++ b/src/main/java/racingcar/Cars.java @@ -13,19 +13,16 @@ public class Cars { Car.instantiateCar(carNames, cars, positions); } - public void printTheProcess(int totalTurns) { - System.out.println("실행 결과"); - for (int currentTurn = 0; currentTurn Date: Fri, 10 May 2019 17:45:11 +0900 Subject: [PATCH 26/45] =?UTF-8?q?=EC=9E=90=EB=8F=99=EC=B0=A8=20=EC=9B=80?= =?UTF-8?q?=EC=A7=81=EC=9D=B4=EB=8A=94=20=ED=95=A8=EC=88=98=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=9E=91=EC=84=B1.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/Car.java | 5 +++-- src/main/java/racingcar/Cars.java | 2 +- src/main/java/racingcar/view/OutputView.java | 3 ++- src/test/java/racingcar/CarTest.java | 7 +++++++ src/test/java/racingcar/CarsTest.java | 1 + 5 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/main/java/racingcar/Car.java b/src/main/java/racingcar/Car.java index a8931baaa7..39db94ae49 100644 --- a/src/main/java/racingcar/Car.java +++ b/src/main/java/racingcar/Car.java @@ -14,6 +14,7 @@ public class Car { throw new IllegalArgumentException(); } this.name = name; + } Car(String name, int position) { @@ -68,11 +69,11 @@ public static boolean isOverLimit(String name) { return name.length() > 5; } - public void moveCar() { - int randomNumber = randomNumberGenerator(); + public Car moveCar(int randomNumber) { if (randomNumber >= 4) { position++; } + return this; } public static int randomNumberGenerator() { diff --git a/src/main/java/racingcar/Cars.java b/src/main/java/racingcar/Cars.java index 8b9d42a4c4..75091a1fa5 100644 --- a/src/main/java/racingcar/Cars.java +++ b/src/main/java/racingcar/Cars.java @@ -22,7 +22,7 @@ public String getCarState(int index) { } public void updateCarMovement(int index) { - cars.get(index).moveCar(); + cars.get(index).moveCar(Car.randomNumberGenerator()); } public int decideMaxPosition() { diff --git a/src/main/java/racingcar/view/OutputView.java b/src/main/java/racingcar/view/OutputView.java index 96218ddd06..2034536a3a 100644 --- a/src/main/java/racingcar/view/OutputView.java +++ b/src/main/java/racingcar/view/OutputView.java @@ -4,6 +4,7 @@ import racingcar.Winners; public class OutputView { + public static void printTheProcess(Cars cars, int totalTurns) { System.out.println("실행 결과"); for (int currentTurn = 0; currentTurn < totalTurns; currentTurn++) { @@ -13,7 +14,7 @@ public static void printTheProcess(Cars cars, int totalTurns) { } public static void updateAndPrintMovement(Cars cars) { - for (int i=0, n=cars.getSize(); i < n; i++) { + for (int i = 0, n = cars.getSize(); i < n; i++) { cars.updateCarMovement(i); String carState = cars.getCarState(i); System.out.println(carState); diff --git a/src/test/java/racingcar/CarTest.java b/src/test/java/racingcar/CarTest.java index 51d878337d..58657e640f 100644 --- a/src/test/java/racingcar/CarTest.java +++ b/src/test/java/racingcar/CarTest.java @@ -42,6 +42,13 @@ void isDuplicateTest() { void isOverLimitTest() { assertThat(Car.isOverLimit("aaaaaa")).isEqualTo(true); } + + @Test + void moveCarTest() { + Car car = new Car("abc"); + Car checkCar = new Car("abc", 1); + assertThat(car.moveCar(4)).isEqualTo(checkCar); + } /* @Test diff --git a/src/test/java/racingcar/CarsTest.java b/src/test/java/racingcar/CarsTest.java index 5642339a2f..72742c10f8 100644 --- a/src/test/java/racingcar/CarsTest.java +++ b/src/test/java/racingcar/CarsTest.java @@ -18,6 +18,7 @@ void setUp(){ carPositions = Arrays.asList(101, 101, 99); testCars = new Cars(carNames, carPositions); } + @Test void decideMaxPositionTest() { assertThat(testCars.decideMaxPosition()).isEqualTo(107); From a4e18bbfeca39a5e5e955873290ed76f73f527cd Mon Sep 17 00:00:00 2001 From: KIMJUNGMIN Date: Fri, 10 May 2019 17:48:35 +0900 Subject: [PATCH 27/45] =?UTF-8?q?=EC=9E=90=EB=8F=99=EC=B0=A8=EC=83=81?= =?UTF-8?q?=ED=83=9C=ED=99=95=EC=9D=B8=ED=95=98=EB=8A=94=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=EC=BD=94=EB=93=9C=20=EC=9E=91=EC=84=B1.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/racingcar/CarTest.java | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/test/java/racingcar/CarTest.java b/src/test/java/racingcar/CarTest.java index 58657e640f..22f7069f77 100644 --- a/src/test/java/racingcar/CarTest.java +++ b/src/test/java/racingcar/CarTest.java @@ -49,15 +49,11 @@ void moveCarTest() { Car checkCar = new Car("abc", 1); assertThat(car.moveCar(4)).isEqualTo(checkCar); } - /* @Test - void addCarToCarsTest() { - List names = Arrays.asList("a", "b", "c"); - List cars = Arrays.asList(new Car(names.get(0)), new Car(names.get(1)), new Car(names.get(2))); - Cars carsTest = new Cars(names); - - assertThat(Car.addCarToCars(names, cars)) + void getCarStateTest() { + String checkString = "abc : ---"; + Car testCar = new Car("abc", 3); + assertThat(testCar.getCarState()).isEqualTo(checkString); } - */ } From a7ce293d711973e766e7ad1dcd2ff56c96d4480a Mon Sep 17 00:00:00 2001 From: KIMJUNGMIN Date: Fri, 10 May 2019 18:21:03 +0900 Subject: [PATCH 28/45] refactor --- src/main/java/racingcar/Car.java | 2 +- src/main/java/racingcar/CarGameLauncher.java | 3 +-- src/main/java/racingcar/Cars.java | 6 +++--- src/main/java/racingcar/Winners.java | 2 +- src/main/java/racingcar/view/InputView.java | 10 ++++++---- src/test/java/racingcar/CarTest.java | 10 ++++------ src/test/java/racingcar/CarsTest.java | 6 ++++-- 7 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/main/java/racingcar/Car.java b/src/main/java/racingcar/Car.java index 39db94ae49..a4410d1582 100644 --- a/src/main/java/racingcar/Car.java +++ b/src/main/java/racingcar/Car.java @@ -95,7 +95,7 @@ public void findScoreSameAsMax(int maxPosition, List winners) { public String getCarState() { String carState = this.name + " : "; - for(int i=0; i carNames = InputView.askAndReceiveCarNames(); - Cars cars = new Cars(carNames); + Cars cars = new Cars(InputView.askAndReceiveCarNames()); int totalTurns = InputView.askAndReceiveTotalTurns(); OutputView.printTheProcess(cars, totalTurns); Winners winners = new Winners(); diff --git a/src/main/java/racingcar/Cars.java b/src/main/java/racingcar/Cars.java index 75091a1fa5..70c847f0e5 100644 --- a/src/main/java/racingcar/Cars.java +++ b/src/main/java/racingcar/Cars.java @@ -5,7 +5,7 @@ public class Cars { private List cars = new ArrayList<>(); - Cars(List carNames){ + Cars(List carNames) { Car.instantiateCar(carNames, cars); } @@ -27,7 +27,7 @@ public void updateCarMovement(int index) { public int decideMaxPosition() { int max = 0; - for (int i=0, n=this.cars.size(); i decideWinners(int maxPosition) { List winnersName = new ArrayList<>(); - for(int i=0; i listOfWinners; - public void decideWinners(Cars cars){ + public void decideWinners(Cars cars) { this.listOfWinners = cars.decideWinners(cars.decideMaxPosition()); } diff --git a/src/main/java/racingcar/view/InputView.java b/src/main/java/racingcar/view/InputView.java index afa7ea896c..f57b3e9a98 100644 --- a/src/main/java/racingcar/view/InputView.java +++ b/src/main/java/racingcar/view/InputView.java @@ -6,11 +6,11 @@ import java.util.Scanner; public class InputView { - public static List askAndReceiveCarNames(){ + public static List askAndReceiveCarNames() { System.out.println("경주할 자동차 이름을 입력하세요(이름은 쉼표(,)를 기준으로 구분)."); Scanner scanner = new Scanner(System.in); String names = scanner.nextLine(); - names = names.replaceAll("\\s+",""); + names = names.replaceAll("\\s+", ""); List carNames = new ArrayList<>(Arrays.asList(names.split(","))); return carNames; } @@ -20,13 +20,15 @@ public static int askAndReceiveTotalTurns() { int userInput = askAndReceiveTurns(); checkConditionsForTurns(userInput); return userInput; - }catch(Exception e) { + } catch (Exception e) { return askAndReceiveTotalTurns(); } } public static void checkConditionsForTurns(int userInput) { - if (userInput <= 0) {throw new IllegalArgumentException();} + if (userInput <= 0) { + throw new IllegalArgumentException(); + } } public static int askAndReceiveTurns() { diff --git a/src/test/java/racingcar/CarTest.java b/src/test/java/racingcar/CarTest.java index 22f7069f77..2bd7939280 100644 --- a/src/test/java/racingcar/CarTest.java +++ b/src/test/java/racingcar/CarTest.java @@ -9,14 +9,12 @@ public class CarTest { @Test - void randomNumberGeneratorTest(){ + void randomNumberGeneratorTest() { int random = Car.randomNumberGenerator(); - System.out.println(random); - if(random >= 4){ - System.out.println("is bigger than 4"); + if (random >= 4) { assertThat(random >= 4).isEqualTo(true); - }if(random < 4){ - System.out.println("is less than 4"); + } + if (random < 4) { assertThat(random < 4).isEqualTo(true); } } diff --git a/src/test/java/racingcar/CarsTest.java b/src/test/java/racingcar/CarsTest.java index 72742c10f8..f236c5ac94 100644 --- a/src/test/java/racingcar/CarsTest.java +++ b/src/test/java/racingcar/CarsTest.java @@ -13,7 +13,7 @@ public class CarsTest { Cars testCars; @BeforeEach - void setUp(){ + void setUp() { carNames = Arrays.asList("a", "b", "c"); carPositions = Arrays.asList(101, 101, 99); testCars = new Cars(carNames, carPositions); @@ -23,11 +23,13 @@ void setUp(){ void decideMaxPositionTest() { assertThat(testCars.decideMaxPosition()).isEqualTo(107); } + @Test void decideWinnersTest() { - List checkList = Arrays.asList("a","b"); + List checkList = Arrays.asList("a", "b"); assertThat(testCars.decideWinners(101)).isEqualTo(checkList); } + @Test void carInstantiationTest() { List testNames = Arrays.asList("a", "b", "c"); From 800c083800d0d6be14084ebbdbc92ac6760aad28 Mon Sep 17 00:00:00 2001 From: KIMJUNGMIN Date: Fri, 10 May 2019 18:23:44 +0900 Subject: [PATCH 29/45] =?UTF-8?q?reademe=20=EC=88=98=EC=A0=95.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 50639cc136..9f96cf4128 100644 --- a/README.md +++ b/README.md @@ -2,19 +2,19 @@ ## 기능 구현 목록 1. [입력] 유저에게 인풋을 받는다. 2. [처리] 유저의 인풋을 숫자와 기호로 나눈다. -* [예외처리] 유저의 인풋이 숫자가 아닌 경우를 확인한다. -* [예외처리] 유저의 인풋이 기호가 아닌 경우를 확인한다. -* [예외처리] 유저의 인풋이 Zero-Division인지 확인한다. + * [예외처리] 유저의 인풋이 숫자가 아닌 경우를 확인한다. + * [예외처리] 유저의 인풋이 기호가 아닌 경우를 확인한다. + * [예외처리] 유저의 인풋이 Zero-Division인지 확인한다. 3. [결과] 해당식의 결과를 출력한다. #2번째 미션 - java-racingcar ## 기능 구현 목록 1. [입력] 유저에게 자동차의 이름을 입력받는다. -* [예외처리] 이름의 공백이 경우를 확인한다. -* [예외처리] 중복된 이름이 있는 경우 -* [예외처리] 이름이의 길이가 다섯자 이하인지 확인한다. + * [예외처리] 이름의 공백이 경우를 확인한다. + * [예외처리] 중복된 이름이 있는 경우 + * [예외처리] 이름이의 길이가 다섯자 이하인지 확인한다. 2. [입력] 유저에게 시도학 횟수를 입력받는다. -* [예외처리] 0이하의 숫자인지 확인한다. + * [예외처리] 0이하의 숫자인지 확인한다. 3. [계산/출력] 자동차가 간 거리를 계산한다. 4. [결과출력] 우승자를 출력한다. From 7fb56943906e4e0544bf2db64b20b8ae780cba17 Mon Sep 17 00:00:00 2001 From: hyojaekim Date: Sat, 11 May 2019 21:25:17 +0900 Subject: [PATCH 30/45] =?UTF-8?q?=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81=20:?= =?UTF-8?q?=20=EC=BD=94=EB=93=9C=20=EC=BB=A8=EB=B2=A4=EC=85=98=20=EB=B0=8F?= =?UTF-8?q?=20=EB=B6=88=ED=95=84=EC=9A=94=ED=95=9C=20=EA=B3=B5=EB=B0=B1=20?= =?UTF-8?q?=EC=A0=9C=EA=B1=B0.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/calculator/Calculator.java | 21 ++++++++------------ src/test/java/calculator/CalculatorTest.java | 10 +++++----- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/src/main/java/calculator/Calculator.java b/src/main/java/calculator/Calculator.java index 87b3a969f7..4a0ac460a6 100644 --- a/src/main/java/calculator/Calculator.java +++ b/src/main/java/calculator/Calculator.java @@ -21,26 +21,26 @@ public static void doCalculate() { } public static List extractNumbers(List numbers) { - List listOfNumbers= new ArrayList<>(); - for (int i=0; i listOfNumbers = new ArrayList<>(); + for (int i = 0; i < numbers.size(); i += 2) { int number = CalculatorException.extractNumbersException(numbers.get(i)); listOfNumbers.add(number); } return listOfNumbers; } - public static List extractSymbols(List symbols){ + public static List extractSymbols(List symbols) { List listOfSymbols = new ArrayList<>(); - for(int i=1; ilistOfNumbers, ListlistOfSymbols) { + public static int calculate(List listOfNumbers, List listOfSymbols) { int result = listOfNumbers.get(0); - for (int i=0; i testList = Arrays.asList("2", "+", "3"); - List checkList = Arrays.asList(2,3); + List checkList = Arrays.asList(2, 3); assertThat(cal.extractNumbers(testList)).isEqualTo(checkList); } @@ -31,14 +31,14 @@ void extractSymbolsTest() { } @Test - void calculateTest(){ - List testList = Arrays.asList(2,3); + void calculateTest() { + List testList = Arrays.asList(2, 3); List checkList = Arrays.asList("+"); assertThat(cal.calculate(testList, checkList)).isEqualTo(5); } @Test - void innerCalculateTest(){ + void innerCalculateTest() { assertThat(Calculator.applyCalculation(2, 3, "+")).isEqualTo(5); } From 25f9040cecf677be13d22e9e78e3696d252975c3 Mon Sep 17 00:00:00 2001 From: hyojaekim Date: Sun, 12 May 2019 20:02:37 +0900 Subject: [PATCH 31/45] =?UTF-8?q?enum=EC=9D=84=20=EC=82=AC=EC=9A=A9?= =?UTF-8?q?=ED=95=98=EC=97=AC=20=EA=B3=84=EC=82=B0=EA=B8=B0=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=ED=95=98=EA=B8=B0=20=EB=B0=8F=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/calculator/Calculate.java | 15 +++++++++++++++ src/main/java/calculator/Calculator.java | 14 ++------------ src/test/java/calculator/CalculatorTest.java | 2 +- 3 files changed, 18 insertions(+), 13 deletions(-) create mode 100644 src/main/java/calculator/Calculate.java diff --git a/src/main/java/calculator/Calculate.java b/src/main/java/calculator/Calculate.java new file mode 100644 index 0000000000..434c33447d --- /dev/null +++ b/src/main/java/calculator/Calculate.java @@ -0,0 +1,15 @@ +package calculator; + +import exceptionsCalculator.CalculatorException; + +enum Calculate { + RESULT(); + + public int getCalculateResult(int firstNumber, int secondNumber, String symbol) { + if (symbol.equals("+")) return firstNumber + secondNumber; + if (symbol.equals("-")) return firstNumber - secondNumber; + if (symbol.equals("*")) return firstNumber * secondNumber; + if (symbol.equals("/")) return CalculatorException.divisionException(firstNumber, secondNumber); + return CalculatorException.applyCalculationException(); + } +} diff --git a/src/main/java/calculator/Calculator.java b/src/main/java/calculator/Calculator.java index 4a0ac460a6..760ee23c94 100644 --- a/src/main/java/calculator/Calculator.java +++ b/src/main/java/calculator/Calculator.java @@ -1,6 +1,5 @@ package calculator; - import exceptionsCalculator.CalculatorException; import java.util.ArrayList; @@ -8,6 +7,7 @@ import java.util.List; public class Calculator { + public static void main(String[] args) { doCalculate(); } @@ -40,18 +40,8 @@ public static List extractSymbols(List symbols) { public static int calculate(List listOfNumbers, List listOfSymbols) { int result = listOfNumbers.get(0); for (int i = 0; i < listOfSymbols.size(); i++) { - result = applyCalculation(result, listOfNumbers.get(i + 1), listOfSymbols.get(i)); + result = Calculate.RESULT.getCalculateResult(result, listOfNumbers.get(i + 1), listOfSymbols.get(i)); } return result; } - - public static int applyCalculation(int result, int number, String symbol) { - if (symbol.equals("+")) return result + number; - if (symbol.equals("-")) return result - number; - if (symbol.equals("/")) return CalculatorException.divisionException(result, number); - if (symbol.equals("*")) return result * number; - return CalculatorException.applyCalculationException(); - } - - } diff --git a/src/test/java/calculator/CalculatorTest.java b/src/test/java/calculator/CalculatorTest.java index 1c907d8330..a002d392c5 100644 --- a/src/test/java/calculator/CalculatorTest.java +++ b/src/test/java/calculator/CalculatorTest.java @@ -39,7 +39,7 @@ void calculateTest() { @Test void innerCalculateTest() { - assertThat(Calculator.applyCalculation(2, 3, "+")).isEqualTo(5); + assertThat(Calculate.RESULT.getCalculateResult(2, 3, "+")).isEqualTo(5); } } From 361f2bca31220ff59837fe82cdacbeb81c82eebf Mon Sep 17 00:00:00 2001 From: hyojaekim Date: Sun, 12 May 2019 22:38:59 +0900 Subject: [PATCH 32/45] =?UTF-8?q?for=20each=20=EC=A0=81=EC=9A=A9=20?= =?UTF-8?q?=EB=B0=8F=20=EC=83=81=EC=88=98=20=ED=99=9C=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/Car.java | 15 ++++++++++----- src/main/java/racingcar/Cars.java | 8 ++++---- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/main/java/racingcar/Car.java b/src/main/java/racingcar/Car.java index a4410d1582..a7824ac83a 100644 --- a/src/main/java/racingcar/Car.java +++ b/src/main/java/racingcar/Car.java @@ -3,6 +3,11 @@ import java.util.*; public class Car { + private static final int MAX_NAME_SIZE = 5; + private static final int POSSIBLE_MOVE_CAR = 4; + private static final int MAX_RANDOM_NUMBER = 10; + private static final int MIN_RANDOM_NUMBER = 0; + private final String name; private int position = 0; @@ -41,8 +46,8 @@ public static void instantiateCar(List carNames, List cars, List carNames, List cars) { - for (int i = 0, n = carNames.size(); i < n; i++) { - cars.add(new Car(carNames.get(i))); + for (String name : carNames) { + cars.add(new Car(name)); } } @@ -66,18 +71,18 @@ public static boolean isDuplicate(List names) { } public static boolean isOverLimit(String name) { - return name.length() > 5; + return name.length() > MAX_NAME_SIZE; } public Car moveCar(int randomNumber) { - if (randomNumber >= 4) { + if (randomNumber >= POSSIBLE_MOVE_CAR) { position++; } return this; } public static int randomNumberGenerator() { - return (int) (Math.random() * 10); + return (int) (Math.random() * MAX_RANDOM_NUMBER) + MIN_RANDOM_NUMBER; } public int findMax(int max) { diff --git a/src/main/java/racingcar/Cars.java b/src/main/java/racingcar/Cars.java index 70c847f0e5..72c464354d 100644 --- a/src/main/java/racingcar/Cars.java +++ b/src/main/java/racingcar/Cars.java @@ -27,16 +27,16 @@ public void updateCarMovement(int index) { public int decideMaxPosition() { int max = 0; - for (int i = 0, n = this.cars.size(); i < n; i++) { - max = cars.get(i).findMax(max); + for (Car car : cars) { + max = car.findMax(max); } return max; } public List decideWinners(int maxPosition) { List winnersName = new ArrayList<>(); - for (int i = 0; i < cars.size(); i++) { - cars.get(i).findScoreSameAsMax(maxPosition, winnersName); + for (Car car : cars) { + car.findScoreSameAsMax(maxPosition, winnersName); } return winnersName; } From 49fb2f5185fa054253e8b9c1e93aa016d8da7b4c Mon Sep 17 00:00:00 2001 From: hyojaekim Date: Sun, 12 May 2019 23:20:55 +0900 Subject: [PATCH 33/45] =?UTF-8?q?Car=EC=99=80=20Cars=EC=9D=98=20=EC=97=AD?= =?UTF-8?q?=ED=95=A0=20=EB=AA=85=ED=99=95=20=EB=B0=8F=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/Car.java | 40 ----------------- src/main/java/racingcar/CarGameLauncher.java | 2 - src/main/java/racingcar/Cars.java | 46 ++++++++++++++++++-- src/main/java/racingcar/Winners.java | 2 +- src/test/java/racingcar/CarTest.java | 16 +++---- src/test/java/racingcar/CarsTest.java | 8 +++- 6 files changed, 58 insertions(+), 56 deletions(-) diff --git a/src/main/java/racingcar/Car.java b/src/main/java/racingcar/Car.java index a7824ac83a..2285b13c53 100644 --- a/src/main/java/racingcar/Car.java +++ b/src/main/java/racingcar/Car.java @@ -19,7 +19,6 @@ public class Car { throw new IllegalArgumentException(); } this.name = name; - } Car(String name, int position) { @@ -27,49 +26,10 @@ public class Car { this.position = position; } - public static void instantiateCar(List carNames, List cars) { - try { - isDuplicate(carNames); - addCarToCars(carNames, cars); - } catch (Exception e) { - CarGameLauncher.doCarGame(); - } - } - - public static void instantiateCar(List carNames, List cars, List positions) { - try { - isDuplicate(carNames); - addCarToCars(carNames, cars, positions); - } catch (Exception e) { - CarGameLauncher.doCarGame(); - } - } - - public static void addCarToCars(List carNames, List cars) { - for (String name : carNames) { - cars.add(new Car(name)); - } - } - - public static void addCarToCars(List carNames, List cars, List positions) { - for (int i = 0, n = carNames.size(); i < n; i++) { - cars.add(new Car(carNames.get(i), positions.get(i))); - } - } - public static boolean isWhiteSpaceOnly(String name) { return name.isEmpty(); } - public static boolean isDuplicate(List names) { - Set nameSet = new HashSet<>(names); - if (names.size() != nameSet.size()) { - System.out.println("이름에 중복이 있습니다!"); - return true; - } - return false; - } - public static boolean isOverLimit(String name) { return name.length() > MAX_NAME_SIZE; } diff --git a/src/main/java/racingcar/CarGameLauncher.java b/src/main/java/racingcar/CarGameLauncher.java index 9d77baf588..bcb23b98d3 100644 --- a/src/main/java/racingcar/CarGameLauncher.java +++ b/src/main/java/racingcar/CarGameLauncher.java @@ -3,8 +3,6 @@ import racingcar.view.InputView; import racingcar.view.OutputView; -import java.util.*; - public class CarGameLauncher { public static void main(String[] args) { doCarGame(); diff --git a/src/main/java/racingcar/Cars.java b/src/main/java/racingcar/Cars.java index 72c464354d..ad5eda73be 100644 --- a/src/main/java/racingcar/Cars.java +++ b/src/main/java/racingcar/Cars.java @@ -6,11 +6,11 @@ public class Cars { private List cars = new ArrayList<>(); Cars(List carNames) { - Car.instantiateCar(carNames, cars); + instantiateCar(carNames, cars); } Cars(List carNames, List positions) { - Car.instantiateCar(carNames, cars, positions); + instantiateCar(carNames, cars, positions); } public int getSize() { @@ -33,14 +33,54 @@ public int decideMaxPosition() { return max; } - public List decideWinners(int maxPosition) { + public List decideWinners() { List winnersName = new ArrayList<>(); + int maxPosition = decideMaxPosition(); for (Car car : cars) { car.findScoreSameAsMax(maxPosition, winnersName); } return winnersName; } + public static void instantiateCar(List carNames, List cars) { + try { + isDuplicate(carNames); + addCarToCars(carNames, cars); + } catch (Exception e) { + CarGameLauncher.doCarGame(); + } + } + + public static void instantiateCar(List carNames, List cars, List positions) { + try { + isDuplicate(carNames); + addCarToCars(carNames, cars, positions); + } catch (Exception e) { + CarGameLauncher.doCarGame(); + } + } + + public static void addCarToCars(List carNames, List cars) { + for (String name : carNames) { + cars.add(new Car(name)); + } + } + + public static void addCarToCars(List carNames, List cars, List positions) { + for (int i = 0, n = carNames.size(); i < n; i++) { + cars.add(new Car(carNames.get(i), positions.get(i))); + } + } + + public static boolean isDuplicate(List names) { + Set nameSet = new HashSet<>(names); + if (names.size() != nameSet.size()) { + System.out.println("이름에 중복이 있습니다!"); + return true; + } + return false; + } + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/src/main/java/racingcar/Winners.java b/src/main/java/racingcar/Winners.java index d2131411a1..df81eeb6b9 100644 --- a/src/main/java/racingcar/Winners.java +++ b/src/main/java/racingcar/Winners.java @@ -6,7 +6,7 @@ public class Winners { private List listOfWinners; public void decideWinners(Cars cars) { - this.listOfWinners = cars.decideWinners(cars.decideMaxPosition()); + this.listOfWinners = cars.decideWinners(); } public String getWinners() { diff --git a/src/test/java/racingcar/CarTest.java b/src/test/java/racingcar/CarTest.java index 2bd7939280..08dce42830 100644 --- a/src/test/java/racingcar/CarTest.java +++ b/src/test/java/racingcar/CarTest.java @@ -2,9 +2,6 @@ import org.junit.jupiter.api.Test; -import java.util.Arrays; -import java.util.List; - import static org.assertj.core.api.Assertions.assertThat; public class CarTest { @@ -30,12 +27,6 @@ void isWhiteSpaceOnlyTest() { assertThat(Car.isWhiteSpaceOnly("")).isEqualTo(true); } - @Test - void isDuplicateTest() { - List names = Arrays.asList("a", "b", "a"); - assertThat(Car.isDuplicate(names)).isEqualTo(true); - } - @Test void isOverLimitTest() { assertThat(Car.isOverLimit("aaaaaa")).isEqualTo(true); @@ -48,6 +39,13 @@ void moveCarTest() { assertThat(car.moveCar(4)).isEqualTo(checkCar); } + @Test + void notMoveCarTest() { + Car car = new Car("abc"); + Car checkCar = new Car("abc", 0); + assertThat(car.moveCar(3)).isEqualTo(checkCar); + } + @Test void getCarStateTest() { String checkString = "abc : ---"; diff --git a/src/test/java/racingcar/CarsTest.java b/src/test/java/racingcar/CarsTest.java index f236c5ac94..d83a44aa94 100644 --- a/src/test/java/racingcar/CarsTest.java +++ b/src/test/java/racingcar/CarsTest.java @@ -27,7 +27,7 @@ void decideMaxPositionTest() { @Test void decideWinnersTest() { List checkList = Arrays.asList("a", "b"); - assertThat(testCars.decideWinners(101)).isEqualTo(checkList); + assertThat(testCars.decideWinners()).isEqualTo(checkList); } @Test @@ -39,4 +39,10 @@ void carInstantiationTest() { assertThat(testCars1).isEqualTo(checkCars2); } + @Test + void isDuplicateTest() { + List names = Arrays.asList("a", "b", "a"); + assertThat(Cars.isDuplicate(names)).isEqualTo(true); + } + } From bc91ce4520784877c6ca05bfcf1a19b57243a9fd Mon Sep 17 00:00:00 2001 From: hyojaekim Date: Mon, 13 May 2019 15:00:50 +0900 Subject: [PATCH 34/45] =?UTF-8?q?List=20Position=20=EC=97=86=EC=9D=B4=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84=20=EB=B0=8F=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/CarGameLauncher.java | 1 + src/main/java/racingcar/Cars.java | 32 +++++--------------- src/test/java/racingcar/CarsTest.java | 17 ++++++----- 3 files changed, 19 insertions(+), 31 deletions(-) diff --git a/src/main/java/racingcar/CarGameLauncher.java b/src/main/java/racingcar/CarGameLauncher.java index bcb23b98d3..d249025594 100644 --- a/src/main/java/racingcar/CarGameLauncher.java +++ b/src/main/java/racingcar/CarGameLauncher.java @@ -15,5 +15,6 @@ public static void doCarGame() { Winners winners = new Winners(); winners.decideWinners(cars); OutputView.printWinners(winners); + System.exit(0); } } diff --git a/src/main/java/racingcar/Cars.java b/src/main/java/racingcar/Cars.java index ad5eda73be..b39c378276 100644 --- a/src/main/java/racingcar/Cars.java +++ b/src/main/java/racingcar/Cars.java @@ -6,11 +6,11 @@ public class Cars { private List cars = new ArrayList<>(); Cars(List carNames) { - instantiateCar(carNames, cars); + instantiateCar(carNames); } - Cars(List carNames, List positions) { - instantiateCar(carNames, cars, positions); + Cars(ArrayList cars) { + this.cars = cars; } public int getSize() { @@ -42,43 +42,27 @@ public List decideWinners() { return winnersName; } - public static void instantiateCar(List carNames, List cars) { + public void instantiateCar(List carNames) { try { isDuplicate(carNames); - addCarToCars(carNames, cars); + addCarToCars(carNames); } catch (Exception e) { CarGameLauncher.doCarGame(); } } - public static void instantiateCar(List carNames, List cars, List positions) { - try { - isDuplicate(carNames); - addCarToCars(carNames, cars, positions); - } catch (Exception e) { - CarGameLauncher.doCarGame(); - } - } - - public static void addCarToCars(List carNames, List cars) { + public void addCarToCars(List carNames) { for (String name : carNames) { cars.add(new Car(name)); } } - public static void addCarToCars(List carNames, List cars, List positions) { - for (int i = 0, n = carNames.size(); i < n; i++) { - cars.add(new Car(carNames.get(i), positions.get(i))); - } - } - - public static boolean isDuplicate(List names) { + public static void isDuplicate(List names) { Set nameSet = new HashSet<>(names); if (names.size() != nameSet.size()) { System.out.println("이름에 중복이 있습니다!"); - return true; + throw new IllegalArgumentException(); } - return false; } @Override diff --git a/src/test/java/racingcar/CarsTest.java b/src/test/java/racingcar/CarsTest.java index d83a44aa94..0965f4aa7e 100644 --- a/src/test/java/racingcar/CarsTest.java +++ b/src/test/java/racingcar/CarsTest.java @@ -6,22 +6,23 @@ import java.util.*; import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; public class CarsTest { - List carNames; - List carPositions; Cars testCars; @BeforeEach void setUp() { - carNames = Arrays.asList("a", "b", "c"); - carPositions = Arrays.asList(101, 101, 99); - testCars = new Cars(carNames, carPositions); + ArrayList cars = new ArrayList<>(); + cars.add(new Car("a", 101)); + cars.add(new Car("b", 101)); + cars.add(new Car("c", 99)); + testCars = new Cars(cars); } @Test void decideMaxPositionTest() { - assertThat(testCars.decideMaxPosition()).isEqualTo(107); + assertThat(testCars.decideMaxPosition()).isEqualTo(101); } @Test @@ -42,7 +43,9 @@ void carInstantiationTest() { @Test void isDuplicateTest() { List names = Arrays.asList("a", "b", "a"); - assertThat(Cars.isDuplicate(names)).isEqualTo(true); + assertThrows(IllegalArgumentException.class, ()-> { + Cars.isDuplicate(names); + }); } } From d8a7233ad3a41690ab708aa6203f5f8909265d19 Mon Sep 17 00:00:00 2001 From: hyojaekim Date: Mon, 13 May 2019 15:47:01 +0900 Subject: [PATCH 35/45] =?UTF-8?q?InputView=20=EC=B2=98=EB=A6=AC=20?= =?UTF-8?q?=EB=82=B4=EC=9A=A9=20=EB=B6=84=EB=A6=AC=ED=95=98=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/CarGameLauncher.java | 3 ++- src/main/java/racingcar/Cars.java | 4 ++- src/main/java/racingcar/view/InputView.java | 25 ++----------------- .../racingcar/view/InputViewException.java | 19 ++++++++++++++ 4 files changed, 26 insertions(+), 25 deletions(-) create mode 100644 src/main/java/racingcar/view/InputViewException.java diff --git a/src/main/java/racingcar/CarGameLauncher.java b/src/main/java/racingcar/CarGameLauncher.java index d249025594..995e5d3b3f 100644 --- a/src/main/java/racingcar/CarGameLauncher.java +++ b/src/main/java/racingcar/CarGameLauncher.java @@ -1,6 +1,7 @@ package racingcar; import racingcar.view.InputView; +import racingcar.view.InputViewException; import racingcar.view.OutputView; public class CarGameLauncher { @@ -10,7 +11,7 @@ public static void main(String[] args) { public static void doCarGame() { Cars cars = new Cars(InputView.askAndReceiveCarNames()); - int totalTurns = InputView.askAndReceiveTotalTurns(); + int totalTurns = InputViewException.askAndReceiveTotalTurns(); OutputView.printTheProcess(cars, totalTurns); Winners winners = new Winners(); winners.decideWinners(cars); diff --git a/src/main/java/racingcar/Cars.java b/src/main/java/racingcar/Cars.java index b39c378276..3a5574c0d0 100644 --- a/src/main/java/racingcar/Cars.java +++ b/src/main/java/racingcar/Cars.java @@ -5,7 +5,9 @@ public class Cars { private List cars = new ArrayList<>(); - Cars(List carNames) { + Cars(String names) { + names = names.replaceAll("\\s+", ""); + List carNames = new ArrayList<>(Arrays.asList(names.split(","))); instantiateCar(carNames); } diff --git a/src/main/java/racingcar/view/InputView.java b/src/main/java/racingcar/view/InputView.java index f57b3e9a98..34d3fa9ad2 100644 --- a/src/main/java/racingcar/view/InputView.java +++ b/src/main/java/racingcar/view/InputView.java @@ -1,34 +1,13 @@ package racingcar.view; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; import java.util.Scanner; public class InputView { - public static List askAndReceiveCarNames() { + public static String askAndReceiveCarNames() { System.out.println("경주할 자동차 이름을 입력하세요(이름은 쉼표(,)를 기준으로 구분)."); Scanner scanner = new Scanner(System.in); String names = scanner.nextLine(); - names = names.replaceAll("\\s+", ""); - List carNames = new ArrayList<>(Arrays.asList(names.split(","))); - return carNames; - } - - public static int askAndReceiveTotalTurns() { - try { - int userInput = askAndReceiveTurns(); - checkConditionsForTurns(userInput); - return userInput; - } catch (Exception e) { - return askAndReceiveTotalTurns(); - } - } - - public static void checkConditionsForTurns(int userInput) { - if (userInput <= 0) { - throw new IllegalArgumentException(); - } + return names; } public static int askAndReceiveTurns() { diff --git a/src/main/java/racingcar/view/InputViewException.java b/src/main/java/racingcar/view/InputViewException.java new file mode 100644 index 0000000000..7bb3a077a0 --- /dev/null +++ b/src/main/java/racingcar/view/InputViewException.java @@ -0,0 +1,19 @@ +package racingcar.view; + +public class InputViewException { + public static int askAndReceiveTotalTurns() { + try { + int userInput = InputView.askAndReceiveTurns(); + checkConditionsForTurns(userInput); + return userInput; + } catch (Exception e) { + return askAndReceiveTotalTurns(); + } + } + + public static void checkConditionsForTurns(int userInput) { + if (userInput <= 0) { + throw new IllegalArgumentException(); + } + } +} From 5a29bd04bc315408f96ea7fd84c7e1a7a3faa659 Mon Sep 17 00:00:00 2001 From: hyojaekim Date: Mon, 13 May 2019 19:32:02 +0900 Subject: [PATCH 36/45] =?UTF-8?q?OutputView=20=EC=B2=98=EB=A6=AC=20?= =?UTF-8?q?=EB=82=B4=EC=9A=A9=20=EB=B6=84=EB=A6=AC=ED=95=98=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/CarGameLauncher.java | 2 +- src/main/java/racingcar/Play.java | 31 ++++++++++++++++++++ src/main/java/racingcar/view/OutputView.java | 18 +++++------- src/test/java/racingcar/CarsTest.java | 4 +-- src/test/java/racingcar/PlayTest.java | 21 +++++++++++++ 5 files changed, 62 insertions(+), 14 deletions(-) create mode 100644 src/main/java/racingcar/Play.java create mode 100644 src/test/java/racingcar/PlayTest.java diff --git a/src/main/java/racingcar/CarGameLauncher.java b/src/main/java/racingcar/CarGameLauncher.java index 995e5d3b3f..e732315403 100644 --- a/src/main/java/racingcar/CarGameLauncher.java +++ b/src/main/java/racingcar/CarGameLauncher.java @@ -12,7 +12,7 @@ public static void main(String[] args) { public static void doCarGame() { Cars cars = new Cars(InputView.askAndReceiveCarNames()); int totalTurns = InputViewException.askAndReceiveTotalTurns(); - OutputView.printTheProcess(cars, totalTurns); + Play.printCarState(cars, totalTurns); Winners winners = new Winners(); winners.decideWinners(cars); OutputView.printWinners(winners); diff --git a/src/main/java/racingcar/Play.java b/src/main/java/racingcar/Play.java new file mode 100644 index 0000000000..2bc1bd02a9 --- /dev/null +++ b/src/main/java/racingcar/Play.java @@ -0,0 +1,31 @@ +package racingcar; + +import racingcar.view.OutputView; + +import java.util.ArrayList; +import java.util.List; + +public class Play { + + public static void printCarState(Cars cars, int turn) { + OutputView.printResult(); + for (int i = 0; i < turn; i++) { + moveCar(cars); + OutputView.printState(getCarStates(cars)); + } + } + + public static List getCarStates(Cars cars) { + List carStates = new ArrayList<>(); + for (int i = 0, n = cars.getSize(); i < n; i++) { + carStates.add(cars.getCarState(i)); + } + return carStates; + } + + public static void moveCar(Cars cars) { + for (int i = 0, n = cars.getSize(); i < n; i++) { + cars.updateCarMovement(i); + } + } +} diff --git a/src/main/java/racingcar/view/OutputView.java b/src/main/java/racingcar/view/OutputView.java index 2034536a3a..ee20d23f6c 100644 --- a/src/main/java/racingcar/view/OutputView.java +++ b/src/main/java/racingcar/view/OutputView.java @@ -1,24 +1,20 @@ package racingcar.view; -import racingcar.Cars; import racingcar.Winners; +import java.util.List; + public class OutputView { - public static void printTheProcess(Cars cars, int totalTurns) { + public static void printResult() { System.out.println("실행 결과"); - for (int currentTurn = 0; currentTurn < totalTurns; currentTurn++) { - updateAndPrintMovement(cars); - System.out.println(); - } } - public static void updateAndPrintMovement(Cars cars) { - for (int i = 0, n = cars.getSize(); i < n; i++) { - cars.updateCarMovement(i); - String carState = cars.getCarState(i); - System.out.println(carState); + public static void printState(List states) { + for (String state : states) { + System.out.println(state); } + System.out.println(); } public static void printWinners(Winners winners) { diff --git a/src/test/java/racingcar/CarsTest.java b/src/test/java/racingcar/CarsTest.java index 0965f4aa7e..51e4b3d2dd 100644 --- a/src/test/java/racingcar/CarsTest.java +++ b/src/test/java/racingcar/CarsTest.java @@ -33,9 +33,9 @@ void decideWinnersTest() { @Test void carInstantiationTest() { - List testNames = Arrays.asList("a", "b", "c"); + String testNames = "a, b, c"; Cars testCars1 = new Cars(testNames); - List checkNames = Arrays.asList("a", "b", "c"); + String checkNames = "a, b, c"; Cars checkCars2 = new Cars(checkNames); assertThat(testCars1).isEqualTo(checkCars2); } diff --git a/src/test/java/racingcar/PlayTest.java b/src/test/java/racingcar/PlayTest.java new file mode 100644 index 0000000000..3065b260ee --- /dev/null +++ b/src/test/java/racingcar/PlayTest.java @@ -0,0 +1,21 @@ +package racingcar; + +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +class PlayTest { + @Test + void carStateTest() { + ArrayList cars = new ArrayList<>(); + List states = Arrays.asList("a : -", "b : --"); + + cars.add(new Car("a", 1)); + cars.add(new Car("b", 2)); + assertThat(Play.getCarStates(new Cars(cars))).isEqualTo(states); + } +} \ No newline at end of file From ad2f38bb20b0881305344db5c2a131c9522a0f40 Mon Sep 17 00:00:00 2001 From: hyojaekim Date: Mon, 13 May 2019 20:32:47 +0900 Subject: [PATCH 37/45] =?UTF-8?q?Winner=20=EB=A1=9C=EC=A7=81=20=EB=B6=84?= =?UTF-8?q?=EB=A6=AC=20=EB=B0=8F=20=EC=BB=A8=EB=B2=A4=EC=85=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/Car.java | 7 +++-- src/main/java/racingcar/CarGameLauncher.java | 3 +- src/main/java/racingcar/Cars.java | 17 ++--------- src/main/java/racingcar/Winners.java | 29 ++++++++++++++++-- src/test/java/racingcar/CarsTest.java | 13 +------- src/test/java/racingcar/WinnersTest.java | 32 ++++++++++++++++++++ 6 files changed, 66 insertions(+), 35 deletions(-) create mode 100644 src/test/java/racingcar/WinnersTest.java diff --git a/src/main/java/racingcar/Car.java b/src/main/java/racingcar/Car.java index 2285b13c53..76b9af83a4 100644 --- a/src/main/java/racingcar/Car.java +++ b/src/main/java/racingcar/Car.java @@ -52,10 +52,11 @@ public int findMax(int max) { return max; } - public void findScoreSameAsMax(int maxPosition, List winners) { - if (maxPosition == this.position) { - winners.add(this.name); + public String sameMaxPositionCarName(int maxPosition) { + if (this.position == maxPosition) { + return this.name; } + return null; } public String getCarState() { diff --git a/src/main/java/racingcar/CarGameLauncher.java b/src/main/java/racingcar/CarGameLauncher.java index e732315403..84c7fd65c1 100644 --- a/src/main/java/racingcar/CarGameLauncher.java +++ b/src/main/java/racingcar/CarGameLauncher.java @@ -13,8 +13,7 @@ public static void doCarGame() { Cars cars = new Cars(InputView.askAndReceiveCarNames()); int totalTurns = InputViewException.askAndReceiveTotalTurns(); Play.printCarState(cars, totalTurns); - Winners winners = new Winners(); - winners.decideWinners(cars); + Winners winners = new Winners(cars); OutputView.printWinners(winners); System.exit(0); } diff --git a/src/main/java/racingcar/Cars.java b/src/main/java/racingcar/Cars.java index 3a5574c0d0..069cbdbca9 100644 --- a/src/main/java/racingcar/Cars.java +++ b/src/main/java/racingcar/Cars.java @@ -27,21 +27,8 @@ public void updateCarMovement(int index) { cars.get(index).moveCar(Car.randomNumberGenerator()); } - public int decideMaxPosition() { - int max = 0; - for (Car car : cars) { - max = car.findMax(max); - } - return max; - } - - public List decideWinners() { - List winnersName = new ArrayList<>(); - int maxPosition = decideMaxPosition(); - for (Car car : cars) { - car.findScoreSameAsMax(maxPosition, winnersName); - } - return winnersName; + public List getCars() { + return cars; } public void instantiateCar(List carNames) { diff --git a/src/main/java/racingcar/Winners.java b/src/main/java/racingcar/Winners.java index df81eeb6b9..558c04ac6e 100644 --- a/src/main/java/racingcar/Winners.java +++ b/src/main/java/racingcar/Winners.java @@ -3,10 +3,33 @@ import java.util.*; public class Winners { - private List listOfWinners; + private List listOfWinners = new ArrayList<>(); - public void decideWinners(Cars cars) { - this.listOfWinners = cars.decideWinners(); + Winners(Cars cars) { + List carsState = cars.getCars(); + int maxPosition = decideMaxPosition(carsState); + decideWinners(carsState, maxPosition); + } + + public int decideMaxPosition(List cars) { + int max = 0; + for (Car car : cars) { + max = car.findMax(max); + } + return max; + } + + public void decideWinners(List cars, int maxPosition) { + for (Car car : cars) { + addWinners(car, maxPosition); + } + } + + public void addWinners(Car car, int maxPosition) { + String winnerName = car.sameMaxPositionCarName(maxPosition); + if (winnerName != null) { + listOfWinners.add(winnerName); + } } public String getWinners() { diff --git a/src/test/java/racingcar/CarsTest.java b/src/test/java/racingcar/CarsTest.java index 51e4b3d2dd..1d0515b529 100644 --- a/src/test/java/racingcar/CarsTest.java +++ b/src/test/java/racingcar/CarsTest.java @@ -20,17 +20,6 @@ void setUp() { testCars = new Cars(cars); } - @Test - void decideMaxPositionTest() { - assertThat(testCars.decideMaxPosition()).isEqualTo(101); - } - - @Test - void decideWinnersTest() { - List checkList = Arrays.asList("a", "b"); - assertThat(testCars.decideWinners()).isEqualTo(checkList); - } - @Test void carInstantiationTest() { String testNames = "a, b, c"; @@ -43,7 +32,7 @@ void carInstantiationTest() { @Test void isDuplicateTest() { List names = Arrays.asList("a", "b", "a"); - assertThrows(IllegalArgumentException.class, ()-> { + assertThrows(IllegalArgumentException.class, () -> { Cars.isDuplicate(names); }); } diff --git a/src/test/java/racingcar/WinnersTest.java b/src/test/java/racingcar/WinnersTest.java new file mode 100644 index 0000000000..139e6a965d --- /dev/null +++ b/src/test/java/racingcar/WinnersTest.java @@ -0,0 +1,32 @@ +package racingcar; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; + +import static org.assertj.core.api.Assertions.assertThat; + +class WinnersTest { + Cars testCars; + + @BeforeEach + void setUp() { + ArrayList cars = new ArrayList<>(); + cars.add(new Car("a", 101)); + cars.add(new Car("b", 101)); + cars.add(new Car("c", 99)); + testCars = new Cars(cars); + } + + @Test + void getWinnerNameTest() { + Car car = new Car("a", 3); + assertThat(car.sameMaxPositionCarName(3)).isEqualTo("a"); + } + + @Test + void decideMaxPositionTest() { + assertThat(new Winners(testCars).decideMaxPosition(testCars.getCars())).isEqualTo(101); + } +} \ No newline at end of file From dddc55dd160b23cfde40bac85f0fc0513eae6377 Mon Sep 17 00:00:00 2001 From: hyojaekim Date: Wed, 15 May 2019 17:22:53 +0900 Subject: [PATCH 38/45] =?UTF-8?q?[=EC=88=98=EC=A0=95]=20=EB=9E=9C=EB=8D=A4?= =?UTF-8?q?=20=EC=88=AB=EC=9E=90=20=EC=83=9D=EC=84=B1=20=EB=A6=AC=ED=8C=A9?= =?UTF-8?q?=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/Car.java | 6 ------ src/main/java/racingcar/util/Utils.java | 9 +++++++++ src/test/java/racingcar/CarTest.java | 11 ----------- 3 files changed, 9 insertions(+), 17 deletions(-) create mode 100644 src/main/java/racingcar/util/Utils.java diff --git a/src/main/java/racingcar/Car.java b/src/main/java/racingcar/Car.java index 76b9af83a4..de57bbbc55 100644 --- a/src/main/java/racingcar/Car.java +++ b/src/main/java/racingcar/Car.java @@ -5,8 +5,6 @@ public class Car { private static final int MAX_NAME_SIZE = 5; private static final int POSSIBLE_MOVE_CAR = 4; - private static final int MAX_RANDOM_NUMBER = 10; - private static final int MIN_RANDOM_NUMBER = 0; private final String name; private int position = 0; @@ -41,10 +39,6 @@ public Car moveCar(int randomNumber) { return this; } - public static int randomNumberGenerator() { - return (int) (Math.random() * MAX_RANDOM_NUMBER) + MIN_RANDOM_NUMBER; - } - public int findMax(int max) { if (position > max) { max = position; diff --git a/src/main/java/racingcar/util/Utils.java b/src/main/java/racingcar/util/Utils.java new file mode 100644 index 0000000000..7a2325b78b --- /dev/null +++ b/src/main/java/racingcar/util/Utils.java @@ -0,0 +1,9 @@ +package racingcar.util; + +public class Utils { + private static final int MAX_RANDOM_NUMBER = 10; + + public static int randomNumberGenerator() { + return (int) (Math.random() * MAX_RANDOM_NUMBER); + } +} diff --git a/src/test/java/racingcar/CarTest.java b/src/test/java/racingcar/CarTest.java index 08dce42830..bc954c2076 100644 --- a/src/test/java/racingcar/CarTest.java +++ b/src/test/java/racingcar/CarTest.java @@ -5,17 +5,6 @@ import static org.assertj.core.api.Assertions.assertThat; public class CarTest { - @Test - void randomNumberGeneratorTest() { - int random = Car.randomNumberGenerator(); - if (random >= 4) { - assertThat(random >= 4).isEqualTo(true); - } - if (random < 4) { - assertThat(random < 4).isEqualTo(true); - } - } - @Test void compareTest() { Car testCar = new Car("abc"); From 7ee3a0e9ce0ec8640a92c1483385b88a3d8da148 Mon Sep 17 00:00:00 2001 From: hyojaekim Date: Wed, 15 May 2019 17:53:09 +0900 Subject: [PATCH 39/45] =?UTF-8?q?[=EC=88=98=EC=A0=95]=20test=20=EB=A9=94?= =?UTF-8?q?=EC=86=8C=EB=93=9C=EB=AA=85,=20=EC=BD=94=EB=93=9C=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/Cars.java | 4 +++- src/test/java/calculator/CalculatorTest.java | 8 ++++---- src/test/java/racingcar/CarTest.java | 12 ++++++------ src/test/java/racingcar/CarsTest.java | 14 +++++++------- src/test/java/racingcar/PlayTest.java | 2 +- src/test/java/racingcar/WinnersTest.java | 4 ++-- 6 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/main/java/racingcar/Cars.java b/src/main/java/racingcar/Cars.java index 069cbdbca9..d52b667117 100644 --- a/src/main/java/racingcar/Cars.java +++ b/src/main/java/racingcar/Cars.java @@ -1,5 +1,7 @@ package racingcar; +import racingcar.util.Utils; + import java.util.*; public class Cars { @@ -24,7 +26,7 @@ public String getCarState(int index) { } public void updateCarMovement(int index) { - cars.get(index).moveCar(Car.randomNumberGenerator()); + cars.get(index).moveCar(Utils.randomNumberGenerator()); } public List getCars() { diff --git a/src/test/java/calculator/CalculatorTest.java b/src/test/java/calculator/CalculatorTest.java index a002d392c5..f012e89340 100644 --- a/src/test/java/calculator/CalculatorTest.java +++ b/src/test/java/calculator/CalculatorTest.java @@ -17,28 +17,28 @@ void setup() { } @Test - void extractNumbersTest() { + void 타입을_제대로_변경하는지_확인() { List testList = Arrays.asList("2", "+", "3"); List checkList = Arrays.asList(2, 3); assertThat(cal.extractNumbers(testList)).isEqualTo(checkList); } @Test - void extractSymbolsTest() { + void 기호만_따로_잘분리_하는지_확인() { List testList = Arrays.asList("2", "+", "3"); List checkList = Arrays.asList("+"); assertThat(cal.extractSymbols(testList)).isEqualTo(checkList); } @Test - void calculateTest() { + void 결과값을_제대로_반환하는지_확인() { List testList = Arrays.asList(2, 3); List checkList = Arrays.asList("+"); assertThat(cal.calculate(testList, checkList)).isEqualTo(5); } @Test - void innerCalculateTest() { + void 결과값을_enum을_통해_반환하는지_확인() { assertThat(Calculate.RESULT.getCalculateResult(2, 3, "+")).isEqualTo(5); } diff --git a/src/test/java/racingcar/CarTest.java b/src/test/java/racingcar/CarTest.java index bc954c2076..595784787d 100644 --- a/src/test/java/racingcar/CarTest.java +++ b/src/test/java/racingcar/CarTest.java @@ -6,37 +6,37 @@ public class CarTest { @Test - void compareTest() { + void 맥스값을_찾는다() { Car testCar = new Car("abc"); assertThat(testCar.findMax(101)).isEqualTo(101); } @Test - void isWhiteSpaceOnlyTest() { + void 공백이_있는지_확인() { assertThat(Car.isWhiteSpaceOnly("")).isEqualTo(true); } @Test - void isOverLimitTest() { + void 이름의_크기가_5글자가_넘는지_확인() { assertThat(Car.isOverLimit("aaaaaa")).isEqualTo(true); } @Test - void moveCarTest() { + void 자동차가_잘_움직이는지_확인() { Car car = new Car("abc"); Car checkCar = new Car("abc", 1); assertThat(car.moveCar(4)).isEqualTo(checkCar); } @Test - void notMoveCarTest() { + void 자동차가_안움직이는_경우를_확인() { Car car = new Car("abc"); Car checkCar = new Car("abc", 0); assertThat(car.moveCar(3)).isEqualTo(checkCar); } @Test - void getCarStateTest() { + void 자동차의_상태를_String으로_반환() { String checkString = "abc : ---"; Car testCar = new Car("abc", 3); assertThat(testCar.getCarState()).isEqualTo(checkString); diff --git a/src/test/java/racingcar/CarsTest.java b/src/test/java/racingcar/CarsTest.java index 1d0515b529..74ad329fd9 100644 --- a/src/test/java/racingcar/CarsTest.java +++ b/src/test/java/racingcar/CarsTest.java @@ -3,7 +3,9 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -13,15 +15,13 @@ public class CarsTest { @BeforeEach void setUp() { - ArrayList cars = new ArrayList<>(); - cars.add(new Car("a", 101)); - cars.add(new Car("b", 101)); - cars.add(new Car("c", 99)); + ArrayList cars = new ArrayList<>(Arrays.asList(new Car("a", 101), new Car("b", 101), + new Car("c", 99))); testCars = new Cars(cars); } @Test - void carInstantiationTest() { + void 객체가_생성되는지_확인() { String testNames = "a, b, c"; Cars testCars1 = new Cars(testNames); String checkNames = "a, b, c"; @@ -30,7 +30,7 @@ void carInstantiationTest() { } @Test - void isDuplicateTest() { + void 중복된_값을_확인() { List names = Arrays.asList("a", "b", "a"); assertThrows(IllegalArgumentException.class, () -> { Cars.isDuplicate(names); diff --git a/src/test/java/racingcar/PlayTest.java b/src/test/java/racingcar/PlayTest.java index 3065b260ee..7bc9e2c021 100644 --- a/src/test/java/racingcar/PlayTest.java +++ b/src/test/java/racingcar/PlayTest.java @@ -10,7 +10,7 @@ class PlayTest { @Test - void carStateTest() { + void 자동차의_전체_상태를_String으로_반환받는다() { ArrayList cars = new ArrayList<>(); List states = Arrays.asList("a : -", "b : --"); diff --git a/src/test/java/racingcar/WinnersTest.java b/src/test/java/racingcar/WinnersTest.java index 139e6a965d..667ff42894 100644 --- a/src/test/java/racingcar/WinnersTest.java +++ b/src/test/java/racingcar/WinnersTest.java @@ -20,13 +20,13 @@ void setUp() { } @Test - void getWinnerNameTest() { + void 최종거리와_같은_자동차이름_반환받기() { Car car = new Car("a", 3); assertThat(car.sameMaxPositionCarName(3)).isEqualTo("a"); } @Test - void decideMaxPositionTest() { + void 우승_거리를_반환받는다() { assertThat(new Winners(testCars).decideMaxPosition(testCars.getCars())).isEqualTo(101); } } \ No newline at end of file From 7f316c3b2c72cdabf02266b14a6392f2e13069b3 Mon Sep 17 00:00:00 2001 From: hyojaekim Date: Wed, 15 May 2019 18:03:21 +0900 Subject: [PATCH 40/45] =?UTF-8?q?[=EC=88=98=EC=A0=95]=20null=20=EA=B0=92?= =?UTF-8?q?=EC=9D=84=20=EB=A6=AC=ED=84=B4=ED=95=98=EB=8A=94=20=EB=A9=94?= =?UTF-8?q?=EC=86=8C=EB=93=9C=20=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/Car.java | 10 +++++++--- src/main/java/racingcar/Winners.java | 5 ++--- src/test/java/racingcar/WinnersTest.java | 9 ++++----- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/main/java/racingcar/Car.java b/src/main/java/racingcar/Car.java index de57bbbc55..a98e7250ef 100644 --- a/src/main/java/racingcar/Car.java +++ b/src/main/java/racingcar/Car.java @@ -24,6 +24,10 @@ public class Car { this.position = position; } + public String getName() { + return this.name; + } + public static boolean isWhiteSpaceOnly(String name) { return name.isEmpty(); } @@ -46,11 +50,11 @@ public int findMax(int max) { return max; } - public String sameMaxPositionCarName(int maxPosition) { + public boolean isSameMaxPosition(int maxPosition) { if (this.position == maxPosition) { - return this.name; + return true; } - return null; + return false; } public String getCarState() { diff --git a/src/main/java/racingcar/Winners.java b/src/main/java/racingcar/Winners.java index 558c04ac6e..6464d37f7b 100644 --- a/src/main/java/racingcar/Winners.java +++ b/src/main/java/racingcar/Winners.java @@ -26,9 +26,8 @@ public void decideWinners(List cars, int maxPosition) { } public void addWinners(Car car, int maxPosition) { - String winnerName = car.sameMaxPositionCarName(maxPosition); - if (winnerName != null) { - listOfWinners.add(winnerName); + if (car.isSameMaxPosition(maxPosition)) { + listOfWinners.add(car.getName()); } } diff --git a/src/test/java/racingcar/WinnersTest.java b/src/test/java/racingcar/WinnersTest.java index 667ff42894..922c75e120 100644 --- a/src/test/java/racingcar/WinnersTest.java +++ b/src/test/java/racingcar/WinnersTest.java @@ -4,6 +4,7 @@ import org.junit.jupiter.api.Test; import java.util.ArrayList; +import java.util.Arrays; import static org.assertj.core.api.Assertions.assertThat; @@ -12,17 +13,15 @@ class WinnersTest { @BeforeEach void setUp() { - ArrayList cars = new ArrayList<>(); - cars.add(new Car("a", 101)); - cars.add(new Car("b", 101)); - cars.add(new Car("c", 99)); + ArrayList cars = new ArrayList<>(Arrays.asList(new Car("a", 101), + new Car("b", 101), new Car("c", 99))); testCars = new Cars(cars); } @Test void 최종거리와_같은_자동차이름_반환받기() { Car car = new Car("a", 3); - assertThat(car.sameMaxPositionCarName(3)).isEqualTo("a"); + assertThat(car.isSameMaxPosition(3)).isEqualTo(true); } @Test From d548d210045704d5e0f0d20a64b301c8a587b67c Mon Sep 17 00:00:00 2001 From: hyojaekim Date: Wed, 15 May 2019 18:43:08 +0900 Subject: [PATCH 41/45] =?UTF-8?q?[=EC=88=98=EC=A0=95]=20=EB=A9=94=EC=9D=B8?= =?UTF-8?q?=ED=95=A8=EC=88=98=20=EC=97=AD=ED=95=A0=20=EC=A7=80=EC=A0=95=20?= =?UTF-8?q?=EB=B0=8F=20play=20=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/CarGameLauncher.java | 11 +++++---- src/main/java/racingcar/Play.java | 24 ++++++++++++-------- src/test/java/CalculatorTest.java | 4 ++-- src/test/java/racingcar/CarsTest.java | 2 +- src/test/java/racingcar/PlayTest.java | 9 ++------ src/test/java/racingcar/WinnersTest.java | 2 +- 6 files changed, 27 insertions(+), 25 deletions(-) diff --git a/src/main/java/racingcar/CarGameLauncher.java b/src/main/java/racingcar/CarGameLauncher.java index 84c7fd65c1..98e3354429 100644 --- a/src/main/java/racingcar/CarGameLauncher.java +++ b/src/main/java/racingcar/CarGameLauncher.java @@ -10,11 +10,14 @@ public static void main(String[] args) { } public static void doCarGame() { - Cars cars = new Cars(InputView.askAndReceiveCarNames()); + String names = InputView.askAndReceiveCarNames(); int totalTurns = InputViewException.askAndReceiveTotalTurns(); - Play.printCarState(cars, totalTurns); - Winners winners = new Winners(cars); - OutputView.printWinners(winners); + Play play = new Play(names); + OutputView.printResult(); + for (int i = 0; i < totalTurns; i++) { + OutputView.printState(play.moveCarState()); + } + OutputView.printWinners(play.getWinners()); System.exit(0); } } diff --git a/src/main/java/racingcar/Play.java b/src/main/java/racingcar/Play.java index 2bc1bd02a9..17ba11eb22 100644 --- a/src/main/java/racingcar/Play.java +++ b/src/main/java/racingcar/Play.java @@ -1,21 +1,25 @@ package racingcar; -import racingcar.view.OutputView; - import java.util.ArrayList; import java.util.List; public class Play { + private final Cars cars; - public static void printCarState(Cars cars, int turn) { - OutputView.printResult(); - for (int i = 0; i < turn; i++) { - moveCar(cars); - OutputView.printState(getCarStates(cars)); - } + public Play(String names) { + cars = new Cars(names); + } + + public Winners getWinners() { + return new Winners(this.cars); + } + + public List moveCarState() { + moveCar(); + return getCarStates(); } - public static List getCarStates(Cars cars) { + public List getCarStates() { List carStates = new ArrayList<>(); for (int i = 0, n = cars.getSize(); i < n; i++) { carStates.add(cars.getCarState(i)); @@ -23,7 +27,7 @@ public static List getCarStates(Cars cars) { return carStates; } - public static void moveCar(Cars cars) { + private void moveCar() { for (int i = 0, n = cars.getSize(); i < n; i++) { cars.updateCarMovement(i); } diff --git a/src/test/java/CalculatorTest.java b/src/test/java/CalculatorTest.java index 65005a01f8..0b6201d34b 100644 --- a/src/test/java/CalculatorTest.java +++ b/src/test/java/CalculatorTest.java @@ -13,11 +13,11 @@ public void setUp() { @Test void addTest() { - assertThat(cal.addition(1,2)).isEqualTo(3); + assertThat(cal.addition(1, 2)).isEqualTo(3); } @Test void subTest() { - assertThat(cal.subtraction(2,2)).isEqualTo(0); + assertThat(cal.subtraction(2, 2)).isEqualTo(0); } } diff --git a/src/test/java/racingcar/CarsTest.java b/src/test/java/racingcar/CarsTest.java index 74ad329fd9..635dca258b 100644 --- a/src/test/java/racingcar/CarsTest.java +++ b/src/test/java/racingcar/CarsTest.java @@ -16,7 +16,7 @@ public class CarsTest { @BeforeEach void setUp() { ArrayList cars = new ArrayList<>(Arrays.asList(new Car("a", 101), new Car("b", 101), - new Car("c", 99))); + new Car("c", 99))); testCars = new Cars(cars); } diff --git a/src/test/java/racingcar/PlayTest.java b/src/test/java/racingcar/PlayTest.java index 7bc9e2c021..a555f44b53 100644 --- a/src/test/java/racingcar/PlayTest.java +++ b/src/test/java/racingcar/PlayTest.java @@ -2,7 +2,6 @@ import org.junit.jupiter.api.Test; -import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -11,11 +10,7 @@ class PlayTest { @Test void 자동차의_전체_상태를_String으로_반환받는다() { - ArrayList cars = new ArrayList<>(); - List states = Arrays.asList("a : -", "b : --"); - - cars.add(new Car("a", 1)); - cars.add(new Car("b", 2)); - assertThat(Play.getCarStates(new Cars(cars))).isEqualTo(states); + List states = Arrays.asList("a : ", "b : "); + assertThat(new Play("a,b").getCarStates()).isEqualTo(states); } } \ No newline at end of file diff --git a/src/test/java/racingcar/WinnersTest.java b/src/test/java/racingcar/WinnersTest.java index 922c75e120..96924f53a3 100644 --- a/src/test/java/racingcar/WinnersTest.java +++ b/src/test/java/racingcar/WinnersTest.java @@ -14,7 +14,7 @@ class WinnersTest { @BeforeEach void setUp() { ArrayList cars = new ArrayList<>(Arrays.asList(new Car("a", 101), - new Car("b", 101), new Car("c", 99))); + new Car("b", 101), new Car("c", 99))); testCars = new Cars(cars); } From 0d5e7c9e251110df20cff8c2ab56980ceb0dcc13 Mon Sep 17 00:00:00 2001 From: hyojaekim Date: Wed, 15 May 2019 18:56:23 +0900 Subject: [PATCH 42/45] =?UTF-8?q?[=EC=88=98=EC=A0=95]=20toString=20?= =?UTF-8?q?=EC=A0=81=EC=9A=A9=20=EB=B0=8F=20=EB=A6=AC=ED=8C=A9=ED=86=A0?= =?UTF-8?q?=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/Car.java | 12 +++++++----- src/main/java/racingcar/Cars.java | 2 +- src/test/java/racingcar/CarTest.java | 5 ++--- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/main/java/racingcar/Car.java b/src/main/java/racingcar/Car.java index a98e7250ef..987f7440e5 100644 --- a/src/main/java/racingcar/Car.java +++ b/src/main/java/racingcar/Car.java @@ -57,12 +57,14 @@ public boolean isSameMaxPosition(int maxPosition) { return false; } - public String getCarState() { - String carState = this.name + " : "; - for (int i = 0; i < this.position; i++) { - carState += "-"; + @Override + public String toString() { + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append(name + " : "); + for (int i = 0; i < position; i++) { + stringBuilder.append("-"); } - return carState; + return stringBuilder.toString(); } @Override diff --git a/src/main/java/racingcar/Cars.java b/src/main/java/racingcar/Cars.java index d52b667117..dc84abb694 100644 --- a/src/main/java/racingcar/Cars.java +++ b/src/main/java/racingcar/Cars.java @@ -22,7 +22,7 @@ public int getSize() { } public String getCarState(int index) { - return cars.get(index).getCarState(); + return cars.get(index).toString(); //TODO } public void updateCarMovement(int index) { diff --git a/src/test/java/racingcar/CarTest.java b/src/test/java/racingcar/CarTest.java index 595784787d..293c81ff22 100644 --- a/src/test/java/racingcar/CarTest.java +++ b/src/test/java/racingcar/CarTest.java @@ -36,9 +36,8 @@ public class CarTest { } @Test - void 자동차의_상태를_String으로_반환() { - String checkString = "abc : ---"; + void 자동차의_상태를_문자로_반환() { Car testCar = new Car("abc", 3); - assertThat(testCar.getCarState()).isEqualTo(checkString); + assertThat(testCar).isEqualTo(new Car("abc", 3)); } } From 324af580c4d619353fa2f2b7a3395bd29491546c Mon Sep 17 00:00:00 2001 From: hyojaekim Date: Wed, 15 May 2019 19:00:34 +0900 Subject: [PATCH 43/45] =?UTF-8?q?[=EC=88=98=EC=A0=95]=20Car=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1=EC=9E=90=20=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/Car.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/racingcar/Car.java b/src/main/java/racingcar/Car.java index 987f7440e5..10a622f620 100644 --- a/src/main/java/racingcar/Car.java +++ b/src/main/java/racingcar/Car.java @@ -10,6 +10,10 @@ public class Car { private int position = 0; Car(String name) { + this(name, 0); + } + + Car(String name, int position) { if (isWhiteSpaceOnly(name)) { throw new IllegalArgumentException(); } @@ -17,10 +21,6 @@ public class Car { throw new IllegalArgumentException(); } this.name = name; - } - - Car(String name, int position) { - this.name = name; this.position = position; } From b2e0f3da7864164230dd0577b460809f5d11b903 Mon Sep 17 00:00:00 2001 From: hyojaekim Date: Wed, 15 May 2019 19:08:55 +0900 Subject: [PATCH 44/45] =?UTF-8?q?[=EC=88=98=EC=A0=95]=20for=20each=20?= =?UTF-8?q?=EC=A0=81=EC=9A=A9=20=EB=B0=8F=20=EB=B0=A9=EC=8B=9D=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/Cars.java | 8 +++++--- src/main/java/racingcar/Play.java | 8 +------- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/main/java/racingcar/Cars.java b/src/main/java/racingcar/Cars.java index dc84abb694..6345727ee3 100644 --- a/src/main/java/racingcar/Cars.java +++ b/src/main/java/racingcar/Cars.java @@ -22,11 +22,13 @@ public int getSize() { } public String getCarState(int index) { - return cars.get(index).toString(); //TODO + return cars.get(index).toString(); } - public void updateCarMovement(int index) { - cars.get(index).moveCar(Utils.randomNumberGenerator()); + public void updateCarMovement() { + for (Car car : cars) { + car.moveCar(Utils.randomNumberGenerator()); + } } public List getCars() { diff --git a/src/main/java/racingcar/Play.java b/src/main/java/racingcar/Play.java index 17ba11eb22..dbfd8593b9 100644 --- a/src/main/java/racingcar/Play.java +++ b/src/main/java/racingcar/Play.java @@ -15,7 +15,7 @@ public Winners getWinners() { } public List moveCarState() { - moveCar(); + cars.updateCarMovement(); return getCarStates(); } @@ -26,10 +26,4 @@ public List getCarStates() { } return carStates; } - - private void moveCar() { - for (int i = 0, n = cars.getSize(); i < n; i++) { - cars.updateCarMovement(i); - } - } } From d9f6ad0c19f78da0a409df0134954ea110d36fe7 Mon Sep 17 00:00:00 2001 From: hyojaekim Date: Wed, 15 May 2019 20:01:22 +0900 Subject: [PATCH 45/45] =?UTF-8?q?[=EC=88=98=EC=A0=95]=20=EC=9A=B0=EC=8A=B9?= =?UTF-8?q?=EC=9E=90=EB=93=A4=20=EA=B5=AC=ED=95=98=EB=8A=94=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=20=EB=B3=80=EA=B2=BD=20=EB=B0=8F=20=EB=A6=AC=ED=8C=A9?= =?UTF-8?q?=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Calculator.java | 5 +++-- src/main/java/racingcar/Play.java | 4 ++-- src/main/java/racingcar/Winners.java | 15 ++++++++------- src/main/java/racingcar/view/OutputView.java | 6 ++---- src/test/java/racingcar/WinnersTest.java | 7 ++++++- 5 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/main/java/Calculator.java b/src/main/java/Calculator.java index b17a845909..92027f670f 100644 --- a/src/main/java/Calculator.java +++ b/src/main/java/Calculator.java @@ -1,8 +1,9 @@ public class Calculator { int addition(int i, int j) { - return i+j; + return i + j; } + int subtraction(int i, int j) { - return i-j; + return i - j; } } diff --git a/src/main/java/racingcar/Play.java b/src/main/java/racingcar/Play.java index dbfd8593b9..a35e131952 100644 --- a/src/main/java/racingcar/Play.java +++ b/src/main/java/racingcar/Play.java @@ -10,8 +10,8 @@ public Play(String names) { cars = new Cars(names); } - public Winners getWinners() { - return new Winners(this.cars); + public String getWinners() { + return new Winners(this.cars).getWinnerNames(); } public List moveCarState() { diff --git a/src/main/java/racingcar/Winners.java b/src/main/java/racingcar/Winners.java index 6464d37f7b..dbe247af3c 100644 --- a/src/main/java/racingcar/Winners.java +++ b/src/main/java/racingcar/Winners.java @@ -3,12 +3,12 @@ import java.util.*; public class Winners { - private List listOfWinners = new ArrayList<>(); + private List listOfWinners; Winners(Cars cars) { List carsState = cars.getCars(); int maxPosition = decideMaxPosition(carsState); - decideWinners(carsState, maxPosition); + this.listOfWinners = getResultWinners(carsState, maxPosition); } public int decideMaxPosition(List cars) { @@ -19,20 +19,21 @@ public int decideMaxPosition(List cars) { return max; } - public void decideWinners(List cars, int maxPosition) { + public List getResultWinners(List cars, int maxPosition) { //TODO get winners + List listOfWinners = new ArrayList<>(); for (Car car : cars) { - addWinners(car, maxPosition); + findWinner(listOfWinners, car, maxPosition); } + return listOfWinners; } - public void addWinners(Car car, int maxPosition) { + public void findWinner(List listOfWinners, Car car, int maxPosition) { if (car.isSameMaxPosition(maxPosition)) { listOfWinners.add(car.getName()); } } - public String getWinners() { + public String getWinnerNames() { return String.join(", ", listOfWinners); } - } diff --git a/src/main/java/racingcar/view/OutputView.java b/src/main/java/racingcar/view/OutputView.java index ee20d23f6c..82f65f8e31 100644 --- a/src/main/java/racingcar/view/OutputView.java +++ b/src/main/java/racingcar/view/OutputView.java @@ -1,7 +1,5 @@ package racingcar.view; -import racingcar.Winners; - import java.util.List; public class OutputView { @@ -17,7 +15,7 @@ public static void printState(List states) { System.out.println(); } - public static void printWinners(Winners winners) { - System.out.println(winners.getWinners() + " 가 우승했습니다!"); + public static void printWinners(String winners) { + System.out.println(winners + " 가 우승했습니다!"); } } diff --git a/src/test/java/racingcar/WinnersTest.java b/src/test/java/racingcar/WinnersTest.java index 96924f53a3..0f8f56a782 100644 --- a/src/test/java/racingcar/WinnersTest.java +++ b/src/test/java/racingcar/WinnersTest.java @@ -19,7 +19,7 @@ void setUp() { } @Test - void 최종거리와_같은_자동차이름_반환받기() { + void 최종거리와_같은지_확인한다() { Car car = new Car("a", 3); assertThat(car.isSameMaxPosition(3)).isEqualTo(true); } @@ -28,4 +28,9 @@ void setUp() { void 우승_거리를_반환받는다() { assertThat(new Winners(testCars).decideMaxPosition(testCars.getCars())).isEqualTo(101); } + + @Test + void 우승자들을_리턴받는다() { + assertThat(new Winners(testCars).getWinnerNames()).isEqualTo("a, b"); + } } \ No newline at end of file