-
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
Step4 리뷰 요청드립니다. #5528
base: zhenxikim
Are you sure you want to change the base?
Step4 리뷰 요청드립니다. #5528
Changes from all commits
217d833
32fcbe5
6308e65
8f7f303
2ccff9b
71bf0c9
0d9dc58
5136683
0347467
5021299
3bfa13f
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,2 +1,5 @@ | ||
- [X] 생선된 자동차 수는 1보다 크다. | ||
- [X] 전진하는 조건은 0에서 9 사이에서 random 값을 구한 후 random 값이 4이상일 경우이다. | ||
- [X] 자동차 이름은 5자를 초과할 수 없다. | ||
- [X] 우승자는 한명 이상일 수 있다. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import java.util.List; | ||
|
||
/** | ||
* @author jeongheekim | ||
* @date 3/27/24 | ||
*/ | ||
public class FormattingUtil { | ||
private FormattingUtil() {} | ||
|
||
public static String formattingResult(List<String> list) { | ||
StringBuilder sb = new StringBuilder(); | ||
int size = list.size(); | ||
for (int i = 0; i < size; i++) { | ||
sb.append(list.get(i)); | ||
addComma(i, size, sb); | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
private static void addComma(int i, int size, StringBuilder sb) { | ||
if (i < size - 1) { | ||
sb.append(","); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,51 +8,65 @@ | |
public class Game { | ||
|
||
public static final String DELIMETER = "-"; | ||
|
||
public static final String CAR_NAME_DELIMETER = ","; | ||
|
||
private int gameCount; | ||
|
||
private Car[] cars; | ||
|
||
public void start() { | ||
cars = createCar(); | ||
|
||
cars = createCar(); | ||
|
||
gameCount = getGameCount(); | ||
System.out.println("실행 결과"); | ||
for (int i = 0; i < gameCount; i++) { | ||
job(); | ||
} | ||
|
||
Winner winner = new Winner(); | ||
System.out.println(FormattingUtil.formattingResult(winner.getWinners(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. |
||
|
||
} | ||
|
||
private void job() { | ||
for (Car car : this.cars) { | ||
car.drive(car.getNumber(), DELIMETER); | ||
System.out.println(car.getStatus()); | ||
System.out.println(car.getName() + ":" + car.getStatus()); | ||
} | ||
System.out.println(" "); | ||
} | ||
|
||
private int getGameCount() { | ||
Scanner scanner = new Scanner(System.in); | ||
System.out.println("시도할 회수는 몇 회 인가요?"); | ||
|
||
int gameCount = scanner.nextInt(); | ||
System.out.println("gameCount = " + gameCount); | ||
System.out.println(gameCount); | ||
|
||
return gameCount; | ||
} | ||
|
||
|
||
private Car[] createCar() { | ||
int carCount = getCarCount(); | ||
Car[] cars = new Car[carCount]; | ||
for (int i = 0; i < carCount; i++) { | ||
cars[i] = new SmallCar(); | ||
String[] carNameInputArr = getCarNameInputArr(); | ||
int length = carNameInputArr.length; | ||
Car[] cars = new Car[length]; | ||
for (int i = 0; i < length; i++) { | ||
cars[i] = new SmallCar(carNameInputArr[i]); | ||
|
||
} | ||
return cars; | ||
} | ||
|
||
private int getCarCount() { | ||
System.out.println("자동차 대수는 몇 대 인가요?"); | ||
|
||
private String[] getCarNameInputArr() { | ||
System.out.println("경주할 자동차 이름을 입력하세요(이름은 " + CAR_NAME_DELIMETER + "를 기준으로 구분)."); | ||
Scanner scanner = new Scanner(System.in); | ||
int carCount = Integer.parseInt(scanner.nextLine()); | ||
System.out.println("자동차 대수 = " + carCount); | ||
return carCount; | ||
String carNameInput = scanner.nextLine(); | ||
System.out.println(carNameInput); | ||
return carNameInput.split(CAR_NAME_DELIMETER); | ||
} | ||
|
||
public Car[] getCars() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* @author jeongheekim | ||
* @date 3/26/24 | ||
*/ | ||
public class Winner { | ||
private List<String> winners = new ArrayList<>(); | ||
private int maxLength; | ||
|
||
public void addWinner(String name) { | ||
this.winners.add(name); | ||
} | ||
|
||
public void updateMaxLength(int length) { | ||
this.maxLength = length; | ||
} | ||
|
||
public List<String> getWinners(Car[] cars) { | ||
this.filterMaxLength(cars); | ||
for (Car car : cars) { | ||
this.checkWinnerCondition(car); | ||
} | ||
return this.winners; | ||
} | ||
Comment on lines
+20
to
+26
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 void filterMaxLength (Car[] cars) { | ||
for (Car car : cars) { | ||
int carStatusLength = car.getStatus().length(); | ||
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. 자동차의 길이를 꺼내어 처리하고 있네요! |
||
this.compareMaxLength(carStatusLength); | ||
} | ||
} | ||
|
||
private void compareMaxLength(int carStatusLength) { | ||
if (this.maxLength <= carStatusLength) { | ||
this.updateMaxLength(carStatusLength); | ||
} | ||
} | ||
|
||
private void checkWinnerCondition(Car car) { | ||
int statusLength = car.getStatus().length(); | ||
if (this.maxLength <= statusLength) { | ||
this.addWinner(car.getName()); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
/** | ||
* @author jeongheekim | ||
* @date 3/27/24 | ||
*/ | ||
class WinnerTest { | ||
|
||
@Test | ||
@DisplayName("우승자는 한명 이상이다.") | ||
void winnerLengthTest() { | ||
SmallCar car = new SmallCar("pobi"); | ||
car.drive(7, "-"); | ||
|
||
Car[] cars = new Car[1]; | ||
cars[0] = car; | ||
|
||
Winner winner = new Winner(); | ||
List<String> winners = winner.getWinners(cars); | ||
|
||
assertTrue(winners.size() >= 1); | ||
} | ||
|
||
} |
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.
👍