-
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
๐ 5๋จ๊ณ - ์๋์ฐจ ๊ฒฝ์ฃผ(๋ฆฌํฉํ ๋ง) #6078
base: baeksangha
Are you sure you want to change the base?
Changes from all commits
2d671a8
c871377
bb17bfe
83009f8
adb180a
8b55e9a
2908754
24d38bf
3162b06
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package edu.nextstep.camp.carracing; | ||
|
||
import edu.nextstep.camp.carracing.domain.Cars; | ||
import edu.nextstep.camp.carracing.util.RandomNumberGenerator; | ||
import edu.nextstep.camp.carracing.view.ResultView; | ||
|
||
import java.util.List; | ||
|
||
import static edu.nextstep.camp.carracing.view.InputView.getTryCount; | ||
import static edu.nextstep.camp.carracing.view.InputView.inputCarNames; | ||
|
||
public class CarRacingApplication { | ||
public static void main(String[] args) { | ||
List<String> carNames = inputCarNames(); | ||
int tryCount = getTryCount(); | ||
|
||
Cars cars = Cars.fromNames(carNames); | ||
|
||
ResultView.printResultMessage(); | ||
for (int i = 0; i < tryCount; i++) { | ||
cars.moveCars(new RandomNumberGenerator(10)); | ||
ResultView.printCarsStatus(cars); | ||
} | ||
ResultView.printWinners(cars.getWinners()); | ||
} | ||
} |
This file was deleted.
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package edu.nextstep.camp.carracing.domain; | ||
|
||
public class Car { | ||
private static final int MOVE_THRESHOLD = 4; | ||
|
||
private final CarName name; | ||
private Position position; | ||
|
||
public Car(String name) { | ||
this(name, new Position()); | ||
} | ||
|
||
public Car(String name, Position position) { | ||
this.name = new CarName(name); | ||
this.position = position; | ||
} | ||
|
||
public void move(int number) { | ||
if (number >= MOVE_THRESHOLD) { | ||
this.position = this.position.increment(); | ||
} | ||
} | ||
|
||
public boolean isMaxPosition(int position) { | ||
return this.position.isSame(position); | ||
} | ||
|
||
public int getMaxValue(int value) { | ||
return this.position.max(value); | ||
} | ||
|
||
public String getNameValue() { | ||
return this.name.getName(); | ||
} | ||
|
||
public Position getPosition() { | ||
return this.position; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package edu.nextstep.camp.carracing.domain; | ||
|
||
import edu.nextstep.camp.carracing.util.NumberGenerator; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class Cars { | ||
private final List<Car> values; | ||
|
||
public Cars(List<Car> cars) { | ||
this.values = cars; | ||
} | ||
|
||
public static Cars fromNames(List<String> carNames) { | ||
List<Car> cars = new ArrayList<>(); | ||
Comment on lines
+16
to
+17
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. ์ ์ ํฉํ ๋ฆฌ ๋ฉ์๋๋ก ๊ฐ๊ฒฐํ๊ฒ ํํ์ด ๊ฐ๋ฅํ๋ค์! ๐ ๐ |
||
for (String carName : carNames) { | ||
cars.add(new Car(carName)); | ||
} | ||
return new Cars(cars); | ||
} | ||
|
||
public void moveCars(NumberGenerator generator) { | ||
for (Car car : this.values) { | ||
car.move(generator.generateNumber()); | ||
} | ||
} | ||
|
||
private int getMaxPosition() { | ||
int maxPosition = 0; | ||
for (Car car : this.values) { | ||
maxPosition = car.getMaxValue(maxPosition); | ||
} | ||
return maxPosition; | ||
} | ||
|
||
public List<String> getWinners() { | ||
int winnerPosition = getMaxPosition(); | ||
return this.values.stream() | ||
.filter(car -> car.isMaxPosition(winnerPosition)) | ||
.map(Car::getNameValue) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
public List<Car> getValues() { | ||
return this.values; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package edu.nextstep.camp.carracing.domain; | ||
|
||
import java.util.Objects; | ||
|
||
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. var position1 = new Position(5);
var position2 = new Position(5);
position1.equals(position2); // false ๊ฐ์ ๊ฐ์๋ณด์ด๋๋ฐ ์ ๋ค๋ฅด๋ค๊ณ ๋์ฌ๊น์? 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. bb17bfe |
||
private final int value; | ||
|
||
public Position() { | ||
this(0); | ||
} | ||
|
||
public Position(int value) { | ||
if (value < 0) { | ||
throw new IllegalArgumentException("์์น ๊ฐ์ ์์๊ฐ ๋ค์ด๊ฐ ์ ์์ต๋๋ค."); | ||
} | ||
this.value = value; | ||
} | ||
|
||
public Position increment() { | ||
return new Position(this.value + 1); | ||
} | ||
|
||
public boolean isSame(int number) { | ||
return this.value == number; | ||
} | ||
|
||
public int max(int number) { | ||
return Math.max(this.value, number); | ||
} | ||
|
||
public int getValue() { | ||
return this.value; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (!(o instanceof Position)) { | ||
return false; | ||
} | ||
Position other = (Position) o; | ||
return this.value == other.value; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(value); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package edu.nextstep.camp.carracing.util; | ||
|
||
public class FixedNumberGenerator implements NumberGenerator { | ||
private final int number; | ||
|
||
public FixedNumberGenerator(int number) { | ||
this.number = number; | ||
} | ||
|
||
@Override | ||
public int generateNumber() { | ||
return number; | ||
} | ||
} |
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.
๋ถ๋ณ ์ฒ๋ฆฌ ์ข๋ค์! ๐