-
Notifications
You must be signed in to change notification settings - Fork 830
Expand file tree
/
Copy pathView.java
More file actions
129 lines (112 loc) · 3.55 KB
/
Copy pathView.java
File metadata and controls
129 lines (112 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package racingcar;
import camp.nextstep.edu.missionutils.Console;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class View {
/*
클래스 변수, 상수
인스턴스 변수
생성자
*/
static final int MAX_NAME = 5;
List<String> carNames = new ArrayList<String>();
List<Car> cars = new ArrayList<Car>();
Game game = new Game();
public void game(){
int count;
String input;
input = inputCars();
carNames =inputCarNames(input); //이름 리스트 생성
count = inputCount();
cars = addCars(carNames);
play(count,cars);
System.out.println( game.winners(cars));
}
/*
실행 결과
wo: -
po:
메소드 구분
*/
public void play(int count,List<Car> cars){
System.out.println("실행결과");
for(int i =0; i<count ; i++){
//여기부터
for(Car car : cars) {
printGame(car);
}
System.out.println();
//여기 구분
if(i == count){
game.winners(cars);
}
}
}
private void printGame(Car car){
car.move();
car.getPosition();
System.out.print(car.getName() + " : ");
for(int i = 0; i< car.getPosition(); i++){
System.out.print("-");
}
System.out.println();
}
public int inputCount(){
System.out.println("시도할 횟수는 몇 회 인가요");
String input = Console.readLine();
return isVaildNum(input);
}
public String inputCars() {
System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)");
return Console.readLine().trim();
}
public List<String> inputCarNames(String input) {
String[] names = input.split(",");
for (int i = 0; i < names.length; i++) {
names[i] = names[i].trim();
isVaildName(names[i]);
}
return Arrays.asList(names);
}
public List<Car> addCars(List<String> carNames){
for(String carName : carNames){
Car car = new Car(carName);
cars.add(car);
}
return cars;
}
private void isVaildNameMax(String name) {
if (name.length() > MAX_NAME || name.isEmpty()) {
throw new IllegalArgumentException("이름은 1글자 이상 5글자 이하로 입력해주세요.");
}
}
private void isVaildInputEmpty(String input) {
if (input.isEmpty()) {
throw new IllegalArgumentException("입력이 비었습니다.");
}
}
public void isVaildName(String name) {
if (name.length() > MAX_NAME || name.isEmpty()) {
throw new IllegalArgumentException("이름은 1글자 이상 5글자 이하로 입력해주세요.");
}
if (!name.matches("[0-9|a-z|A-Z|ㄱ-ㅎ|ㅏ-ㅣ|가-힝]*")) {
throw new IllegalArgumentException("이름에 특수문자가 있습니다.");
}
}
public int isVaildNum(String input) {
try {
isVaildInputEmpty(input);
int count = Integer.parseInt(input);
isVaildPositive(count);
return count;
} catch (NumberFormatException e) {
throw new IllegalArgumentException("숫자가 아닙니다.");
}
}
private void isVaildPositive(int count) {
if (count < 0) {
throw new IllegalArgumentException("0보다 큰 수를 입력해야합니다.");
}
}
}