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

Open
wants to merge 9 commits into
base: baeksangha
Choose a base branch
from
Open
Changes from all commits
Commits
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
40 changes: 0 additions & 40 deletions src/main/java/edu/nextstep/camp/carracing/Car.java

This file was deleted.

53 changes: 0 additions & 53 deletions src/main/java/edu/nextstep/camp/carracing/CarRacing.java

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());
}
}
18 changes: 0 additions & 18 deletions src/main/java/edu/nextstep/camp/carracing/CarValidator.java

This file was deleted.

This file was deleted.

39 changes: 0 additions & 39 deletions src/main/java/edu/nextstep/camp/carracing/Winners.java

This file was deleted.

39 changes: 39 additions & 0 deletions src/main/java/edu/nextstep/camp/carracing/domain/Car.java
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
@@ -1,22 +1,35 @@
package edu.nextstep.camp.carracing;
package edu.nextstep.camp.carracing.domain;

public class CarName {
private static final int MAX_LENGTH = 5;

private final String name;

public CarName(String name) {
validateCarName(name);
this.name = name;
}

private void validateCarName(String name) {
if (name == null || name.isBlank()) {
throw new IllegalArgumentException("์ž๋™์ฐจ ์ด๋ฆ„์€ null์ด๊ฑฐ๋‚˜ ๋นˆ ๋ฌธ์ž์—ด์ผ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.");
}
if (name.length() > MAX_LENGTH) {
throw new IllegalArgumentException("์ž๋™์ฐจ ์ด๋ฆ„์€ 5์ž๋ฅผ ์ดˆ๊ณผํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.");
}
this.name = name.trim();
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof CarName)) {
return false;
}
CarName other = (CarName) o;
return name.equals(other.name);
}

@Override
public int hashCode() {
return name.hashCode();
}
Comment on lines +18 to 33
Copy link
Member

Choose a reason for hiding this comment

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

๋ถˆ๋ณ€ ์ฒ˜๋ฆฌ ์ข‹๋„ค์š”! ๐Ÿ‘


public String getName() {
49 changes: 49 additions & 0 deletions src/main/java/edu/nextstep/camp/carracing/domain/Cars.java
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
Copy link
Member

Choose a reason for hiding this comment

The 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;
}
}
51 changes: 51 additions & 0 deletions src/main/java/edu/nextstep/camp/carracing/domain/Position.java
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 {
Copy link
Member

Choose a reason for hiding this comment

The 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

๊ฐ’์€ ๊ฐ™์•„๋ณด์ด๋Š”๋ฐ ์™œ ๋‹ค๋ฅด๋‹ค๊ณ  ๋‚˜์˜ฌ๊นŒ์š”?
๋™๋“ฑ์„ฑ์„ ๋ณด์žฅ์‹œ์ผœ ๋ณด๋ฉด ์ข‹๊ฒ ์Šต๋‹ˆ๋‹ค!

Copy link
Member

Choose a reason for hiding this comment

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

๋ชจ๋“  ๋กœ์ง์— ๋‹จ์œ„ ํ…Œ์ŠคํŠธ๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค

์œ„ ์ฝ”๋ฉ˜ํŠธ ์ฐธ๊ณ ํ•ด์„œ ํ…Œ์ŠคํŠธ๋„ ๊ฐ™์ด ์ž‘์„ฑํ•ด ๋ณด๋ฉด ์ข‹๊ฒ ์Šต๋‹ˆ๋‹ค!

Copy link
Author

Choose a reason for hiding this comment

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

bb17bfe
๋™๋“ฑ์„ฑ ๋ณด์žฅ์„ ์œ„ํ•ด equals์™€ hashcode ๋ฉ”์„œ๋“œ ์˜ค๋ฒ„๋ผ์ด๋”ฉํ•ด์ฃผ๊ณ , ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋„ ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!

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;
}
}
Loading