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

[step4] πŸš€ 4단계 - μžλ™μ°¨ κ²½μ£Ό(우승자) #5477

Open
wants to merge 2 commits into
base: shgpwn
Choose a base branch
from
Open
Show file tree
Hide file tree
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: 38 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# μžλ™μ°¨ κ²½μ£Ό κ²Œμž„
## 3단계 - μžλ™μ°¨ κ²½μ£Ό

## πŸš€ 3단계 - μžλ™μ°¨ κ²½μ£Ό
### κ΅¬ν˜„ν•  κΈ°λŠ₯ λͺ©λ‘
#### View
###### InputView
Expand Down Expand Up @@ -27,4 +28,39 @@
* μžλ™μ°¨ κ²½μ£Ό κ²Œμž„ κ²°κ³Ό 좜λ ₯
#### Test
* λ‹¨μœ„ ν…ŒμŠ€νŠΈ
* 4이상일 κ²½μš°μ—λ§Œ 전진, 아닐 경우 멈좀
* 4이상일 κ²½μš°μ—λ§Œ 전진, 아닐 경우 멈좀

## πŸš€ 4단계 - μžλ™μ°¨ κ²½μ£Ό(우승자)
### κ΅¬ν˜„ν•  κΈ°λŠ₯ λͺ©λ‘
#### View
###### InputView
* μžλ™μ°¨ κ²½μ£Ό κ²Œμž„ μ‹œμž‘μ„ μœ„ν•œ 데이터 μž…λ ₯
* μžλ™μ°¨ 이름
* μ‹œλ„ν•  횟수 m회
###### ResultView
* μžλ™μ°¨ κ²½μ£Ό κ²Œμž„ κ²°κ³Ό 좜λ ₯
* 각 νšŒμ°¨λ™μ•ˆμ˜ μžλ™μ°¨μ˜ μƒνƒœ 좜λ ₯
* 각 회차의 λ§ˆμ§€λ§‰ μ‹œμ μ— 좜λ ₯
* μžλ™μ°¨ κ²½μ£Ό κ²Œμž„μ„ μ™„λ£Œν•œ ν›„ 우승자 좜λ ₯
* μš°μŠΉμžλŠ” ν•œ λͺ… 이상
#### Model
###### Car
* 이름
* ν˜„μž¬ μœ„μΉ˜
* 이번 λΌμš΄λ“œ 진행
* 전진 or 멈좀
* 이번 λΌμš΄λ“œ κ²°κ³Ό λ„μΆœ
* 이번 λΌμš΄λ“œμ˜ κ²°κ³Όλ₯Ό String κ°’μœΌλ‘œ λ°˜ν™˜
* 우승자 νŒλ³„μ„ μœ„ν•œ μ΅œμ’… μœ„μΉ˜ 비ꡐ
#### Controller
###### RacingCarController
* μžλ™μ°¨ κ²½μ£Ό κ²Œμž„ μ‹œμž‘μ„ μœ„ν•œ 데이터 μž…λ ₯κ°’ λ°›μŒ
* μžλ™μ°¨ κ²½μ£Ό κ²Œμž„ μ‹œμž‘μ„ μœ„ν•œ 데이터 μ„ΈνŒ…
* μžλ™μ°¨ κ²½μ£Ό μ‹œμž‘
* μžλ™μ°¨ κ²½μ£Ό κ²Œμž„ κ²°κ³Ό 좜λ ₯
* 각 회차 결과
* κ²½μ£Ό κ²Œμž„ μ’…λ£Œ ν›„ 우승자
#### Test
* λ‹¨μœ„ ν…ŒμŠ€νŠΈ
* 4이상일 κ²½μš°μ—λ§Œ 전진, 아닐 경우 멈좀
* μžλ™μ°¨ 이름 μ‰Όν‘œ(,)둜 뢄리
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package RacingCarWinner.Controller;

import RacingCarWinner.Model.Car;
import RacingCarWinner.View.InputView;
import RacingCarWinner.View.ResultView;

import java.util.*;

public class RacingCarWinnerController {
private static final String REGEX = ",";
InputView inputView;
ResultView resultView;

List<Car> carList;
String[] carNameList;
int roundCount;
Random random;
Comment on lines +11 to +17

Choose a reason for hiding this comment

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

