Skip to content
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๋‹จ๊ณ„ - ์ž๋™์ฐจ ๊ฒฝ์ฃผ(๋ฆฌํŒฉํ† ๋ง) #5818

Open
wants to merge 32 commits into
base: jonghyeonleee
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
e66ce53
refactor: state๋ฅผ position์œผ๋กœ ๋ณ€๊ฒฝ
Oct 15, 2024
065e71c
refactor: random ์ธํ„ฐํŽ˜์ด์Šค์— ๋Œ€ํ•œ ๋ช…์นญ์„ ์ถ”์ƒ์ ์œผ๋กœ ๋ณ€๊ฒฝ
Oct 15, 2024
90057c1
feat: Cars ์ผ๊ธ‰ ์ฝœ๋ž™์…˜ ๊ตฌํ˜„
Oct 15, 2024
d905f4a
refactor: ์ฝ”๋“œ ์ •๋ฆฌ
Oct 15, 2024
9d3b593
refactor: Random์„ controller ๋‹จ์—์„œ ์ œ์–ดํ•˜๋„๋ก ์ˆ˜์ •
Oct 15, 2024
99749ca
refactor: ์™ธ๋ถ€์—์„œ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฐ’์˜ ์ ‘๊ทผ ์ œ์–ด์ž ๋ณ€๊ฒฝ
Oct 15, 2024
f6053fa
refactor: ๊ฐ„์ ‘ ํ…Œ์ŠคํŠธ ์ œ๊ฑฐ
Oct 15, 2024
8d16b5f
feat: position ๊ตฌํ˜„
Oct 15, 2024
516281d
feat: position ์ƒ์„ฑ์ž ์ถ”๊ฐ€ ๊ตฌํ˜„
Oct 15, 2024
1332ebf
feat: postion add, get ํ•จ์ˆ˜ ๊ตฌํ˜„
Oct 15, 2024
f0c3b6a
refator: ํ”„๋ฆฌ๋ฏธํ‹ฐ๋ธŒ ํƒ€์ž…์˜ position์„ ๊ฐ์ฒด๋กœ ๋ณ€๊ฒฝ
Oct 15, 2024
ed49ad8
refactor: ๋Œ€์‹œ๋ฅผ ๋ฐ˜ํ™˜ํ•  ๋•Œ position ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ๊ธฐ๋Šฅ ์ˆ˜์ •
Oct 15, 2024
2591e7f
feat: ํฐ ๊ฐ’์ธ์ง€ ์—ฌ๋ถ€๋ฅผ ๊ตฌ๋ถ„ํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜ ๊ตฌํ˜„ ๋ฐ car ๋ฉ”์„œ๋“œ ๋Œ€์ฒด
Oct 15, 2024
2a99564
refator: ๊ฐ„์ ‘ ํ…Œ์ŠคํŠธ ์ œ๊ฑฐ
Oct 15, 2024
c01dacd
refactor: ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ์ฝ”๋“œ ์ œ๊ฑฐ
Oct 15, 2024
ff2a4e7
refactor: cars ์ผ๊ธ‰ ์ฝœ๋ž™์…˜์— car ๊ฐ์ฒด๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ์ฝ”๋“œ ๊ตฌํ˜„
Oct 15, 2024
8533d99
refactor: List๋กœ ์„ ์–ธ๋œ ํŒŒ๋ผ๋ฏธํ„ฐ, ๋ฐ˜ํ™˜ ๊ฐ’์„ Cars๋กœ ๋ณ€๊ฒฝ
Oct 15, 2024
e386be1
refactor: cars ๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ๋ฆฌํŒฉํ† ๋ง ์ง„ํ–‰
Oct 15, 2024
4fd6712
feat: carName ๊ฐ์ฒด ๊ตฌํ˜„
Oct 15, 2024
7651e0c
refactor: String ํ”„๋ฆฌ๋ฏธํ‹ฐ๋ธŒ ํƒ€์ž…์—์„œ CarName ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ์ˆ˜์ •
Oct 15, 2024
094b6be
refactor: cars์—์„œ maxposition์„ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ๋ฆฌํŒฉํ† ๋ง ์ง„ํ–‰
Oct 15, 2024
0bee42d
refactor: Cars ๊ฐ์ฒด ๋‚ด๋ถ€์—์„œ ์šฐ์Šน์ž๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ๋ฆฌํŒฉํ† ๋ง
Oct 15, 2024
8e94225
refactor: carSize๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋งค์„œ๋“œ ๊ตฌํ˜„
Oct 15, 2024
1643450
refactor: resultview์—์„œ ์‚ฌ์šฉํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ Cars ํด๋ž˜์Šค ๋‚ด๋ถ€์—์„œ ๊ตฌํ˜„ํ•˜์—ฌ ํ…Œ์ŠคํŠธ ํ•˜๋„๋ก ์ˆ˜์ •
Oct 15, 2024
9a3199f
refactor: ๋ฉ”์ธ ํ•จ์ˆ˜๋Š” ์‹œ์ž‘์  ์—ญํ• ๋งŒ ํ•˜๊ณ  ์‹ค์ œ ์„œ๋น„์Šค(์ปจํŠธ๋กค)๋Š” ๋ถ„๋ฆฌ, ํ•จ์ˆ˜๋ช…์„ ๋ช…ํ™•ํ•˜๊ฒŒ ์ˆ˜์ •
Oct 22, 2024
16ee9f8
refactor: sout์„ view ๋‹จ์œผ๋กœ ๋ถ„๋ฆฌ, ์‚ฌ์šฉํ•˜์ง€ ์•Š์€ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์ œ๊ฑฐ
Oct 22, 2024
be64fc4
refactor: carlist -> caritems๋กœ ๋„ค์ด๋ฐ ์ˆ˜์ •, ์ผ๊ธ‰ ๊ฐ์ฒด์˜ item์„ private๋กœ ์„ ์–ธ
Oct 22, 2024
13b1a3c
refactor: ๋ฐฉ์–ด์  ๋ณต์‚ฌ ๊ธฐ๋ฒ• ์‚ฌ์šฉํ•˜์—ฌ get์œผ๋กœ ๋ฆฌํ„ด๋ฐ›์€ ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋„๋ก ์ˆ˜์ •
Oct 22, 2024
0c567b0
refactor: ๊ฐ„์ ‘ ํ…Œ์ŠคํŠธ๋ฅผ ํ†ตํ•œ private ํ•จ์ˆ˜ ํ…Œ์ŠคํŠธ, ๊ธฐ์กด ํ…Œ์ŠคํŠธ ์ œ๊ฑฐ
Oct 22, 2024
8e329e8
fix: ํ…Œ์ŠคํŠธ ์˜ค๋ฅ˜ ์ˆ˜์ •
Oct 22, 2024
81149cf
refactor: ์ธํ„ฐํŽ˜์ด์Šค์—์„œ ๊ตฌ์ฒด์ ์ธ ๋‚ด์šฉ์„ ๋ฐฉํ–ฅ์„ฑ์„ ๋‚˜ํƒ€๋‚ด๋„๋ก ๋ณ€๊ฒฝ
Oct 22, 2024
47bdbbc
refactor: ์ž๋™์ฐจ ์›€์ง์ž„์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ๋ช…ํ™•ํ•˜๊ฒŒ ํ‘œํ˜„
Oct 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions src/main/java/com/racing/Main.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.racing;

