-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathRacingController.java
More file actions
53 lines (40 loc) · 1.58 KB
/
RacingController.java
File metadata and controls
53 lines (40 loc) · 1.58 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
package controller;
import domain.RacingGame;
import util.GenerateRandomDistance;
import view.InputView;
import view.ResultView;
import java.util.*;
public class RacingController {
public void run(){
try{
final var cars = InputView.readCarNames(); //차이름 입력받기
final var tryCount = InputView.readTryCount(); //시도 횟수 입력받기
final var racingGame = new RacingGame(cars, tryCount);
race(racingGame); //경주 실행
ResultView.printWinners(getWinners(racingGame.getCars())); //우승자 출력
}catch(IllegalArgumentException e){
System.out.println(e.getMessage());
}
}
//경주 실행
private void race(RacingGame racingGame){
ResultView resultView = new ResultView();
resultView.start(racingGame.getCars());
for(int i = 0; i < racingGame.getTryCount(); i++){
GenerateRandomDistance.generateRandomDistance(racingGame.getCars());
resultView.printRaceStep(racingGame.getCars());
System.out.println();
}
}
//우승자 구하는 메소드
private List<String> getWinners(Map<String, Integer> cars){
List<String> winners = new ArrayList<>();
int maxValue = Collections.max(cars.values());
for(Map.Entry<String, Integer> entry : cars.entrySet()){ //이거 메소드로 분리하는게 가독성이 더 떨어질듯?
if(entry.getValue() == maxValue){
winners.add(entry.getKey());
}
}
return winners;
}
}