-
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] π 4λ¨κ³ - μλμ°¨ κ²½μ£Ό(μ°μΉμ) #5477
base: shgpwn
Are you sure you want to change the base?
Changes from all commits
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 |
---|---|---|
@@ -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; | ||
|
||
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
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. 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 + "κ° μ΅μ’ μ°μΉνμ΅λλ€."; | ||
} | ||
} |
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(); | ||
} | ||
} |
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; | ||
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. μκΈ°μλ μ κ·Ό μ μ΄μκ° λΆμ΄μΌ ν κ² κ°μ΅λλ€. νλ λΆμ¬μ£Όμμ£ ! |
||
|
||
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(); | ||
} | ||
} |
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); | ||
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. @wheejuni 리뷰μ΄λ μΈλΆμμ μ£Όμ
λ°λ λ°©μμ΄ λ¬΄μ¨ μλ―ΈμΌκΉμ?? |
||
|
||
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); | ||
} | ||
} |
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); | ||
} | ||
} | ||
|
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 { | ||
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. ν μ€νΈμ ꡬμ±μ μμ΄μ λͺ κ°μ§ ν΄λμ€λ€μ λν ν μ€νΈκ° ν ν μ€νΈ ν΄λμ€ νμΌλ‘ ν©μ³μ Έ μλλ°μ, μ건 μΌλ¨...
|
||
@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"); | ||
} | ||
} |
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.
RacingGame
μ΄λΌλ ν΄λμ€λ₯Ό μ€κ³νκ³ μμ±ν΄λ³΄μΈμ. μ΄ ν΄λμ€μ κΈ°λ₯μ λ€μκ³Ό κ°μ΅λλ€.Inputview
λ‘λΆν° νλ μ΄μ΄ μ΄λ¦ μ λ ₯λ°μ