import com.racing.domain.Car;
import com.racing.domain.Cars;
import com.racing.ui.InputView;
import com.racing.ui.ResultView;
import com.racing.utils.CarHelper;
import com.racing.utils.RacingHelper;

import java.util.ArrayList;
import java.util.List;

public class Main {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ด๋ฒˆ์—” Main์˜ ์ฑ…์ž„์„ ์ผ๋ถ€ ๋ถ„๋ฆฌํ•ด๋ณด๋Š”๊ฑด ์–ด๋–จ๊นŒ์š”?
Main์—์„œ๋Š” ๋กœ์ง์„ ์‹œ์ž‘ํ•˜๋Š” ์ •๋„์˜ ์ฑ…์ž„๋งŒ ๊ฐ€์ง€๋Š”๊ฒŒ ๋” ์ ์ ˆํ•ฉ๋‹ˆ๋‹ค.
๊ทธ ์ž์ฒด๋กœ๋„ ์ง„์ž…์  ์—ญํ• ์„ ํ•˜๊ณ  ์žˆ๊ฑฐ๋“ ์š”.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

9a3199f

๋ฉ”์ธ ์ง„์ž…์ ์„ ๋ถ„๋ฆฌํ–ˆ์Šต๋‹ˆ๋‹ค!

Expand All @@ -15,34 +16,40 @@ public static void main(String[] args) {

List<String> validCarNames = CarHelper.splitCarName(carNames);

List<Car> carList = createCars(validCarNames);
runRace(carList, tryNumber);
Cars cars = createCars(validCarNames);

List<Car> winners = CarHelper.determineWinners(carList);
runRace(cars, tryNumber);

Cars winners =
cars.determineWinners();
ResultView.printWinners(winners);
}

private static List<Car> createCars(List<String> carNames) {
List<Car> carList = new ArrayList<>();
private static Cars createCars(List<String> carNames) {
Cars cars = new Cars();

for (String carName : carNames) {
carList.add(new Car(0, carName));
cars.addCars(new Car(0, carName));
}

return carList;
return cars;
}

private static void runRace(List<Car> carList, int tryNumber) {
private static void runRace(Cars cars, int tryNumber) {
ResultView.printStartMessage();
for (int i = 0; i < tryNumber; i++) {
moveAllCars(carList);
ResultView.printRoundResult(i + 1, carList);
moveAllCars(cars);

int roundNumber = i + 1;
ResultView.printRoundResult(roundNumber, cars);
}
}

private static void moveAllCars(List<Car> carList) {
for (Car car : carList) {
car.move();
private static void moveAllCars(Cars cars) {
RacingHelper racingHelper = new RacingHelper();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RacingHelper ๋ณด๋‹ค๋Š” ๋” ๊ดœ์ฐฎ์€ ์ด๋ฆ„์œผ๋กœ ๋ณ€๊ฒฝํ•ด์ฃผ์„ธ์š”.
์ƒ์œ„ ์ธํ„ฐํŽ˜์ด์Šค์ธ RandomStrategy ๋ฅผ ์—ฐ์ƒํ•ด๋‚ด๊ธฐ ์–ด๋ ต์Šต๋‹ˆ๋‹ค.
์ธํ„ฐํŽ˜์ด์Šค์˜ ์ด๋ฆ„์ด ์ด๋ฏธ ํ•˜์œ„ ๊ตฌํ˜„์ฒด์˜ ์—ญํ• ์„ ์ •ํ•ด๋ฒ„๋ ค์„œ ๊ทธ๋Ÿฐ ๊ฒƒ์ผ์ˆ˜๋„ ์žˆ์–ด์š”.

๊ทธ๋ฆฌ๊ณ  ํ•˜์œ„ ๊ตฌํ˜„์ฒด๋ฅผ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•˜๋ฉด ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํ†ตํ•œ ์•ฝ๊ฒฐํ•ฉ์ด๋ผ๋Š” ์žฅ์ ์„ ๊ฐ€์ ธ๊ฐˆ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
๊ตฌํ˜„์ฒด๊ฐ€ ์•„๋‹ˆ๋ผ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํ™œ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ๋ฐ”๊ฟ”๋ณด์„ธ์š”.

๋ฐฉ๋ฒ•์€ ์—ฌ๋Ÿฌ๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

  1. Car ์˜ ์ƒ์„ฑ์ž์—์„œ RandomStrategy ๋ฅผ ์ฃผ์ž…๋ฐ›์•„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
  2. moveAllCars ์—์„œ ์ง์ ‘ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๋งค๊ฐœ๋ณ€์ˆ˜ ํ˜•ํƒœ๋กœ RandomStrategy์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
    • ์ด๋ ‡๊ฒŒ ๋˜๋ฉด moveAllCars์—์„œ ์ง์ ‘ ์ƒ์„ฑํ•˜์ง€ ์•Š๊ณ  ์™ธ๋ถ€๋กœ๋ถ€ํ„ฐ ์ฃผ์ž…๋ฐ›๋Š” ํ˜•ํƒœ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.
    • ๋”ฐ๋ผ์„œ ์‚ฌ์šฉ์ž์˜ ํ•„์š”์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ๊ตฌํ˜„์ฒด๋กœ ๋ฐ”๊พธ์–ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Copy link
Author

@jonghyeonLeee jonghyeonLeee Oct 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

47bdbbc

RacingHelper๋Š” ๋ถˆ๋ช…ํ™•ํ•œ ๋ถ€๋ถ„์ด ์žˆ์–ด ์ฐจ์˜ ์›€์ง์ž„์„ ๋‚˜ํƒ€๋‚ด๋Š” CarMovement๋กœ ๋ณ€๊ฒฝํ–ˆ์Šต๋‹ˆ๋‹ค.

moveAllCars์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›์•„์˜ค๋Š”๊ฑด ์ดํ•ดํ•˜์˜€์ง€๋งŒ ๊ตฌํ˜„์„ ์–ด๋–ป๊ฒŒ ํ• ์ง€ ๋„์ €ํžˆ ์ƒ๊ฐ๋‚˜์ง€ ์•Š์•„ ์ˆ˜์ •ํ•˜์ง€ ๋ชปํ–ˆ์Šท๋นˆ๋‹ค.. ใ…œใ…œ
ํ˜น์‹œ ์‹œ๊ฐ„ ๋‚˜์‹ค ๋•Œ ํŽ˜์–ด์ฝ”๋”ฉ์ด๋‚˜ ๋‹ค๋ฅธ ๋„์›€์„ ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ๋ถ€๋ถ„์ด ์žˆ์„์ง€ ๋ฌธ์˜๋“œ๋ฆฝ๋‹ˆ๋‹ค!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. service์˜ ํ•„๋“œ์— CarMovement ๋ฅผ ํ•„๋“œ๋กœ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ service๋ฅผ ์ดˆ๊ธฐํ™”ํ•  ๋•Œ ์ „๋žต์„ ์„ ํƒ์ ์œผ๋กœ ๋ฐ€์–ด๋„ฃ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  2. moveAllCars์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์ง€๊ธˆ cars ํ•˜๋‚˜๋งŒ ์žˆ๋Š”๋ฐ cars, movementStrategy 2๊ฐœ๋กœ ํ•ด๋‘์‹œ๋ฉด ์ด ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ์ „๋žต์„ ์„ ํƒ์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ „๋žต ํŒจํ„ด์˜ ๊ธฐ๋ณธ์€ ์–ธ์ œ๋“ ์ง€ ์ „๋žต์„ ๋ฐ”๊พธ์–ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ž„์„ ์ƒ๊ฐํ•ด๋ณด์‹œ๋ฉด ๋„์›€์ด ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


for (Car car : cars.getCarList()) {
car.move(racingHelper.shouldMove());
}
}
}
36 changes: 14 additions & 22 deletions src/main/java/com/racing/domain/Car.java
Original file line number Diff line number Diff line change
@@ -1,48 +1,40 @@
package com.racing.domain;

import com.racing.utils.RacingHelper;

public class Car {

private int state = 0;
private String name;
private Position position;
private CarName carName;

// ๊ธฐ๋ณธ ์ƒ์„ฑ์ž
public Car() {
this(0, ""); // ๊ธฐ๋ณธ๊ฐ’ 0์œผ๋กœ ๋‹ค๋ฅธ ์ƒ์„ฑ์ž ํ˜ธ์ถœ
}

// ์ƒํƒœ ์ง€์ • ์ƒ์„ฑ์ž
public Car(int state, String name) {
this.state = state;
this.name = name;
public Car(int position, String carName) {
this.position = new Position(position);
this.carName = new CarName(carName);
}

public void move() {
RacingHelper racingHelper = new RacingHelper();

if (racingHelper.shouldMove()) {
state++;
public void move(boolean isMovable) {
if (isMovable) {
position.addPosition();
}
}

public String displayRacingState() {
return displayCarName() + " : " + displayState();
}

public String displayState() {
return "-".repeat(state);
public String displayRacingPosition() {
return carName.getCarName() + " : " + position.displayDashAsPosition();
}

public String displayCarName() {
return name;
return carName.getCarName();
}

public int getState() {
return state;
public int getPosition() {
return position.getPosition();
}

public boolean isDefeated(int verseState) {
return state < verseState;
return position.isBiggerThanPosition(verseState);
}
}
32 changes: 32 additions & 0 deletions src/main/java/com/racing/domain/CarName.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.racing.domain;

import java.util.Objects;

public class CarName {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์›์ž ๋‹จ์œ„์˜ ์ฑ…์ž„ ๋ถ„๋ฆฌ ๐Ÿ‘

private final String carName;

public CarName(String carName) {
if (carName == null || carName.trim().isEmpty()) {
throw new IllegalArgumentException("Car name cannot be empty or blank.");
}

this.carName = carName;
}

public String getCarName() {
return carName;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CarName carName1 = (CarName) o;
return Objects.equals(carName, carName1.carName);
}

@Override
public int hashCode() {
return Objects.hashCode(carName);
}
}
76 changes: 76 additions & 0 deletions src/main/java/com/racing/domain/Cars.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.racing.domain;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class Cars {
List<Car> carList;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private์ด ์•„๋‹Œ ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”?

๊ทธ๋ฆฌ๊ณ  List์™€ ๊ฐ™์€ ์ž๋ฃŒํ˜•์€ ํ•„์š”์— ๋”ฐ๋ผ ์–ธ์ œ๋“ ์ง€ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
์˜ˆ๋ฅผ ๋“ค๋ฉด Set์œผ๋กœ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๊ฒ ์ฃ ?

๋”ฐ๋ผ์„œ carList๋ณด๋‹ค๋Š” ๋ณต์ˆ˜ํ˜• ์šฉ์–ด๋ฅผ ์‚ฌ์šฉํ•˜์‹œ๋Š”๊ฑธ ์ถ”์ฒœ๋“œ๋ฆฝ๋‹ˆ๋‹ค.
Cars์ฒ˜๋Ÿผ ์ผ๊ธ‰์ปฌ๋ ‰์…˜์ธ ๊ฒฝ์šฐ์—๋Š” items ๊ฐ™์€ ์šฉ์–ด๋ฅผ ์‚ฌ์šฉํ•ด๋„ ๋‚˜์˜์ง€ ์•Š์•„์š”.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

be64fc4

private๋กœ ํ•„๋“œ๋ฅผ ๋ง‰์•˜์–ด์•ผ ํ–ˆ๋Š”๋ฐ ๋นผ๋จน์—ˆ๋„ค์š” ใ…œใ…œ ์ˆ˜์ •ํ–ˆ์Šต๋‹ˆ๋‹ค.
items๋กœ ๋ณ€๊ฒฝํ•  ๊ฒฝ์šฐ Cars.getItems() ์™€ ๊ฐ™์ด ์ผ๋ฐ˜์ ์ธ ๋ฉ”์„œ๋“œ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์–ด ๊ฐ€๋…์„ฑ์—์„œ๋„ ์ข‹์•„ ๋ณด์—ฌ ์ˆ˜์ •ํ–ˆ์Šต๋‹ˆ๋‹ค.

์ค‘๋ณต๋œ ๊ฐ’์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ ์—ฌ๋ถ€๊ฐ€ ๋ฌธ์ œ์— ๋ช…์‹œ๋˜์ง€ ์•Š์•„ ์Šต๊ด€์ ์œผ๋กœ List๋ฅผ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ ์ถ”ํ›„ ๊ด€๋ จ๋œ ๋‚ด์šฉ์ด ์žˆ์œผ๋ฉด Set์œผ๋กœ ๋ฆฌํŒฉํ† ๋ง ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค! :)


public Cars() {
carList = new ArrayList<>();
}

public Cars(List<Car> carList) {
this.carList = carList;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋ฐฉ์–ด์  ๋ณต์‚ฌ ๊ธฐ๋ฒ•์— ๋Œ€ํ•ด ๊ณต๋ถ€ํ•ด๋ณด์‹œ๋ฉด ์ข‹์Šต๋‹ˆ๋‹ค.

Cars๋ฅผ ์ƒ์„ฑํ•œ ์œ„์น˜ ์ฃผ๋ณ€์—์„œ Cars๋ฅผ ์ƒ์„ฑํ•˜๋Š”๋ฐ ์‚ฌ์šฉํ•œ List๋ฅผ ๋ณ€๊ฒฝํ•˜๋ฉด Cars๋„ ์˜ํ–ฅ์„ ๋ฐ›๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

13b1a3c

๋ฐฉ์–ด์  ๋ณต์‚ฌ ๊ธฐ๋ฒ• ๊ณต๋ถ€ ํ›„ ์™ธ๋ถ€์—์„œ ๋ณ€๊ฒฝํ•˜์ง€ ๋ชปํ•˜๋„๋ก ํ•ด๋‹น ๋‚ด์šฉ ๋ฐ˜์˜ ํ–ˆ์Šต๋‹ˆ๋‹ค.


public void addCars(Car car) {
carList.add(car);
}

public List<Car> getCarList() {
return this.carList;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public List<Car> getCarList() {
return this.carList;
}
public List<Car> getCarList() {
return Collections.unmodifiableList(this.carList);
}

์ฝ๊ธฐ ์ „์šฉ ๋ฐ์ดํ„ฐ๋กœ ๋ณ€๊ฒฝํ•ด์„œ ๋‚ด๋ณด๋‚ด์…”์•ผ ์™ธ๋ถ€์˜ ์˜ํ–ฅ์„ ๋ฐ›์ง€ ์•Š๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
getCarList().add(car) ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฉด Cars๊ฐ€ ๋ณ€๊ฒฝ๋˜์–ด ๋ฒ„๋ฆฝ๋‹ˆ๋‹ค.


public int findMaxPosition() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private ๋ฉ”์†Œ๋“œ์—ฌ๋„ ๊ดœ์ฐฎ์ง€ ์•Š์„๊นŒ์š”?

Copy link
Author

@jonghyeonLeee jonghyeonLeee Oct 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0c567b0

์ž‘์„ฑํ•  ๋•Œ ์ง์ ‘ ํ…Œ์ŠคํŠธ๋ฅผ ํ•˜์˜€๋Š”๋ฐ ์ถ”ํ›„ ๊ฐ„์ ‘ ํ…Œ์ŠคํŠธ๋ฅผ ํ•˜๊ณ  ์žˆ๋‹ค ํŒ๋‹จํ•˜์—ฌ private ๋ณ€๊ฒฝ ํ›„ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์ œ๊ฑฐํ–ˆ์Šต๋‹ˆ๋‹ค!
์ผ๋ฐ˜์ ์œผ๋กœ ์ด๋Ÿฐ์‹์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋Š”์ง€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private ๋„ ํ…Œ์ŠคํŠธ๊ฐ€ ํ•„์š”ํ•  ๋งŒํผ ๋ณต์žกํ•œ ์ˆœ๊ฐ„๋„ ๋ถ„๋ช… ์žˆ์ง€๋งŒ, public์„ ํ†ตํ•ด ๊ฐ„์ ‘์ ์œผ๋กœ ํ…Œ์ŠคํŠธํ•˜๋Š”๊ฒŒ ๋ฐฉ๋ฒ• ์ž์ฒด๊ฐ€ ๋” ์˜ฌ๋ฐ”๋ฆ…๋‹ˆ๋‹ค.

private์„ ๊ตณ์ด ๋”ฐ์ง€์ž๋ฉด public์˜ ์ฝ”๋“œ๋ฅผ ๋”ฐ๋กœ ์ ‘์–ด๋‘” ๋Š๋‚Œ์ด๋‹ˆ๊นŒ์š”.

int maxPosition = 0;
for (Car car : this.carList) {
maxPosition = car.isDefeated(maxPosition) ? maxPosition : car.getPosition();
}

return maxPosition;
}

public Cars determineWinners() {
int maxPosition = this.findMaxPosition();

List<Car> winnerList = this.carList.stream()
.filter(car -> car.getPosition() == maxPosition)
.collect(Collectors.toList());

return new Cars(winnerList);
}

public int carsSize() {
return this.carList.size();
}

public String getCarName(int index) {
if (index < 0 || index >= carList.size()) {
throw new IndexOutOfBoundsException("Invalid car index");
}

return carList.get(index).displayCarName();
}

public void printRacingPositions() {
for (Car car : carList) {
System.out.println(car.displayRacingPosition());
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

domain์—์„œ๋Š” sout ์„ ์‚ฌ์šฉํ•˜์‹œ๋ฉด UI๋ฅผ ๋ถ„๋ฆฌํ•œ ์˜๋ฏธ๊ฐ€ ํ‡ด์ƒ‰๋ฉ๋‹ˆ๋‹ค.

sout์€ UI์—์„œ๋งŒ ์‚ฌ์šฉํ•ด์ฃผ์„ธ์š”.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

16ee9f8

ํ•ด๋‹น ๋‚ด์šฉ ์ ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค :)


@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Cars cars = (Cars) o;
return Objects.equals(carList, cars.carList);
}

@Override
public int hashCode() {
return Objects.hashCode(carList);
}
}
44 changes: 44 additions & 0 deletions src/main/java/com/racing/domain/Position.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.racing.domain;

import java.util.Objects;

public class Position {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๐Ÿ‘

private int position;

public Position() {
this(0);
}

public Position(int position) {
this.position = position;
}

public void addPosition() {
this.position++;
}

public int getPosition() {
return position;
}

public String displayDashAsPosition() {
return "-".repeat(this.position);
}

public boolean isBiggerThanPosition(int number) {
return this.position < number;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Position position1 = (Position) o;
return position == position1.position;
}

@Override
public int hashCode() {
return Objects.hashCode(position);
}
}
18 changes: 8 additions & 10 deletions src/main/java/com/racing/ui/ResultView.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
package com.racing.ui;

import com.racing.domain.Car;

import java.util.List;
import com.racing.domain.Cars;

public class ResultView {
public static void printStartMessage() {
System.out.println("์‹คํ–‰ ๊ฒฐ๊ณผ");
}

public static void printRoundResult(int round, List<Car> carList) {
public static void printRoundResult(int round, Cars cars) {
System.out.println("# " + round + "ํšŒ์ฐจ");
for (Car car : carList) {
System.out.println(car.displayRacingState());
}

cars.printRacingPositions();

System.out.println();
}

public static void printWinners(List<Car> winners) {
public static void printWinners(Cars winners) {
System.out.print("์ตœ์ข… ์šฐ์Šน์ž: ");
int winnerSize = winners.size();
int winnerSize = winners.carsSize();
for (int i = 0; i < winnerSize; i++) {
System.out.print(winners.get(i).displayCarName());
System.out.print(winners.getCarName(i));
printComma(i < winnerSize - 1);
}

Expand Down
31 changes: 1 addition & 30 deletions src/main/java/com/racing/utils/CarHelper.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.racing.utils;

import com.racing.domain.Car;

import java.util.ArrayList;
import java.util.List;

Expand All @@ -28,39 +26,12 @@ private static void addIfValid(String carName, List<String> validNames) {
}
}

public static boolean isValidCarName(String carName) {
private static boolean isValidCarName(String carName) {
if (carName.isEmpty()) {
return false;
}

return carName.length() < VALID_CAR_NAME;
}

public static List<Car> determineWinners(List<Car> carList) {
List<Car> winners = new ArrayList<>();
int maxState = findMaxState(carList);

for (Car car : carList) {
addWinners(car, maxState, winners);
}

return winners;
}

private static void addWinners(Car car, int maxState, List<Car> winners) {
if (car.getState() == maxState) {
winners.add(car);
}
}

private static int findMaxState(List<Car> carList) {
int maxState = 0;
for (Car car : carList) {
maxState = car.isDefeated(maxState) ? maxState : car.getState();

}

return maxState;
}

}
2 changes: 1 addition & 1 deletion src/main/java/com/racing/utils/RacingHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* ๋ ˆ์ด์‹ฑ์—์„œ ์‚ฌ์šฉ ํ•  ํ—ฌํผ ๋ฉ”์„œ๋“œ๋ฅผ ๋ชจ์•„๋†“์€ ํด๋ž˜์Šค
*/
public class RacingHelper implements RandomMover {
public class RacingHelper implements RandomStrategy {
private static final int RANDOM_NUMBER_RANGE = 10;
private static final int FORWARD_MINIMUM_NUMBER = 4;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.racing.utils;

public interface RandomMover {
public interface RandomStrategy {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ด ์ธํ„ฐํŽ˜์ด์Šค๋ช…์—์„œ Random์„ ์–ธ๊ธ‰ํ•˜๋ฉด ๊ตฌ์ฒด์ ์ธ ๋ฐฉ๋ฒ•์„ ๋ช…์‹œํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
์ธํ„ฐํŽ˜์ด์Šค๋Š” ๋ฐฉํ–ฅ์„ "์ œ์‹œ"ํ•˜๋Š” ์šฉ๋„๋กœ๋งŒ ์‚ฌ์šฉํ•˜์‹œ๋Š”๊ฒŒ ์ข‹์•„์š”.

๊ทผ๋ฐ ์ด ์ธํ„ฐํŽ˜์ด์Šค๋Š” ์ด๋ฏธ ๋ฐฉํ–ฅ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๊ฐˆ ๊ธธ๊นŒ์ง€ ์ „๋ถ€ ์•Œ๋ ค์ฃผ๋Š” ๋Š๋‚Œ์ด๋„ค์š”.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

81149cf

RandomStrategy -> MovementStrategy ๋กœ ๋ณ€๊ฒฝํ–ˆ์Šต๋‹ˆ๋‹ค!
์ธํ„ฐํŽ˜์ด์Šค๋ช…์—์„œ ๊ตฌ์ฒด์ ์ธ ๋‚ด์šฉ์„ ์ œ๊ฑฐํ•˜๋Š” ๋ฐฉํ–ฅ์œผ๋กœ ์ˆ˜์ •ํ–ˆ์Šต๋‹ˆ๋‹ค.

public boolean shouldMove();
}
Loading