Skip to content

Commit 2e97edb

Browse files
authored
고생하셨습니다.
🎉 PR 머지 완료! 🎉
1 parent 3a8cf14 commit 2e97edb

File tree

10 files changed

+398
-0
lines changed

10 files changed

+398
-0
lines changed

src/main/java/Car.java

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import java.util.Random;
2+
3+
public class Car {
4+
private String name;
5+
6+
public Car(String name){
7+
if (name.isEmpty()){
8+
throw new IllegalArgumentException();
9+
}
10+
11+
this.name = name;
12+
}
13+
14+
public String getName(){
15+
return name;
16+
}
17+
18+
public int move(){
19+
Random ran = new Random();
20+
if (ran.nextInt(10) >= 4){
21+
return 1; // 한 칸 이동 (전진)
22+
}
23+
24+
return 0;
25+
}
26+
27+
}

src/main/java/FindWinCar.java

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import java.util.Objects;
2+
import java.util.stream.IntStream;
3+
import java.util.Arrays;
4+
5+
public class FindWinCar {
6+
private Car[] cars;
7+
private int[] locate;
8+
9+
public FindWinCar(int n, Car[] car){
10+
if (n <= 0){
11+
throw new IllegalArgumentException();
12+
}
13+
14+
this.cars = Objects.requireNonNull(car);
15+
this.locate = new int[n];
16+
}
17+
18+
public void allMove(int times){
19+
if (times < 0){
20+
throw new IllegalArgumentException();
21+
}
22+
23+
for (int t = 0; t < times; ++t){
24+
move();
25+
}
26+
}
27+
28+
public void move(){
29+
for (int r = 0; r < cars.length; ++r){
30+
locate[r] += cars[r].move();
31+
}
32+
}
33+
34+
public Car[] findWin(){
35+
int max = Arrays.stream(locate)
36+
.max()
37+
.getAsInt();
38+
39+
return IntStream.range(0, cars.length)
40+
.filter(i -> locate[i] == max)
41+
.mapToObj(i -> cars[i])
42+
.toArray(Car[]::new);
43+
}
44+
45+
public int[] getLocate(){
46+
return locate;
47+
}
48+
49+
public Car[] getCars(){
50+
return cars;
51+
}
52+
53+
}

