-
Notifications
You must be signed in to change notification settings - Fork 274
Expand file tree
/
Copy pathSectionController.java
More file actions
84 lines (73 loc) · 2.82 KB
/
SectionController.java
File metadata and controls
84 lines (73 loc) · 2.82 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
package subway.controller;
import java.util.Scanner;
import subway.domain.Line;
import subway.domain.Station;
import subway.domain.service.LineService;
import subway.domain.service.SectionService;
import subway.domain.service.StationService;
import subway.view.DetailView;
import utils.Category;
import utils.ScriptUtils;
public class SectionController {
private static DetailView sectionView = new DetailView(Category.SECTION);
private static int askCount = 2;
private static int ADD = 1;
private SectionController() { }
public static void manageSection(Scanner scanner, int selection) {
if (selection == ADD) {
addSection(scanner, selection);
return;
}
deleteSection(scanner, selection);
}
private static void deleteSection(Scanner scanner, int selection) {
Line line = askLine(scanner, selection);
Station station = askStation(scanner, selection);
if (line != null && station != null) {
SectionService.deleteSection(line, station);
}
}
private static void addSection(Scanner scanner, int selection) {
Line line = askLine(scanner, selection);
Station station = askStation(scanner, selection);
int idx = askIdx(scanner, selection, line);
if (line != null && station != null && idx >= 0) {
SectionService.createSection(line, station, idx);
}
}
private static int askIdx(Scanner scanner, int selection, Line line) {
int idx = sectionView.askSectionNumber(scanner, ScriptUtils.ASK_ANSWER_FOR_SECTION[selection-1][2]);
if (idx < 0) {
return idx;
}
try {
if (idx > line.getLength()) {
throw new IllegalArgumentException(ScriptUtils.ERROR_OUT_OF_BOUNDARY);
}
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
return -1;
}
return idx;
}
private static Station askStation(Scanner scanner, int selection) {
try {
String stationName = sectionView.askSection(scanner, ScriptUtils.ASK_ANSWER_FOR_SECTION[selection-1][1]);
SectionService.checkDuplicate(1, stationName);
return StationService.readStation(stationName);
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
return null;
}
}
private static Line askLine(Scanner scanner, int selection) {
try {
String lineName = sectionView.askSection(scanner, ScriptUtils.ASK_ANSWER_FOR_SECTION[selection-1][0]);
SectionService.checkDuplicate(0, lineName);
return LineService.readLine(lineName);
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
return null;
}
}
}