-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
π 4λ¨κ³ - μλμ°¨ κ²½μ£Ό(μ°μΉμ) #6036
Changes from all commits
2f3b7a2
722dd29
1c8d98f
6135635
26ce345
d8e3b7d
90d0ed3
68bf8aa
601b2af
8dc4217
40c38cb
d8eca96
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,21 +3,41 @@ | |
import racingcar.strategy.MovingStrategy; | ||
|
||
public class Car { | ||
public static final int CAR_NAME_SIZE = 5; | ||
|
||
private String name; | ||
private int moveCount; | ||
|
||
public Car() { | ||
this.moveCount = 1; | ||
} | ||
|
||
public Car(String name) { | ||
validName(name); | ||
this.name = name; | ||
this.moveCount = 1; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. μ΄λ κ² λ κ²½μ° There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. λ΅! nameμλν κ²μ¦μ car μμ νλλ‘ μμ νμμ΅λλ€! ( κΈ°μ‘΄ inpuviewμ μλ κ²μ¦ λ‘μ§λ μ΄λνμμ΅λλ€. ) |
||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public int getMoveCount() { | ||
return moveCount; | ||
} | ||
|
||
|
||
public void move(MovingStrategy movingStrategy) { | ||
if(movingStrategy.isMove()){ | ||
if (movingStrategy.isMove()) { | ||
moveCount++; | ||
} | ||
} | ||
|
||
private void validName(String name) { | ||
if (name == null || name.isEmpty()) { | ||
throw new IllegalArgumentException("μλμ°¨ μ΄λ¦μ λΉκ°μΌ μ μμ΅λλ€."); | ||
} | ||
if (name.length() > CAR_NAME_SIZE) { | ||
throw new IllegalArgumentException("μλμ°¨ μ΄λ¦μ 5μλ₯Ό μ΄κ³Όν μ μμ΅λλ€."); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. π― π― π― π― π― |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,8 +4,10 @@ | |
import racingcar.ui.ResultView; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class Cars { | ||
|
||
|
@@ -15,11 +17,37 @@ public Cars(List<Car> cars) { | |
this.cars = new ArrayList<>(cars); | ||
} | ||
|
||
public static Cars fromNames(String[] names) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. μ’λ€μ π |
||
validCarNames(names); | ||
List<Car> carList = Arrays.stream(names) | ||
.map(Car::new) | ||
.collect(Collectors.toList()); | ||
return new Cars(carList); | ||
} | ||
|
||
private static void validCarNames(String[] names) { | ||
if (Arrays.stream(names).distinct().count() < names.length) { | ||
throw new IllegalArgumentException("μλμ°¨ μ΄λ¦μ μ€λ³΅λ μ μμ΅λλ€."); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. κ³ λ―Όνμ λΆλΆμΈ κ² κ°μλ° μ λ μ΄ μλ¦¬κ° λ§λ€κ³ μκ°ν©λλ€ π |
||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. μ€λ³΅μλν κ²μ¦μ μ΄λμ νλ©΄ μ’μμ§ κ³ λ―Όμ ν΄λ΄€λλ° Carsμμ κ²μ¦νλλ‘ μΆκ°λ₯Ό νμ΅λλ€! |
||
|
||
public void moveAll(MovingStrategy strategy) { | ||
cars.forEach(car -> car.move(strategy)); | ||
} | ||
|
||
public List<Car> getCurrentStatus() { | ||
return Collections.unmodifiableList(cars); | ||
} | ||
|
||
public List<String> getWinners() { | ||
int maxMoveCount = cars.stream() | ||
.mapToInt(Car::getMoveCount) | ||
.max() | ||
.orElse(0); | ||
|
||
return cars.stream() | ||
.filter(car -> car.getMoveCount() == maxMoveCount) | ||
.map(Car::getName) | ||
.collect(Collectors.toList()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. λΉμ¦λμ€ λ‘μ§ λ³Έμ°μ λ μ§μ€λμλ€μ π μμ£Ό μμν μ견μ΄κΈ΄ ν©λλ€λ§ κ°μ μ¬ν κΉμ§ μλκ³ κ΅¬μ‘°μ μΈ μ견μ΄λ μ°Έκ³ μ λλ§ ν΄μ£ΌμΈμ π |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package racingcar.domain; | ||
|
||
import racingcar.strategy.MovingStrategy; | ||
import racingcar.ui.InputView; | ||
import racingcar.ui.ResultView; | ||
|
||
public class Race { | ||
|
@@ -12,6 +13,12 @@ public Race(Cars cars, int totalAttempts) { | |
this.totalAttempts = totalAttempts; | ||
} | ||
|
||
public static Race create() { | ||
String[] carNames = InputView.inputdNameOfCar(); | ||
int attempts = InputView.inputValidatedNumberOfAttempts(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
μ λ΄μ© νλ² λ³΄κ³ κ°μλ©΄ μ’μ κ² κ°μ΅λλ€ π
|
||
return new Race(Cars.fromNames(carNames), attempts); | ||
} | ||
|
||
public void start(MovingStrategy strategy) { | ||
System.out.println("μ€ν κ²°κ³Ό"); | ||
ResultView.viewRacingCar(cars.getCurrentStatus()); | ||
|
@@ -20,4 +27,13 @@ public void start(MovingStrategy strategy) { | |
ResultView.viewRacingCar(cars.getCurrentStatus()); | ||
} | ||
} | ||
public void startWithName(MovingStrategy strategy) { | ||
System.out.println("μ€ν κ²°κ³Ό"); | ||
ResultView.viewRacingCarWithName(cars.getCurrentStatus()); | ||
for (int attempt = 1; attempt < totalAttempts; attempt++) { | ||
cars.moveAll(strategy); | ||
ResultView.viewRacingCarWithName(cars.getCurrentStatus()); | ||
} | ||
ResultView.viewRacingCarWinner(cars.getWinners()); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. μꡬμ¬ν ꡬν π |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
package racingcar.strategy; | ||
|
||
@FunctionalInterface | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. μμ²μ¬ν λ°μ π |
||
public interface MovingStrategy { | ||
boolean isMove(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,36 @@ | ||
package racingcar.ui; | ||
|
||
import racingcar.domain.Car; | ||
import racingcar.domain.Cars; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class ResultView { | ||
|
||
public static void viewRacingCar(List<Car> cars) { | ||
for(Car car : cars) { | ||
for(int i = 0; i < car.getMoveCount(); i++){ | ||
for (Car car : cars) { | ||
for (int i = 0; i < car.getMoveCount(); i++) { | ||
System.out.print("-"); | ||
} | ||
System.out.println(); | ||
} | ||
System.out.println(); | ||
|
||
} | ||
|
||
public static void viewRacingCarWithName(List<Car> cars) { | ||
for (Car car : cars) { | ||
System.out.print(car.getName() + " : "); | ||
for (int i = 0; i < car.getMoveCount(); i++) { | ||
System.out.print("-"); | ||
} | ||
System.out.println(); | ||
} | ||
System.out.println(); | ||
} | ||
|
||
public static void viewRacingCarWinner(List<String> winners) { | ||
System.out.println(String.join(",", winners) + "κ° μ΅μ’ μ°μΉνμ΅λλ€."); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ | |
import racingcar.ui.InputView; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Scanner; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
@@ -30,34 +30,34 @@ void setUp() { | |
|
||
@Test | ||
@DisplayName("μ μ§ μ‘°κ±΄μ λ§μ‘±νλ©΄ μ μ§") | ||
void move(){ | ||
car.move(()->true); | ||
void move() { | ||
car.move(() -> true); | ||
assertThat(car.getMoveCount()).isEqualTo(2); | ||
} | ||
|
||
@Test | ||
@DisplayName("μ μ§ μ‘°κ±΄μ λ§μ‘±νμ§ μμΌλ©΄ λ©μΆ€") | ||
void stop(){ | ||
car.move(()->false); | ||
void stop() { | ||
car.move(() -> false); | ||
assertThat(car.getMoveCount()).isEqualTo(1); | ||
} | ||
|
||
@Test | ||
@DisplayName("μ μ§ μ‘°κ±΄μ λ§μ‘±νλ©΄ λͺ¨λ μ μ§") | ||
void moveAll(){ | ||
cars.moveAll(()->true); | ||
void moveAll() { | ||
cars.moveAll(() -> true); | ||
|
||
assertAll( | ||
() -> assertThat(cars.getCurrentStatus().get(0).getMoveCount()).isEqualTo(2), | ||
() -> assertThat(cars.getCurrentStatus().get(1).getMoveCount()).isEqualTo(2), | ||
() -> assertThat(cars.getCurrentStatus().get(2).getMoveCount()).isEqualTo(2) | ||
); | ||
); | ||
} | ||
|
||
@Test | ||
@DisplayName("μ μ§ μ‘°κ±΄μ λ§μ‘±νμ§ μμΌλ©΄ λͺ¨λ λ©μΆ€") | ||
void stopAll(){ | ||
cars.moveAll(()->false); | ||
void stopAll() { | ||
cars.moveAll(() -> false); | ||
|
||
assertAll( | ||
() -> assertThat(cars.getCurrentStatus().get(0).getMoveCount()).isEqualTo(1), | ||
|
@@ -68,13 +68,13 @@ void stopAll(){ | |
|
||
@Test | ||
@DisplayName("random κ°μ 0κ³Ό 9μ¬μ΄ κ°") | ||
void random(){ | ||
void random() { | ||
assertThat(RandomGenerator.generate()).isBetween(0, 9); | ||
} | ||
|
||
@Test | ||
@DisplayName("μλμ°¨ λμκ° 0λ³΄λ€ μμ κ²½μ° μ€λ₯ 리ν΄") | ||
void inputCar(){ | ||
void inputCar() { | ||
String input = "0"; | ||
InputView.setScanner(new Scanner(new ByteArrayInputStream(input.getBytes()))); | ||
|
||
|
@@ -84,7 +84,7 @@ void inputCar(){ | |
|
||
@Test | ||
@DisplayName("μλν νμκ° 0λ³΄λ€ μμ κ²½μ° μ€λ₯ 리ν΄") | ||
void inputAttempts(){ | ||
void inputAttempts() { | ||
String input = "0"; | ||
InputView.setScanner(new Scanner(new ByteArrayInputStream(input.getBytes()))); | ||
|
||
|
@@ -95,9 +95,60 @@ void inputAttempts(){ | |
@Test | ||
@DisplayName("λ°νλ μλμ°¨ Listμ ν¬κΈ°λ μ λ ₯λ ν¬κΈ°μ κ°λ€.") | ||
public void getCars() { | ||
ArrayList<Car> cars = RacingCar.getCars(CAR); | ||
List<Car> cars = RacingCar.getCars(CAR); | ||
assertThat(cars.size()).isEqualTo(CAR); | ||
} | ||
|
||
@Test | ||
@DisplayName("μλμ°¨ μ΄λ¦μ΄ 곡λμΌ κ²½μ° μ€λ₯ 리ν΄") | ||
void inputCarName_empty() { | ||
assertThatThrownBy(() -> new Car("")) | ||
.isInstanceOf(IllegalArgumentException.class); | ||
} | ||
|
||
@Test | ||
@DisplayName("μλμ°¨ μ΄λ¦μ΄ 5μ μ΄κ³Όν κ²½μ° μ€λ₯ 리ν΄") | ||
void inputCarName_5μμ΄κ³Ό() { | ||
assertThatThrownBy(() -> new Car("hyundai")) | ||
.isInstanceOf(IllegalArgumentException.class); | ||
} | ||
|
||
@Test | ||
@DisplayName("μλμ°¨ μ΄λ¦μ΄ μ€λ³΅λ κ²½μ° μ€λ₯ 리ν΄") | ||
void inputCarName_μ€λ³΅λμ΄λ¦() { | ||
String[] names = {"pobi", "pobi"}; | ||
assertThatThrownBy(() -> Cars.fromNames(names)) | ||
.isInstanceOf(IllegalArgumentException.class); | ||
} | ||
|
||
@Test | ||
@DisplayName("μλμ°¨ μ΄λ¦μ μΌνλ‘ κ΅¬λΆ") | ||
void inputCarName_split() { | ||
String input = "pobi,crong,honux"; | ||
InputView.setScanner(new Scanner(new ByteArrayInputStream(input.getBytes()))); | ||
assertThat(InputView.inputdNameOfCar()).containsExactly(input.split(",")); | ||
|
||
} | ||
|
||
@Test | ||
@DisplayName("λ¨μΌ μ°μΉμ κ²°μ ") | ||
void getWinner() { | ||
Car pobi = new Car("pobi"); | ||
Car crong = new Car("crong"); | ||
Cars cars = new Cars(List.of(pobi, crong)); | ||
pobi.move(() -> true); | ||
|
||
assertThat(cars.getWinners()).containsExactly("pobi"); | ||
} | ||
|
||
@Test | ||
@DisplayName("곡λ μ°μΉμ κ²°μ ") | ||
void getWinners() { | ||
Car pobi = new Car("pobi"); | ||
Car crong = new Car("crong"); | ||
Cars cars = new Cars(List.of(pobi, crong)); | ||
|
||
assertThat(cars.getWinners()).containsExactly("pobi", "crong"); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ν μ€νΈ μμ± μ’λ€μ π |
||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μμν μ견μ λλ€λ§
Race
λCars
κ° μ μ ν©ν 리 λ©μλλ₯Ό μ 곡νλ€λ©΄μ¬μ©νλ κ°μ²΄ μ μ₯μμ ν¨μ¬ μμν κ² κ°μ΅λλ€ π
μ΄λ―Έ κ°μ²΄λ€μ μ μλ μ ν΄μ£Όμ ¨μΌλ μ¬μ©μ± κ°μ μ μν μΈν°νμ΄μ€μͺ½λ
νλ² μ 리νκ³ κ°μλ©΄ μ’μ κ² κ°μ΅λλ€.
μ λ΄μ©νλ² λ³΄μλ©΄ λμλμ€ κ² κ°μ΅λλ€ π
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ μ ν©ν 리 λ©μλλ μκ°μ μν΄λ΄€λλ° λλΆμ λ°°μ°κ³ κ°λλ€!
μμ§ μ΄λ»κ² ꡬνν΄μΌ μ’μμ§ κ°μ΄ μμμ μΌλ¨ Raceλ₯Ό μμ±νλ createμ Carsμ μμ±μλ₯Ό fromNamesλ‘ κ΅¬νν΄λ΄€μ΅λλ€!