-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathOutputView.java
More file actions
68 lines (61 loc) · 2.4 KB
/
OutputView.java
File metadata and controls
68 lines (61 loc) · 2.4 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
package baseball.view;
import baseball.model.BaseballGame;
import baseball.model.BaseballNumber;
import baseball.model.GameStatus;
import baseball.util.Message;
import java.util.Scanner;
public class OutputView {
public void printStartMessage() {
System.out.println(Message.MESSAGE_START);
}
public void printResult(BaseballGame game) {
int strike = 0;
int ball = 0;
for (BaseballNumber targetNumber : game.getTargetNumbers()) {
if (targetNumber.isStrike()) {
strike++;
continue;
}
if (targetNumber.isBall()) {
ball++;
}
}
if (ball == 0 && strike == 0) { // todo: 연산을 view클래스에서?
game.setStatus(GameStatus.FAIL);
System.out.println(Message.MESSAGE_MISS);
} else if (ball == 0) {
game.setStatus(GameStatus.FAIL);
System.out.println(String.format(Message.MESSAGE_STRIKE_ONLY_FORMATTED, strike));
if (strike == game.getNumSize()) {
game.setStatus(GameStatus.SUCCESS);
System.out.println(String.format(Message.MESSAGE_SUCCESS_FORMATTED, BaseballGame.getNumSize()));
}
} else if (strike == 0) {
game.setStatus(GameStatus.FAIL);
System.out.println(String.format(Message.MESSAGE_BALL_ONLY_FORMATTED, ball));
} else {
game.setStatus(GameStatus.FAIL);
System.out.println(String.format(Message.MESSAGE_BALL_STRIKE_FORMATTED, ball, strike));
}
}
public boolean isRestart(BaseballGame game) { // todo: 출력뿐 아니라 조건 분기도 하고 있음..
System.out.println(Message.MESSAGE_RESTART_PROMPT);
Scanner scanner = new Scanner(System.in);
try {
int option = scanner.nextInt();
if (option == 1) {
game.createTargetNumber(1, 9);
game.setStatus(GameStatus.RESTART); // 의미가 있나?
return true;
} else if (option == 2) return false;
else {
// 1,2 외의 숫자
System.out.println(Message.MESSAGE_RESTART_ERROR);
throw new IllegalArgumentException();
}
} catch (Exception e) { // 잘못된 타입
System.out.println(Message.MESSAGE_RESTART_ERROR);
throw e;
}
}
}