From 91bf349abdc6f13daa921596f77044e2965c7864 Mon Sep 17 00:00:00 2001 From: izzy80 <115052929+izzy80@users.noreply.github.com> Date: Wed, 7 May 2025 16:57:25 +0900 Subject: [PATCH 1/6] =?UTF-8?q?docs:=202=EB=8B=A8=EA=B3=84=20=EC=9A=94?= =?UTF-8?q?=EA=B5=AC=EC=82=AC=ED=95=AD=20=EA=B8=B0=EC=9E=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index df2c412c..1bdcbeb9 100644 --- a/README.md +++ b/README.md @@ -9,4 +9,10 @@ 4. Java Style Guide를 기반으로 자바 코드 컨벤션을 지킴 5. indent depth는 2를 넘지 않는다 -> 함수로 분리 6. 3항 연산자, else 예약어 금지 -7. 함수의 길이는 15라인을 넘어가지 않는다. -> 한 가지 일만 하도록 구현 \ No newline at end of file +7. 함수의 길이는 15라인을 넘어가지 않는다. -> 한 가지 일만 하도록 구현 + +## 2단계 : 우승 자동차 구하기 +요구사항 +1. n대의 자동차가 참여할 수 있다 +2. 주어진 횟수 동안 n대의 자동차는 전진 또는 멈출 수 있다 +3. 자동차 경주 게임을 완료한 후 누가 우승했는지 구할 수 있다. 우승자는 1명 이상일 수 있다. \ No newline at end of file From b907bed0245ae3f6269e441558b0dce3fa544809 Mon Sep 17 00:00:00 2001 From: izzy80 <115052929+izzy80@users.noreply.github.com> Date: Wed, 7 May 2025 18:09:27 +0900 Subject: [PATCH 2/6] =?UTF-8?q?feat:=20CarWinner=20=EA=B0=9D=EC=B2=B4=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/CarWinner.java | 76 ++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 src/main/java/CarWinner.java diff --git a/src/main/java/CarWinner.java b/src/main/java/CarWinner.java new file mode 100644 index 00000000..3e4afb12 --- /dev/null +++ b/src/main/java/CarWinner.java @@ -0,0 +1,76 @@ +import java.util.ArrayList; + +public class CarWinner { + private Car[] cars; //참가한 자동차들 + private int winnerPosition; //우승 자동차의 위치(최대값) + private int winnerCnt; //우승 자동차의 수 + private ArrayList winnerNames; //우승 자동차의 이름 + + public CarWinner(Car[] cars) { + this.cars = cars; + winnerCnt = 0; + winnerPosition = Integer.MIN_VALUE; + winnerNames = new ArrayList<>(); + } + + public int getWinnerCnt() { + return winnerCnt; + } + + public int getWinnerPosition() { + return winnerPosition; + } + + public ArrayList getWinnerName() { + return winnerNames; + } + + public void whichWinner(int testRounds){ + //1. 모든 자동차를 주어진 횟수만큼 이동 + moveAllCars(testRounds); + //2. 우승자의 수와 자동차의 위치(최대값) 계산 + countWinners(); + //3. 우승자 이름 저장 + saveWinnerName(); + } + + //1. 모든 자동차를 주어진 횟수만큼 이동 + private void moveAllCars(int testRounds){ + for (Car car : cars) { + for (int i = 0; i < testRounds; i++) { + car.move(RandomUtil.randomGenerator()); + } + } + } + + //2. 우승자의 수와 자동차의 위치(최대값) 계산 + private void countWinners(){ + for (Car car : cars) { + updateWinnerCount(car); + } + } + + //2-1. 자동차 한 대씩 우승자의 수와 최고 위치 갱신 + private void updateWinnerCount(Car car) { + int position = car.getPosition(); + if (position > winnerPosition) { + winnerCnt = 1; + winnerPosition = position; + return; + } + if (position == winnerPosition) { + winnerCnt++; + } + } + + //3. 우승자 이름 저장 + private void saveWinnerName(){ + for (Car car : cars) { + if (car.getPosition() == winnerPosition) { + winnerNames.add(car.getName()); + } + } + } +} + + From 68cc5b3ffca935341180693f746d2bf7da648d0e Mon Sep 17 00:00:00 2001 From: izzy80 <115052929+izzy80@users.noreply.github.com> Date: Wed, 7 May 2025 18:10:12 +0900 Subject: [PATCH 3/6] =?UTF-8?q?Test:=20=EC=9E=90=EB=8F=99=EC=B0=A8=20?= =?UTF-8?q?=EA=B2=BD=EC=A3=BC=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/CarTest.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/test/java/CarTest.java b/src/test/java/CarTest.java index df1a3ef5..d85508ab 100644 --- a/src/test/java/CarTest.java +++ b/src/test/java/CarTest.java @@ -56,6 +56,28 @@ void carMultipleMove() { assertThat(car.getPosition()).isEqualTo(2); } + @Test + @DisplayName("주어진 횟수 동안 n대의 자동차 중에 우승자가 1명 이상 나온다") + void whichCarIsWin() { + Car carA = new Car("A",moveCondition); + Car carB = new Car("B",moveCondition); + Car carC = new Car("C",moveCondition); + + Car[] cars = {carA, carB, carC}; + + int testRounds = 1; + CarWinner winner = new CarWinner(cars); + winner.whichWinner(testRounds); + + //우승자는 1명 이상 나온다 + assertThat(winner.getWinnerCnt()).isGreaterThanOrEqualTo(1); + //우승자의 수와 이름의 수가 같아야 한다 + assertThat(winner.getWinnerCnt()).isEqualTo(winner.getWinnerName().size()); + + } + + + } From b40447007e7f569177904bc03ae467dbdb212686 Mon Sep 17 00:00:00 2001 From: izzy80 <115052929+izzy80@users.noreply.github.com> Date: Thu, 8 May 2025 22:48:09 +0900 Subject: [PATCH 4/6] =?UTF-8?q?refactor:=20Car=20=EB=B3=80=EC=88=98=20?= =?UTF-8?q?=EC=9D=B4=EB=A6=84=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Car.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/Car.java b/src/main/java/Car.java index 7ccc4ed5..1aca23e5 100644 --- a/src/main/java/Car.java +++ b/src/main/java/Car.java @@ -2,29 +2,29 @@ public class Car { private static final int MOVE_THRESHOLD = 4; - private String carName; - private int carPosition = 0; + private String name; + private int position = 0; private final MoveCondition moveCondition; public Car(String carName, MoveCondition moveCondition) { if (carName == null || carName.trim().isEmpty()) { throw new IllegalArgumentException("차의 이름이 비어있습니다"); } - this.carName = carName; + this.name = carName; this.moveCondition = moveCondition; } public int getPosition() { - return carPosition; + return position; } public String getName() { - return carName; + return name; } public void move(int value){ if (moveCondition.isMovable(value)) { - carPosition++; + position++; } } } From 13980342c8652d3e16cb5c4969c4f7635f1e55c3 Mon Sep 17 00:00:00 2001 From: izzy80 <115052929+izzy80@users.noreply.github.com> Date: Thu, 8 May 2025 22:50:04 +0900 Subject: [PATCH 5/6] =?UTF-8?q?feat:=20Cars=20=EC=9D=BC=EA=B8=89=EC=BB=AC?= =?UTF-8?q?=EB=A0=89=EC=85=98=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Cars.java | 49 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/main/java/Cars.java diff --git a/src/main/java/Cars.java b/src/main/java/Cars.java new file mode 100644 index 00000000..5fb8a791 --- /dev/null +++ b/src/main/java/Cars.java @@ -0,0 +1,49 @@ +import java.util.*; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +public class Cars { + //자동차 경주에 참가하는 자동차들의 집합 + private List cars; + + public Cars(List cars) { + this.cars = cars; + } + + //자동차 이동 + public void moveAll(int testRounds) { + for (Car car : cars) { + IntStream.range(0, testRounds) + .forEach(i -> car.move(RandomUtil.randomGenerator())); + } + } + + //이동된 자동차들의 최대위치 구하기 + private int getMaxPosition() { + return cars.stream() + .mapToInt(Car::getPosition) + .max() + .orElse(0); + } + + //우승 자동차 목록 + public List getWinners() { + int max = getMaxPosition(); + return cars.stream() + .filter(car -> car.getPosition() == max) + .collect(Collectors.toList()); + } + + //우승 자동차 이름 목록 + public List getWinnerNames() { + return getWinners().stream() + .map(Car::getName) + .collect(Collectors.toList()); + } + + //우승 자동차의 수 + public int getWinnerCount() { + return getWinners().size(); + } + +} From 75c862a4d1b6ee9a4c6a9b3e01d589660dd0478c Mon Sep 17 00:00:00 2001 From: izzy80 <115052929+izzy80@users.noreply.github.com> Date: Thu, 8 May 2025 22:50:15 +0900 Subject: [PATCH 6/6] =?UTF-8?q?test:=20Cars=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= =?UTF-8?q?=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/test/java/CarTest.java | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/test/java/CarTest.java b/src/test/java/CarTest.java index d85508ab..e9428a0d 100644 --- a/src/test/java/CarTest.java +++ b/src/test/java/CarTest.java @@ -1,5 +1,9 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import java.util.List; import static org.junit.jupiter.api.Assertions.*; @@ -56,24 +60,23 @@ void carMultipleMove() { assertThat(car.getPosition()).isEqualTo(2); } - @Test - @DisplayName("주어진 횟수 동안 n대의 자동차 중에 우승자가 1명 이상 나온다") - void whichCarIsWin() { + @ParameterizedTest + @ValueSource(ints = {1, 2, 3}) + @DisplayName("주어진 횟수 동안 n대의 자동차 중에 우승자가 m명 이상 나온다") + void whichCarsIsWin(int value) { Car carA = new Car("A",moveCondition); Car carB = new Car("B",moveCondition); Car carC = new Car("C",moveCondition); - Car[] cars = {carA, carB, carC}; + Cars cars = new Cars(List.of(carA, carB, carC)); int testRounds = 1; - CarWinner winner = new CarWinner(cars); - winner.whichWinner(testRounds); + cars.moveAll(testRounds); - //우승자는 1명 이상 나온다 - assertThat(winner.getWinnerCnt()).isGreaterThanOrEqualTo(1); + //우승자는 n명 이상 나온다 + assertThat(cars.getWinnerCount()).isGreaterThanOrEqualTo(value); //우승자의 수와 이름의 수가 같아야 한다 - assertThat(winner.getWinnerCnt()).isEqualTo(winner.getWinnerName().size()); - + assertThat(cars.getWinnerCount()).isEqualTo(cars.getWinnerNames().size()); }