-
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๋จ๊ณ - ์๋์ฐจ ๊ฒฝ์ฃผ(๋ฆฌํฉํ ๋ง) #5818
base: jonghyeonleee
Are you sure you want to change the base?
Changes from 24 commits
e66ce53
065e71c
90057c1
d905f4a
9d3b593
99749ca
f6053fa
8d16b5f
516281d
1332ebf
f0c3b6a
ed49ad8
2591e7f
2a99564
c01dacd
ff2a4e7
8533d99
e386be1
4fd6712
7651e0c
094b6be
0bee42d
8e94225
1643450
9a3199f
16ee9f8
be64fc4
13b1a3c
0c567b0
8e329e8
81149cf
47bdbbc
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 |
---|---|---|
@@ -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 { | ||
|
@@ -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(); | ||
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. RacingHelper ๋ณด๋ค๋ ๋ ๊ด์ฐฎ์ ์ด๋ฆ์ผ๋ก ๋ณ๊ฒฝํด์ฃผ์ธ์. ๊ทธ๋ฆฌ๊ณ ํ์ ๊ตฌํ์ฒด๋ฅผ ๊ทธ๋๋ก ์ฌ์ฉํ๋ฉด ์ธํฐํ์ด์ค๋ฅผ ํตํ ์ฝ๊ฒฐํฉ์ด๋ผ๋ ์ฅ์ ์ ๊ฐ์ ธ๊ฐ ์ ์์ต๋๋ค. ๋ฐฉ๋ฒ์ ์ฌ๋ฌ๊ฐ์ง๊ฐ ์์ต๋๋ค.
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. RacingHelper๋ ๋ถ๋ช ํํ ๋ถ๋ถ์ด ์์ด ์ฐจ์ ์์ง์์ ๋ํ๋ด๋ CarMovement๋ก ๋ณ๊ฒฝํ์ต๋๋ค. moveAllCars์ ํ๋ผ๋ฏธํฐ๋ก ๋ฐ์์ค๋๊ฑด ์ดํดํ์์ง๋ง ๊ตฌํ์ ์ด๋ป๊ฒ ํ ์ง ๋์ ํ ์๊ฐ๋์ง ์์ ์์ ํ์ง ๋ชปํ์ท๋น๋ค.. ใ
ใ
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 (Car car : cars.getCarList()) { | ||
car.move(racingHelper.shouldMove()); | ||
} | ||
} | ||
} |
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); | ||
} | ||
} |
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 { | ||
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. ์์ ๋จ์์ ์ฑ ์ ๋ถ๋ฆฌ ๐ |
||
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); | ||
} | ||
} |
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; | ||||||||||||||
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. private์ด ์๋ ์ด์ ๊ฐ ์์๊น์? ๊ทธ๋ฆฌ๊ณ List์ ๊ฐ์ ์๋ฃํ์ ํ์์ ๋ฐ๋ผ ์ธ์ ๋ ์ง ๋ณ๊ฒฝํ ์ ์์ต๋๋ค. ๋ฐ๋ผ์ carList๋ณด๋ค๋ ๋ณต์ํ ์ฉ์ด๋ฅผ ์ฌ์ฉํ์๋๊ฑธ ์ถ์ฒ๋๋ฆฝ๋๋ค. 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. private๋ก ํ๋๋ฅผ ๋ง์์ด์ผ ํ๋๋ฐ ๋นผ๋จน์๋ค์ ใ
ใ
์์ ํ์ต๋๋ค. ์ค๋ณต๋ ๊ฐ์ ๋ํ ์ฒ๋ฆฌ ์ฌ๋ถ๊ฐ ๋ฌธ์ ์ ๋ช ์๋์ง ์์ ์ต๊ด์ ์ผ๋ก List๋ฅผ ์ฌ์ฉํ๋๋ฐ ์ถํ ๊ด๋ จ๋ ๋ด์ฉ์ด ์์ผ๋ฉด Set์ผ๋ก ๋ฆฌํฉํ ๋ง ํด๋ณด๊ฒ ์ต๋๋ค! :) |
||||||||||||||
|
||||||||||||||
public Cars() { | ||||||||||||||
carList = new ArrayList<>(); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
public Cars(List<Car> carList) { | ||||||||||||||
this.carList = carList; | ||||||||||||||
} | ||||||||||||||
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. ๋ฐฉ์ด์ ๋ณต์ฌ ๊ธฐ๋ฒ ๊ณต๋ถ ํ ์ธ๋ถ์์ ๋ณ๊ฒฝํ์ง ๋ชปํ๋๋ก ํด๋น ๋ด์ฉ ๋ฐ์ ํ์ต๋๋ค. |
||||||||||||||
|
||||||||||||||
public void addCars(Car car) { | ||||||||||||||
carList.add(car); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
public List<Car> getCarList() { | ||||||||||||||
return this.carList; | ||||||||||||||
} | ||||||||||||||
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.
Suggested change
์ฝ๊ธฐ ์ ์ฉ ๋ฐ์ดํฐ๋ก ๋ณ๊ฒฝํด์ ๋ด๋ณด๋ด์
์ผ ์ธ๋ถ์ ์ํฅ์ ๋ฐ์ง ์๊ฒ ๋ฉ๋๋ค. |
||||||||||||||
|
||||||||||||||
public int findMaxPosition() { | ||||||||||||||
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. private ๋ฉ์๋์ฌ๋ ๊ด์ฐฎ์ง ์์๊น์? 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. ์์ฑํ ๋ ์ง์ ํ
์คํธ๋ฅผ ํ์๋๋ฐ ์ถํ ๊ฐ์ ํ
์คํธ๋ฅผ ํ๊ณ ์๋ค ํ๋จํ์ฌ private ๋ณ๊ฒฝ ํ ํ
์คํธ ์ฝ๋ ์ ๊ฑฐํ์ต๋๋ค! 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. 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()); | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
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. domain์์๋ sout ์ ์ฌ์ฉํ์๋ฉด UI๋ฅผ ๋ถ๋ฆฌํ ์๋ฏธ๊ฐ ํด์๋ฉ๋๋ค. sout์ UI์์๋ง ์ฌ์ฉํด์ฃผ์ธ์. 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. ํด๋น ๋ด์ฉ ์ ์ฉํ์ต๋๋ค :) |
||||||||||||||
|
||||||||||||||
@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); | ||||||||||||||
} | ||||||||||||||
} |
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 { | ||
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. ๐ |
||
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); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
package com.racing.utils; | ||
|
||
public interface RandomMover { | ||
public interface RandomStrategy { | ||
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. RandomStrategy -> MovementStrategy ๋ก ๋ณ๊ฒฝํ์ต๋๋ค! |
||
public boolean shouldMove(); | ||
} |
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.
9a3199f
๋ฉ์ธ ์ง์ ์ ์ ๋ถ๋ฆฌํ์ต๋๋ค!