-
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 1 commit
2d671a8
c871377
bb17bfe
83009f8
adb180a
8b55e9a
2908754
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.
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,40 @@ | ||
package edu.nextstep.camp.carracing; | ||
package edu.nextstep.camp.carracing.domain; | ||
|
||
public class Car { | ||
private static final int MOVE_THRESHOLD = 4; | ||
private static final String MOVE_SYMBOL = "-"; | ||
|
||
private final CarName name; | ||
private int position; | ||
private final Position position; | ||
|
||
public Car(String name) { | ||
this(name, 0); | ||
this(name, new Position()); | ||
} | ||
|
||
public Car(String name, int position) { | ||
public Car(String name, Position position) { | ||
this.name = new CarName(name); | ||
this.position = position; | ||
} | ||
|
||
public void move(int number) { | ||
if (number >= MOVE_THRESHOLD) { | ||
position++; | ||
this.position.increment(); | ||
} | ||
} | ||
|
||
public String getCurrentPositionString() { | ||
return String.format("%s : %s", name.getName(), MOVE_SYMBOL.repeat(position)); | ||
return String.format("%s : %s", name.getName(), position.repeatSymbol(MOVE_SYMBOL)); | ||
} | ||
|
||
public boolean isMaxPosition(int position) { | ||
return this.position == position; | ||
return this.position.isSame(position); | ||
} | ||
|
||
public CarName getName() { | ||
return name; | ||
public int getMaxValue(int value) { | ||
return this.position.max(value); | ||
} | ||
|
||
public int getPosition() { | ||
return position; | ||
public CarName getName() { | ||
return name; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package edu.nextstep.camp.carracing.domain; | ||
|
||
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 CarRacing { | ||
|
||
private CarRacing() { | ||
throw new IllegalStateException("์ธ์คํด์ค ์์ฑ์ด ๋ถ๊ฐ๋ฅํ ํด๋์ค์ ๋๋ค."); | ||
} | ||
|
||
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(); | ||
cars.printCarStatus(); | ||
} | ||
ResultView.printWinners(cars.getWinners()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package edu.nextstep.camp.carracing.domain; | ||
|
||
import edu.nextstep.camp.carracing.view.ResultView; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static edu.nextstep.camp.carracing.util.RandomNumberGenerator.generateRandomNumber; | ||
|
||
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() { | ||
for (Car car : this.values) { | ||
car.move(generateRandomNumber()); | ||
} | ||
} | ||
Comment on lines
+24
to
+28
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. Car์ ์์ง์์ ํ
์คํธํ๊ธฐ ์ฝ๊ฒ ๋ง๋ค์ด์ก์ง๋ง 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. ์๊ฑฐ ์ด๋ป๊ฒ ํ๋ฉด ํ ์คํธํ๊ธฐ ์ฌ์ด ๊ตฌ์กฐ๋ก ์ฌ๊ตฌ์ฑํ ์ ์์๊น์? ์ข ๊ณ ๋ฏผํด๋ณด์๋๋ฐ ๊ตฌ์กฐ๊ฐ ์๋ ์ค๋ฆ ๋๋ค ๐โโ๏ธ 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. Car์ cars.moveCars(NumberGenerator generator) {
...
car.move(generator.random());
} NumberGenerator๋ฅผ ์ธํฐํ์ด์คํ ์ํค๊ณ ํ๋ก๋์
์ฝ๋์๋ ๋๋ค๋ฒํธ ์์ฑ๊ธฐ ๊ตฌํ์ฒด๋ฅผ, ์ด ๋ฐฉ์์ด ๊ผญ ์๋๋๋ผ๋ ๋ฐฉ๋ฒ์ ๋งค์ฐ ๋ง์ต๋๋ค. |
||
|
||
public void printCarStatus() { | ||
for (Car car : this.values) { | ||
ResultView.printCarStatus(car); | ||
} | ||
ResultView.printLine(); | ||
} | ||
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. ๋๋ฉ์ธ ๋ชจ๋ธ์ด View๋ฅผ ์์กดํ๊ณ ์๋ค์!
์๊ตฌ์ฌํญ์ ๋ฐ๋ผ ๋ฆฌํฉํ ๋ง ํด๋ณด๋ฉด ์ข๊ฒ ์ต๋๋ค! 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. ์๊ฑฐ ํฌํจํ ๋ฆฌํฉํ ๋ง ์ปค๋ฐ์ ๋๋ค. 2908754 |
||
|
||
public int getMaxPosition() { | ||
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.
|
||
int maxPosition = 0; | ||
for (Car car : this.values) { | ||
maxPosition = car.getMaxValue(maxPosition); | ||
} | ||
return maxPosition; | ||
} | ||
|
||
public Cars getWinners() { | ||
int winnerPosition = getMaxPosition(); | ||
List<String> result = new ArrayList<>(); | ||
for (Car car : this.values) { | ||
if (car.isMaxPosition(winnerPosition)) { | ||
result.add(car.getName().getName()); | ||
} | ||
} | ||
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.
๋ฉ์๋๋ฅผ ์ถ์ถํ๋ฉด์ depth๋ฅผ ์ค์ฌ๋ณด๋ฉด ์ข๊ฒ ์ต๋๋ค. |
||
return Cars.fromNames(result); | ||
} | ||
|
||
public List<String> getWinnerNames() { | ||
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.
๋ํ ์ถ๋ ฅ์ ์ํ ๊ธฐ๋ฅ์ธ ๊ฒ ๊ฐ๊ธฐ๋ํ๋ค์ |
||
List<String> result = new ArrayList<>(); | ||
for (Car value : this.values) { | ||
if (value.isMaxPosition(getMaxPosition())) { | ||
result.add(value.getName().getName()); | ||
} | ||
} | ||
return result; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package edu.nextstep.camp.carracing.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. 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 int value; | ||
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 Position() { | ||
this(0); | ||
} | ||
|
||
public Position(int value) { | ||
if (value < 0) { | ||
throw new IllegalArgumentException("์์น ๊ฐ์ ์์๊ฐ ๋ค์ด๊ฐ ์ ์์ต๋๋ค."); | ||
} | ||
this.value = value; | ||
} | ||
|
||
public void increment() { | ||
this.value++; | ||
} | ||
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. ๊ฐ ๊ฐ์ฒด๋ฅผ ๋ถ๋ณ์ผ๋ก ๋ง๋ค์ด๋ณด๋ ๊ฒ์ ์ด๋จ๊น์?
์๋ฅผ ๋ค์ด, 1 + 1 = 2์์ ๊ธฐ์กด์ 1(๊ฐ)์ด 2๋ก ๋ณํ๋ ๊ฒ์ด ์๋๋ผ, ์๋ก์ด 2(๊ฐ)๊ฐ ์์ฑ๋๋ ๊ฒ์ฒ๋ผ์! var newPosition = position.increment(); ์ด๋ ๊ฒ ํ๋ฉด ๋ถ๋ณ์ฑ์ ์ ์งํ ์ ์๊ณ , ๊ฐ์ฒด์ ์์์น ๋ชปํ ๋ณ๊ฒฝ์ ๋ฐฉ์งํ ์ ์์ต๋๋ค! ๐ https://tecoble.techcourse.co.kr/post/2021-05-16-dto-vs-vo-vs-entity/ ๊ด๋ จํด์ ์ฝ์ด๋ณด์๋ฉด ๋์์ด ๋ ๊ฑฐ์์ 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. ํฌ๋น๋ ๋ผ์ด๋ธ ๊ฐ์์์ ์ ๊ฐ ํ๊ฒ์ฒ๋ผ ํํํ์๊ธธ๋ ๋ฐ๋ผ ์ฐ๊ธด ํ๋๋ฐ, ๊ฐ์ฒด์ ๊ฐ๋ณ์ฑvs๋ถ๋ณ์ฑ ์ธก๋ฉด์์ ๋ถ๋ณ์ฑ์ ๊ฐ์ง ๊ฐ์ฒด๋ ๊ทธ๋งํผ ์ธ์คํด์ค๊ฐ ๋ง์ด ์์ฑ๋๋ ๊ฐ์ฒด ์์ฑ ๋น์ฉ์ด๋ GC์ ๋ถ๋ด์ด ์์ ์ ์๋ค๊ณ ์๊ฐํ์ต๋๋ค. 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 boolean isSame(int number) { | ||
return this.value == number; | ||
} | ||
|
||
public String repeatSymbol(String symbol) { | ||
return symbol.repeat(value); | ||
} | ||
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.
view ๊ฐ์ฒด๋ฅผ ์ง์ ์ ์ผ๋ก ์์กดํ์ง๋ ์์ง๋ง ์ถ๋ ฅ์ ์ํ ๋ฉ์๋๋ค์ 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. c871377 ์๋ฐ ์์ผ๋ก ์์ ํด ๋ณด์์ต๋๋ค. |
||
|
||
public int max(int number) { | ||
return Math.max(this.value, number); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,16 @@ | ||
package edu.nextstep.camp.carracing; | ||
package edu.nextstep.camp.carracing.util; | ||
|
||
import java.util.Random; | ||
|
||
public class RandomNumberGenerator { | ||
private static final int DEFAULT_BOUND = 10; | ||
private static final Random random = new Random(); | ||
|
||
private RandomNumberGenerator() { | ||
throw new IllegalStateException("์ธ์คํด์ค ์์ฑ์ด ๋ถ๊ฐ๋ฅํ ํด๋์ค์ ๋๋ค."); | ||
} | ||
|
||
public static int generateRandomNumber(int bound) { | ||
return random.nextInt(bound); | ||
public static int generateRandomNumber() { | ||
return random.nextInt(DEFAULT_BOUND); | ||
} | ||
} |
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.
์ ํ๋ฆฌ์ผ์ด์ ์คํ์ ์ง์ ์ ์ธ

main
๋ฉ์๋๊ฐ ๋๋ฉ์ธ ์์ญ์ ์์นํ๋ ๊ฒ์ ์ด์ํด ๋ณด์ฌ์main
์ ์ ํ๋ฆฌ์ผ์ด์ ์ ์ต์ด ์คํ์ด ๋๋ ์์ญ์ ๋๋ค์ค์ ๋ก์ง์ ์ํํ๋ ๋๋ฉ์ธ ๋ชจ๋ธ๊ณผ๋ ๋ถ๋ฆฌ๋๋ ๊ฒ์ด ์์ฐ์ค๋ฌ์ ๋ณด์ด๋๋ฐ,
๊ทธ๋ฆผ์์๋ ๋๋ฉ์ธ ์์ญ๋ณด๋ค๋ ์ปจํธ๋กค๋ฌ ๊ณ์ธต๊ณผ ๋ ๊ฐ๊น์ด ๋๋์ ์ค๋๋ค.
ํด๋น ํด๋์ค๋ฅผ ๋ค๋ฅธ ํจํค์ง ๊ณ์ธต์ผ๋ก ์ด๋์์ผ๋ณด๋ฉด ์ข์ ๊ฒ ๊ฐ์ต๋๋ค ๐
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.
8b55e9a
domain ๊ฐ์ฒด ์์๋ก ์ด๋ํ๊ณ , ํด๋์ค๋ช ๋ CarRacingApplication์ผ๋ก ๋ณ๊ฒฝํ์ต๋๋ค. ๊ฐ์ฌํฉ๋๋ค.