  1. μ ‘κ·Ό μ œμ–΄μž λˆ„λ½λ˜λ©΄ μ•ˆλ©λ‹ˆλ‹€. κΌ­ μ±™κ²¨μ£Όμ„Έμš”
  2. μ§€κΈˆ μ»¨νŠΈλ‘€λŸ¬μ—μ„œ λ‹€ κ΄€λ¦¬ν•˜κ³  μžˆλŠ” μš” λ³€μˆ˜λ“€μ„ 챙겨쀄 RacingGame μ΄λΌλŠ” 클래슀λ₯Ό μ„€κ³„ν•˜κ³  μž‘μ„±ν•΄λ³΄μ„Έμš”. 이 클래슀의 κΈ°λŠ₯은 λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.
  • Inputview λ‘œλΆ€ν„° ν”Œλ ˆμ΄μ–΄ 이름 μž…λ ₯λ°›μŒ
  • λͺ‡ 번 λŒλ¦΄κ±΄μ§€ 회차 μž…λ ₯λ°›μŒ
  • μ‹€μ œ κ²Œμž„ μˆ˜ν–‰
  • νšŒμ°¨λ³„ κ²°κ³Ό 관리


public RacingCarWinnerController() {
inputView = new InputView();
resultView = new ResultView();
random = new Random();
roundCount = 0;
}

public void run() {
startGame();
setCarList();
sayGameStart();
playGame();
sayGameWinner();
}

private void startGame() {
String inputStringData = inputView.getStringData("κ²½μ£Όν•  μžλ™μ°¨ 이름을 μž…λ ₯ν•˜μ„Έμš”(이름은 μ‰Όν‘œ(,)λ₯Ό κΈ°μ€€μœΌλ‘œ ꡬ뢄).");
carNameList = inputView.getSplitStringData(inputStringData, REGEX);
roundCount = inputView.getIntData("μ‹œλ„ν•  νšŒμˆ˜λŠ” λͺ‡ 회 μΈκ°€μš”?");
}

private void setCarList() {
carList = new ArrayList<>();
for (String carName : carNameList) {
carList.add(new Car(carName));
}
}

private void sayGameStart() {
resultView.printString("μ‹€ν–‰ κ²°κ³Ό");
}

private void playGame() {
for (int round = 0; round <= roundCount; round++) {
playRound();
String resultRound = resultRound();
printResult(resultRound);
}
}

private void playRound() {
for (Car car : carList) {
int boosterValue = random.nextInt(10);
car.moveCar(boosterValue);
}
}

private String resultRound() {
String resultRound = "";
for (Car car : carList) {
String stringCurrentPosition = car.makeStringCurrentPosition();
resultRound += stringCurrentPosition + "\n";
}
Comment on lines +68 to +71

Choose a reason for hiding this comment

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

stream API 둜 κ°œμ„ ν•  수 μžˆμŠ΅λ‹ˆλ‹€. κ°œμ„  μ‹œλ„ν•΄λ³΄μ„Έμš”.

return resultRound;
}

private void printResult(String result) {
resultView.printString(result);
}

private void sayGameWinner() {
String winners = getWinners(getFastCar());
String resultGame = makeStringResultGame(winners);
printResult(resultGame);
}

private Car getFastCar() {
if (carList.size() == 0) {
return null;
}
Collections.sort(carList);

return carList.get(0);
}

private String getWinners(Car max) {
StringBuilder winnerNames = new StringBuilder(max.getName());

long winnerCount = carList.stream().filter(car -> car.getCurrentPosition() == max.getCurrentPosition()).count();
for (int index = 1; index < winnerCount; index++) {
winnerNames.append(REGEX).append(carList.get(index).getName());
}
return winnerNames.toString();
}

private String makeStringResultGame(String winners) {
if (winners==null) {
return "경주에 μ°Έμ—¬ν•œ μžλ™μ°¨κ°€ μ—†μŠ΅λ‹ˆλ‹€.";
}
return winners + "κ°€ μ΅œμ’… μš°μŠΉν–ˆμŠ΅λ‹ˆλ‹€.";
}
}
9 changes: 9 additions & 0 deletions src/main/java/RacingCarWinner/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package RacingCarWinner;

import RacingCarWinner.Controller.RacingCarWinnerController;

public class Main {
public static void main(String[] args) {
new RacingCarWinnerController().run();
}
}
38 changes: 38 additions & 0 deletions src/main/java/RacingCarWinner/Model/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package RacingCarWinner.Model;