src/main/java/GameStart.java

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import java.util.Arrays;
2+
import java.util.Scanner;
3+
4+
public class GameStart {
5+
private static Car[] cars;
6+
private static int times;
7+
private static FindWinCar race;
8+
9+
public static void main(String[] args) {
10+
carNameInput();
11+
timesInput();
12+
13+
// 경기 진행
14+
race = new FindWinCar(cars.length, cars);
15+
for (int i = 0; i < times; ++i){
16+
game();
17+
}
18+
19+
// 최종 출력
20+
print(race.getLocate());
21+
String winNames = Arrays.stream(race.findWin())
22+
.map(Car::getName)
23+
.reduce((a, b) -> a + ", " + b)
24+
.orElse("");
25+
System.out.println(winNames + "가 최종 우승했습니다.");
26+
}
27+
28+
private static void carNameInput(){
29+
Scanner in = new Scanner(System.in);
30+
31+
// 자동차 이름
32+
System.out.println("경주할 자동차 이름을 입력하세요(이름은 쉼표(,)를 기준으로 구분).");
33+
String[] carNames = in.nextLine().split(",");
34+
35+
for (String name : carNames){
36+
if (name.length() > 5 || name.isEmpty()){
37+
throw new IllegalArgumentException();
38+
}
39+
}
40+
41+
// 객체 생성
42+
cars = Arrays.stream(carNames)
43+
.map(Car::new)
44+
.toArray(Car[]::new);
45+
46+
in.close();
47+
}
48+
49+
private static void timesInput(){
50+
Scanner in = new Scanner(System.in);
51+
52+
// 이동 횟수
53+
System.out.println("시도할 회수는 몇회인가요?");
54+
times = in.nextInt();
55+
if (times < 0){
56+
throw new IllegalArgumentException();
57+
}
58+
59+
in.close();
60+
}
61+
62+
private static void game(){
63+
print(race.getLocate());
64+
race.move();
65+
System.out.println();
66+
}
67+
68+
private static void print(int[] locate){
69+
for (int i = 0; i < cars.length; ++i){
70+
System.out.println(cars[i].getName() + " : " + locate[i]);
71+
}
72+
}
73+
74+
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package controller;
2+
3+
import domain.RacingGame;
4+
import view.InputView;
5+
import view.ResultView;
6+
7+
public class Application {
8+
public static void main(final String... args) {
9+
final String[] carNames = InputView.getCarNames();
10+
final int tryCount = InputView.getTryCount();
11+
12+
final var racingGame = new RacingGame(carNames, tryCount);
13+
racingGame.race();
14+
15+
ResultView.printWinners(racingGame.getWinners());
16+
}
17+
}

src/main/java/domain/RacingGame.java

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package domain;
2+
3+
import java.util.Arrays;
4+
import java.util.Objects;
5+
import java.util.stream.IntStream;
6+
7+
import objectTmp.Car;
8+
9+
public class RacingGame {
10+
private static Car[] cars;
11+
private static int times;
12+
private static int[] locate;
13+
14+
public RacingGame(String[] carNames, int times){
15+
if (times <= 0){
16+
throw new IllegalArgumentException();
17+
}
18+
19+
for (String name : carNames){
20+
if (name.length() > 5 || name.isEmpty()){
21+
throw new IllegalArgumentException();
22+
}
23+
}
24+
25+
cars = Arrays.stream(carNames)
26+
.map(Car::new)
27+
.toArray(Car[]::new);
28+
29+
RacingGame.times = times;
30+
}
31+
32+
public static void race(){
33+
for (int i = 0; i < times; ++i){
34+
print(locate);
35+
for (int r = 0; r < cars.length; ++r){
36+
locate[r] += cars[r].move();
37+
}
38+
System.out.println();
39+
}
40+
}
41+
42+
public static void print(int[] locate){
43+
for (int i = 0; i < cars.length; ++i){
44+
System.out.println(cars[i].getName() + " : " + locate[i]);
45+
}
46+
}
47+
48+
public static Car[] getWinners(){
49+
int max = Arrays.stream(locate)
50+
.max()
51+
.getAsInt();
52+
53+
return IntStream.range(0, cars.length)
54+
.filter(i -> locate[i] == max)
55+
.mapToObj(i -> cars[i])
56+
.toArray(Car[]::new);
57+
}
58+
59+
public static int[] getLocate(){
60+
return locate;
61+
}
62+
63+
}

src/main/java/objectTmp/Car.java

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package objectTmp;
2+
3+
import java.util.Random;
4+
5+
public class Car {
6+
private String name;
7+
8+
public Car(String name){
9+
if (name.isEmpty()){
10+
throw new IllegalArgumentException();
11+
}
12+
13+
this.name = name;
14+
}
15+
16+
public String getName(){
17+
return name;
18+
}
19+
20+
public int move(){
21+
Random ran = new Random();
22+
if (ran.nextInt(10) >= 4){
23+
return 1; // 한 칸 이동 (전진)
24+
}
25+
26+
return 0;
27+
}
28+
29+
}

src/main/java/view/InputView.java

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package view;
2+
3+
import java.util.Scanner;
4+
5+
public class InputView {
6+
static Scanner in = new Scanner(System.in);
7+
8+
public static String[] getCarNames(){
9+
System.out.println("경주할 자동차 이름을 입력하세요(이름은 쉼표(,)를 기준으로 구분).");
10+
String[] carNames = in.nextLine().split(",");
11+
12+
for (String name : carNames){
13+
if (name.length() > 5 || name.isEmpty()){
14+
throw new IllegalArgumentException();
15+
}
16+
}
17+
18+
return carNames;
19+
}
20+
21+
public static int getTryCount(){
22+
System.out.println("시도할 회수는 몇회인가요?");
23+
int times = in.nextInt();
24+
if (times < 0){
25+
throw new IllegalArgumentException();
26+
}
27+
28+
return times;
29+
}
30+
}

src/main/java/view/ResultView.java

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package view;
2+
3+
import java.util.Arrays;
4+
5+
import domain.RacingGame;
6+
import objectTmp.Car;
7+
8+
public class ResultView {
9+
public static void printWinners(Car[] winner){
10+
RacingGame.print(RacingGame.getLocate());
11+
String winNames = Arrays.stream(winner)
12+
.map(Car::getName)
13+
.reduce((a, b) -> a + ", " + b)
14+
.orElse("");
15+
System.out.println(winNames + "가 최종 우승했습니다.");
16+
}
17+
18+
}

src/test/java/CarTest.java

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import static org.junit.jupiter.api.Assertions.assertEquals;
2+
import static org.junit.jupiter.api.Assertions.assertThrows;
3+
import static org.junit.jupiter.api.Assertions.assertTrue;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
public class CarTest {
8+
@Test
9+
public void createTest(){
10+
Car c = new Car("mercedes");
11+
assertEquals("mercedes", c.getName());
12+
}
13+
14+
@Test
15+
public void errorTest(){
16+
assertThrows(NullPointerException.class, ()->new Car(null));
17+
assertThrows(IllegalArgumentException.class, ()->new Car(""));
18+
}
19+
20+
@Test
21+
public void moveTest(){
22+
Car c = new Car("a");
23+
24+
int tmp;
25+
for (int i = 0; i < 100; i++) {
26+
tmp = c.move();
27+
assertTrue(tmp == 1 || tmp == 0);
28+
}
29+
}
30+
31+
}

0 commit comments

Comments
 (0)