-
Notifications
You must be signed in to change notification settings - Fork 2
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
[이영진] 자동차게임 재구현 #9
base: youngreal
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
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package game; | ||
|
||
import java.util.regex.Pattern; | ||
|
||
public class Car { | ||
private static final Pattern CAR_NAME_PATTERN = Pattern.compile("^[a-zA-Z]*$"); | ||
private static final int CAR_MAX_LENGTH = 5; | ||
private final String carName; | ||
private static final int ADVANCE_RANDOM_NUMBER = 4; | ||
private final StringBuffer stringLocation = new StringBuffer("-"); | ||
|
||
public Car(String carName) { | ||
if (carName.length() > CAR_MAX_LENGTH) { | ||
throw new IllegalArgumentException("자동차 이름의 길이는 5자리를 넘을수 없다"); | ||
} | ||
|
||
if (isNotMatchCarNamePattern(carName) || carName.isBlank()) { | ||
throw new IllegalArgumentException("자동차 이름은 영문만 올수있습니다"); | ||
} | ||
this.carName = carName; | ||
} | ||
|
||
public StringBuffer advanceCarLocation(int randomNum) { | ||
if (randomNum >= ADVANCE_RANDOM_NUMBER) { | ||
return this.stringLocation.append("-"); | ||
} | ||
return this.stringLocation; | ||
} | ||
|
||
public String getCarName() { | ||
return carName; | ||
} | ||
|
||
private boolean isNotMatchCarNamePattern(String carName) { | ||
return !CAR_NAME_PATTERN.matcher(carName).matches(); | ||
} | ||
|
||
public StringBuffer getStringLocation() { | ||
return stringLocation; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package game; | ||
|
||
import game.storage.CarStorage; | ||
import game.util.Util; | ||
|
||
import java.util.Scanner; | ||
|
||
public class Game { | ||
private final Scanner sc = new Scanner(System.in); | ||
private CarStorage carStorage; | ||
|
||
public void gameStart() { | ||
System.out.println("경주할 자동차 이름을 입력하세요(이름은 쉼표를 기준으로 구분)"); | ||
String[] splitCarNames = sc.nextLine().split(","); | ||
|
||
System.out.println("시도할 회수는 몇회인가요?"); | ||
int gameCount = sc.nextInt(); | ||
|
||
System.out.println("실행 결과"); | ||
Util util = new Util(splitCarNames); | ||
ResultView resultView = new ResultView(splitCarNames,gameCount); | ||
|
||
while (gameCount > 0) { | ||
carStorage = util.addCarsToCarStorage(); | ||
resultView.print(gameCount, carStorage); | ||
System.out.println(""); | ||
gameCount--; | ||
} | ||
resultView.findWinner(splitCarNames,carStorage); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package game; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
Game game = new Game(); | ||
game.gameStart(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package game; | ||
|
||
import game.storage.CarStorage; | ||
|
||
public class ResultView { | ||
private int max =0; | ||
private final int initialGameCountValue; | ||
private final StringBuffer answer = new StringBuffer(""); | ||
private final String[] split; | ||
|
||
public ResultView(String[] split, int initialGameCountValue) { | ||
this.split = split; | ||
this.initialGameCountValue = initialGameCountValue; | ||
} | ||
|
||
public void print(int gameCount, CarStorage carStorage) { | ||
for (int i = 0; i < split.length; i++) { | ||
if (initialGameCountValue == gameCount) { | ||
System.out.println(carStorage.getCarName(i) + " : " + carStorage.getNotAdvanceCar(i)); | ||
continue; | ||
} | ||
System.out.println(carStorage.getCarName(i) + " : " + carStorage.getAdvanceCar(i)); | ||
calculateMaxValue(carStorage, i); | ||
} | ||
} | ||
|
||
public void findWinner(String[] split, CarStorage carStorage) { | ||
for (int i = 0; i < split.length; i++) { | ||
if (isWinnerLength(carStorage, i)) { | ||
answer.append(carStorage.getCarName(i)); | ||
} | ||
} | ||
printWinner(); | ||
} | ||
|
||
private void calculateMaxValue(CarStorage carStorage, int i) { | ||
if (isOverMaxLocation(carStorage, i)) { | ||
max= carStorage.getNotAdvanceCar(i).length(); | ||
} | ||
} | ||
|
||
private boolean isOverMaxLocation(CarStorage carStorage, int i) { | ||
return carStorage.getNotAdvanceCar(i).length() > max; | ||
} | ||
|
||
private void printWinner() { | ||
for (int i = 0; i < answer.length(); i++) { | ||
System.out.print(answer.charAt(i)); | ||
if (isNotFinalSequence(i)) { | ||
System.out.print(","); | ||
} | ||
} | ||
Comment on lines
+47
to
+52
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.
|
||
System.out.print("가 최종 우승했습니다"); | ||
} | ||
|
||
private boolean isNotFinalSequence(int i) { | ||
return i != answer.length() - 1; | ||
} | ||
|
||
private boolean isWinnerLength(CarStorage carStorage, int i) { | ||
return carStorage.getNotAdvanceCar(i).length() == max; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package game.storage; | ||
|
||
import game.Car; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import static game.util.Util.generateRandomNumber; | ||
|
||
public class CarStorage { | ||
private final List<Car> cars; | ||
|
||
public CarStorage(List<Car> cars) { | ||
this.cars = cars; | ||
} | ||
|
||
public List<Car> getCars() { | ||
return Collections.unmodifiableList(cars); | ||
} | ||
|
||
public String getCarName(int number) { | ||
return getCars().get(number).getCarName(); | ||
} | ||
|
||
public StringBuffer getAdvanceCar(int number) { | ||
return getCars().get(number).advanceCarLocation(generateRandomNumber()); | ||
} | ||
|
||
public StringBuffer getNotAdvanceCar(int number) { | ||
return getCars().get(number).getStringLocation(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package game.util; | ||
|
||
import game.Car; | ||
import game.storage.CarStorage; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Util { | ||
private final String[] split; | ||
private final List<Car> cars = new ArrayList<>(); | ||
|
||
public Util(String[] split) { | ||
this.split = split; | ||
} | ||
|
||
public CarStorage addCarsToCarStorage() { | ||
for (String s : this.split) { | ||
Car car = new Car(s); | ||
cars.add(car); | ||
} | ||
return new CarStorage(cars); | ||
} | ||
|
||
public static int generateRandomNumber() { | ||
return (int) (Math.random() * 10); | ||
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. 요구사항에는 |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import game.Car; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
class CarLocationTest { | ||
Car car = new Car("pobi"); | ||
|
||
@Test | ||
@DisplayName("랜덤값이 4이상일때 -가 하나 추가된다 : --출력") | ||
void location_MoreThan_2_Then_AddStringBuffer() { | ||
assertEquals("--",car.advanceCarLocation(4).toString() ); | ||
} | ||
|
||
@Test | ||
@DisplayName("랜덤값이 4미만일때는 초기값(-) 그대로를 출력한다 : -출력") | ||
void location_MoreLess_2_Then_InitStringBuffer() { | ||
assertEquals("-", car.advanceCarLocation(3).toString()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import game.Car; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class CarTest { | ||
|
||
@Test | ||
@DisplayName("자동차 이름은 5자를 넘을수 없음") | ||
void carNameLength_IsExceed_Then_Exception() { | ||
assertThrows(IllegalArgumentException.class, () -> { | ||
inputCarName("aaaaaaaa"); | ||
}); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = {" ","","a, b, c"}) | ||
@DisplayName("자동차 이름은 공백이 포함될수 없음") | ||
void carName_isEmptyOrNull_Then_Exception(String carName) { | ||
assertThrows(IllegalArgumentException.class, () -> { | ||
inputCarName(carName); | ||
}); | ||
} | ||
|
||
@Test | ||
@DisplayName("자동차 이름엔 특수문자가 들어갈수 없음") | ||
void carName_Include_SpecialCharacters_Then_Exception() { | ||
assertThrows(IllegalArgumentException.class, () ->{ | ||
inputCarName("!@#$"); | ||
}); | ||
} | ||
|
||
private void inputCarName(String carName) { | ||
Car car = new Car(carName); | ||
} | ||
} |
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.
현재 구현체에서 carStorage.getNotAdvanceCar(i)의 length() 값을 가져오는 방식보다 carStorage 안에엇 특정한 자동차의 length를 제공하는 메서드를 만드는 방식이 더 좋아보입니다.☺️