public class Car implements Comparable<Car> {
private static final String HYPHEN = "-";
private static final String SPACE = " ";
private static final String COLON = ":";
int currentPosition;

Choose a reason for hiding this comment

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

μš”κΈ°μ—λ„ μ ‘κ·Ό μ œμ–΄μžκ°€ λΆ™μ–΄μ•Ό ν•  것 κ°™μŠ΅λ‹ˆλ‹€. ν•˜λ‚˜ λΆ™μ—¬μ£Όμ‹œμ£ !


String name;

public Car(String name) {
currentPosition = 0;
this.name = name;
}

public int getCurrentPosition() {
return currentPosition;
}

public String getName() {
return name;
}

public void moveCar(int boosterValue) {
if (boosterValue >= 4) {
currentPosition++;
}
}

public String makeStringCurrentPosition() {
return name + SPACE +COLON + SPACE + HYPHEN.repeat(currentPosition);
}

@Override
public int compareTo(Car other) {
return other.getCurrentPosition() - this.getCurrentPosition();
}
}
21 changes: 21 additions & 0 deletions src/main/java/RacingCarWinner/View/InputView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package RacingCarWinner.View;

import java.util.Scanner;

public class InputView {
private Scanner scanner = new Scanner(System.in);

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.

@wheejuni λ¦¬λ·°μ–΄λ‹˜ μ™ΈλΆ€μ—μ„œ μ£Όμž…λ°›λŠ” 방식이 무슨 μ˜λ―ΈμΌκΉŒμš”??
InputViewμ—μ„œλŠ” μž…λ ₯κ°’λ§Œ λ°›κ³ , split 같은 μž…λ ₯ 값에 λŒ€ν•œ λ‘œμ§μ€ μ™ΈλΆμ—μ„œ μ²˜λ¦¬ν•˜λŠ” 방식을 λ§μ”€ν•˜μ‹  κ±ΈκΉŒμš”??
μ œκ°€ μ΄ν•΄ν•œ 게 λ§žλŠ”μ§€ κΆκΈˆν•˜μ—¬ 확인차 μ—¬μ­ˆμ–΄ λ΄…λ‹ˆλ‹€^^


public int getIntData(String question) {
System.out.println(question);
return scanner.nextInt();
}

public String getStringData(String question) {
System.out.println(question);
return scanner.nextLine();
}

public String[] getSplitStringData(String inputStringData, String regex) {
return inputStringData.split(regex);
}
}
8 changes: 8 additions & 0 deletions src/main/java/RacingCarWinner/View/ResultView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package RacingCarWinner.View;

public class ResultView {
public void printString(String str) {
System.out.println(str);
}
}

30 changes: 30 additions & 0 deletions src/test/java/RacingCarWinner/RacingCarWinnerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package RacingCarWinner;

import RacingCarWinner.Model.Car;
import RacingCarWinner.View.InputView;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

public class RacingCarWinnerTest {

Choose a reason for hiding this comment

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

ν…ŒμŠ€νŠΈμ˜ ꡬ성에 μžˆμ–΄μ„œ λͺ‡ 가지 ν΄λž˜μŠ€λ“€μ— λŒ€ν•œ ν…ŒμŠ€νŠΈκ°€ ν•œ ν…ŒμŠ€νŠΈ 클래슀 파일둜 합쳐져 μžˆλŠ”λ°μš”, μš”κ±΄ 일단...

  1. ν…ŒμŠ€νŠΈ λ¨Όμ € μž‘μ„±ν•˜μ‹œκ³  κ΅¬ν˜„ν•˜λŠ” 흐름을 κ°€μ Έκ°€κ³  κ³„μ‹œμ§€ μ•Šλ‹€λŠ” κ±Έ μ•Œ 수 μžˆμŠ΅λ‹ˆλ‹€.
  2. 이 자체둜 μ½”λ“œ μŠ€λ©œμž…λ‹ˆλ‹€. ν…ŒμŠ€νŠΈλŠ” λ©”μ†Œλ“œ λ‹¨μœ„ 싀행이 μ•„λ‹Œ 클래슀 λ‹¨μœ„ 싀행이면 μ’‹κ² κ³ , 각 ν΄λž˜μŠ€κ°€ μ˜λ―Έλ‹¨μœ„λ₯Ό κ°€μ‘ŒμœΌλ©΄ ν•©λ‹ˆλ‹€.

@ParameterizedTest
@DisplayName("random 값이 4이상일 κ²½μš°μ—λ§Œ 전진")
@CsvSource(value = {"0,false", "1,false", "2,false", "3,false", "4,true", "5,true", "6,true"})
void carMoveTest(int num, boolean isGo) {
Car car = new Car("sample");
car.moveCar(num);
String stringCurrentState = car.makeStringCurrentPosition();
assertThat(stringCurrentState.contains("-")).isEqualTo(isGo);
}

@Test
@DisplayName("split κ²°κ³Όκ°€ 크기가 1인 배열인 경우 λ°˜ν™˜ 확인")
void splitSingle() {
InputView inputView = new InputView();
String[] strList = inputView.getSplitStringData("pobi,crong,honux", ",");
assertThat(strList).containsExactly("pobi", "crong", "honux");
}
}