-
Notifications
You must be signed in to change notification settings - Fork 1.2k
π 4λ¨κ³ - μλμ°¨ κ²½μ£Ό(μ°μΉμ) #6100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: jieung2
Are you sure you want to change the base?
Changes from all commits
847d270
8340cbe
302a0c5
ee5f170
fd23e9d
885e2f8
35c023d
1331fd7
c4de5e8
77978a0
c35e4b9
4828376
6b7846d
5e99bc7
75e1f90
f096b98
fbf729e
cef0314
c166437
6a7c40e
829580b
76070dd
b5a30cd
1af10b4
45cb60a
b9a1265
e6ee71c
e1ac418
7d08731
21ca3be
212ee73
d5c0862
014af59
03b6f2a
62b7996
595670d
b50fd86
5c2b938
1404be9
20d4b33
d8009f1
05b0670
8ffe40c
eee0897
bf0b47b
b301e51
f29f066
c051058
2167869
1a91306
0ba008a
04d6ef2
2ad32e7
2dd7dca
9dcc6ec
4c0e4d6
19e58df
77e6957
41df7c0
fb3389f
6d43c59
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
## κΈ°λ₯ μꡬμ¬ν | ||
- κ° μλμ°¨μ μ΄λ¦μ λΆμ¬ν μ μλ€. μλμ°¨ μ΄λ¦μ 5μλ₯Ό μ΄κ³Όν μ μλ€. | ||
- μ μ§νλ μλμ°¨λ₯Ό μΆλ ₯ν λ μλμ°¨ μ΄λ¦μ κ°μ΄ μΆλ ₯νλ€. | ||
- μλμ°¨ μ΄λ¦μ μΌν(,)λ₯Ό κΈ°μ€μΌλ‘ ꡬλΆνλ€. | ||
- μλμ°¨ κ²½μ£Ό κ²μμ μλ£ν ν λκ° μ°μΉνλμ§λ₯Ό μλ €μ€λ€. μ°μΉμλ ν λͺ μ΄μμΌ μ μλ€. | ||
|
||
## κΈ°λ₯ λͺ©λ‘ | ||
- [x] μλμ°¨μ μ΄λ¦μ λΆμ¬νλ€. | ||
- [x] μλμ°¨ μ΄λ¦μ΄ 5μλ₯Ό μ΄κ³Όνλμ§ κ²μ¬νλ€. | ||
- [x] μ μ§νλ μλμ°¨λ₯Ό μΆλ ₯ν λ μλμ°¨ μ΄λ¦μ κ°μ΄ μΆλ ₯νλ€. | ||
- [x] μλμ°¨ μ΄λ¦μ μΌνλ‘ κ΅¬λΆνλ€. | ||
- [x] μλμ°¨ κ²½μ£Ό κ²μ μ°μΉμλ₯Ό νλ³νλ€. | ||
- [x] μλμ°¨ κ²½μ£Ό κ²μ μ°μΉμλ₯Ό μΆλ ₯νλ€. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package domain; | ||
|
||
import utils.NumberGenerator; | ||
|
||
public class Car { | ||
private final static int BASE_NUMBER = 4; | ||
private final static int MAX_NAME_LENGTH = 5; | ||
private final String name; | ||
private final Position position; | ||
|
||
public Car(String name) { | ||
this(name, 0); | ||
} | ||
|
||
public Car(String name, int position) { | ||
validateName(name); | ||
this.name = name; | ||
this.position = new Position(position); | ||
} | ||
|
||
public void move(NumberGenerator numberGenerator) { | ||
int movableNumber = numberGenerator.generate(); | ||
if (isAtLeastBaseNumber(movableNumber)) { | ||
goForward(); | ||
} | ||
} | ||
|
||
private void goForward() { | ||
this.position.increase(); | ||
} | ||
|
||
public int getPosition() { | ||
return this.position.getValue(); | ||
} | ||
|
||
public String getName() { | ||
return this.name; | ||
} | ||
|
||
private boolean isAtLeastBaseNumber(int number) { | ||
return BASE_NUMBER <= number; | ||
} | ||
|
||
private void validateName(String name) { | ||
if (exceedsMaxNameLength(name)) { | ||
throw new IllegalArgumentException("μλμ°¨ μ΄λ¦μ 5μ μ΄νμ¬μΌ ν©λλ€."); | ||
} | ||
} | ||
|
||
private boolean exceedsMaxNameLength(String name) { | ||
return name.length() > MAX_NAME_LENGTH; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package domain; | ||
|
||
import utils.NumberGenerator; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class Cars { | ||
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 λ΄μμ μ°μΉμκ° μ‘΄μ¬νμ§ μμκΉμ? Carsκ° μ°μΉμλ₯Ό returnνλκ² μ΄μΈλ¦¬μ§ μμκΉμ? |
||
List<Car> cars = new ArrayList<>(); | ||
|
||
public Cars(List<Car> cars) { | ||
this.cars = cars; | ||
} | ||
|
||
public static List<Car> generateCars(String[] carNames) { | ||
return Arrays.stream(carNames) | ||
.map(Car::new) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
public void moveAll(NumberGenerator numberGenerator) { | ||
cars.forEach(car -> car.move(numberGenerator)); | ||
} | ||
|
||
public List<Integer> getPositions() { | ||
return cars.stream() | ||
.map(Car::getPosition) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
public List<Car> getCars() { | ||
return List.copyOf(cars); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package domain; | ||
|
||
public class Position { | ||
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. positionμ 0 λ―Έλ§μ κ°μ΄ λ μ μμκΉμ? |
||
private int value; | ||
|
||
public Position(int value) { | ||
this.value = value; | ||
} | ||
|
||
public int getValue() { | ||
return this.value; | ||
} | ||
|
||
public void increase() { | ||
this.value++; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package domain; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class WinnerDeterminer { | ||
|
||
public static List<Car> findWinners(Cars cars) { | ||
int maxPosition = findMaxPosition(cars); | ||
return cars.getCars().stream() | ||
.filter(car -> car.getPosition() == maxPosition) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private static int findMaxPosition(Cars cars) { | ||
return cars.getCars().stream() | ||
.mapToInt(Car::getPosition) | ||
.max() | ||
.orElse(0); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package domain; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import utils.FixedNumberGenerator; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
public class CarTest { | ||
@Test | ||
@DisplayName("μ«μκ° 4 μ΄μμΌ λ μλμ°¨κ° μ μ§νλ€.") | ||
void μλμ°¨_μ μ§() { | ||
Car car = new Car("pobi"); | ||
car.move(new FixedNumberGenerator(4)); | ||
assertThat(car.getPosition()).isEqualTo(1); | ||
} | ||
|
||
@Test | ||
@DisplayName("μ«μκ° 4 λ―Έλ§μΌ λ μλμ°¨κ° μ μ§νλ€.") | ||
void μλμ°¨_μ μ§() { | ||
Car car = new Car("pobi"); | ||
car.move(new FixedNumberGenerator(1)); | ||
assertThat(car.getPosition()).isEqualTo(0); | ||
} | ||
|
||
@Test | ||
@DisplayName("μλμ°¨ μ΄λ¦μ΄ 5μκ° λμΌλ©΄ μμΈκ° λ°μνλ€.") | ||
void μλμ°¨_μ΄λ¦_μ΄κ³Ό() { | ||
assertThrows(IllegalArgumentException.class, () -> { | ||
new Car("pobijjang"); | ||
}); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package domain; | ||
|
||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class PositionTest { | ||
@Test | ||
@DisplayName("value κ°κ³Ό ν¨κ» positionμ μμ±νλ©΄ positionμ κ°μ΄ 4λ‘ μ€μ λλ€.") | ||
void create() { | ||
Position position = new Position(4); | ||
Assertions.assertThat(position.getValue()).isEqualTo(4); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package domain; | ||
|
||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import utils.FixedNumberGenerator; | ||
import utils.NumberGenerator; | ||
|
||
import java.util.List; | ||
|
||
|
||
class WinnerDeterminerTest { | ||
|
||
@Test | ||
@DisplayName("λ¨λ μ°μΉμκ° μ μμ μΌλ‘ νλ³λμ΄μΌ νλ€") | ||
void μ°μΉμ_νλ³() { | ||
Car car1 = new Car("pobi", 4); | ||
Car car2 = new Car("crong", 3); | ||
Car car3 = new Car("honux", 2); | ||
|
||
Cars cars = new Cars(List.of(car1, car2, car3)); | ||
|
||
Assertions.assertThat(WinnerDeterminer.findWinners(cars)).isEqualTo(List.of(car1)); | ||
} | ||
|
||
@Test | ||
@DisplayName("곡λ μ°μΉμκ° μμ κ²½μ°, λͺ¨λ μ°μΉμλ‘ νλ³λμ΄μΌ νλ€") | ||
void 곡λ_μ°μΉμ_νλ³() { | ||
Car car1 = new Car("pobi"); | ||
Car car2 = new Car("crong"); | ||
Car car3 = new Car("honux"); | ||
|
||
Cars cars = new Cars(List.of(car1, car2, car3)); | ||
|
||
NumberGenerator fixedNumberGenerator = new FixedNumberGenerator(4); | ||
car1.move(fixedNumberGenerator); | ||
car2.move(fixedNumberGenerator); | ||
|
||
Assertions.assertThat(WinnerDeterminer.findWinners(cars)).isEqualTo(List.of(car1, car2)); | ||
} | ||
} |
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.
Nameλ νλ² μμκ° ν¬μ₯μ ν΄λ³΄μλ©΄ μ΄λ¨κΉμ?