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 all 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
43 changes: 2 additions & 41 deletions src/main/java/com/racing/Main.java
Original file line number Diff line number Diff line change
@@ -1,48 +1,9 @@
package com.racing;

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

import java.util.ArrayList;
import java.util.List;
import com.racing.service.RacingService;

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

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

public static void main(String[] args) {
String carNames = InputView.getCarName();
int tryNumber = InputView.getTryNumber();

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

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

List<Car> winners = CarHelper.determineWinners(carList);
ResultView.printWinners(winners);
}

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

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

return carList;
}

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

private static void moveAllCars(List<Car> carList) {
for (Car car : carList) {
car.move();
}
RacingService.startRacingGame();
}
}
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);
}
}
71 changes: 71 additions & 0 deletions src/main/java/com/racing/domain/Cars.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.racing.domain;

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

public class Cars {
private final List<Car> items;

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

public Cars(List<Car> carList) {
this.items = carList;
}

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

public List<Car> getItems() {
return Collections.unmodifiableList(this.items);
}

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

return maxPosition;
}

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

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

return new Cars(winnerList);
}

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

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

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

@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(items, cars.items);
}

@Override
public int hashCode() {
return Objects.hashCode(items);
}
}
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);
}
}
55 changes: 55 additions & 0 deletions src/main/java/com/racing/service/RacingService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.racing.service;

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.CarMovement;

import java.util.List;

public class RacingService {
public static void startRacingGame() {
Copy link
Member

Choose a reason for hiding this comment

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

startRacingGame์—์„œ input๊นŒ์ง€ ๋‹ค ํ•˜๊ธฐ๋ณด๋‹ค๋Š” input์€ ๋ฏธ๋ฆฌ ๋ฐ›์€ ๋’ค dto ๋“ฑ์œผ๋กœ ๋„˜๊ธฐ๋ฉด ์กฐ๊ธˆ ๋” ๊น”๋”ํ•˜๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๋„ค์š”.

String carNames = InputView.getCarName();
int tryNumber = InputView.getTryNumber();

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

Cars cars = initializeRacingCars(validCarNames);

executeRaceRounds(cars, tryNumber);

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

private static Cars initializeRacingCars(List<String> carNames) {
Cars cars = new Cars();

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

return cars;
}

private static void executeRaceRounds(Cars cars, int tryNumber) {
ResultView.printStartMessage();
for (int i = 0; i < tryNumber; i++) {
advanceAllCars(cars);

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

private static void advanceAllCars(Cars cars) {
CarMovement carMovement = new CarMovement();

for (Car car : cars.getItems()) {
car.move(carMovement.shouldMove());
}
}
}
17 changes: 9 additions & 8 deletions src/main/java/com/racing/ui/ResultView.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
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());

for (Car car : cars.getItems()) {
System.out.println(car.displayRacingPosition());
}

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
Loading