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
|
package Q10;
import java.io.*;
import java.util.*;
public class TrainTimeTable {
private LinkedList<Station> schedule;
public TrainTimeTable() {
this(new LinkedList<Station>());
}
public TrainTimeTable(LinkedList<Station> schedule) {
this.schedule = schedule;
}
public void delay(String city, int minutes) {
Station station = stationIn(city);
if (station == null)
throw new IllegalArgumentException(String.format("`%s` is not on the schedule", city));
station.delayBy(minutes);
for (int i = schedule.indexOf(station) + 1; i < schedule.size(); i++) {
schedule.get(i).delayBy(minutes);
}
}
public void displaySchedule() {
this.displaySchedule(System.out);
}
public void displaySchedule(PrintStream out) {
out.println(String.format("%15s | %7s | %9s | %3s", "Station", "Arrival", "Departure", "Day"));
for (Station station : schedule)
out.println(
String.format(
"%15s | %7s | %9s | %3d",
station.getCity(), station.getArrival(), station.getDeparture(), station.getDay()));
}
public Station stationIn(String city) {
for (Station station : this.schedule)
if (station.getCity().toLowerCase().equals(city.toLowerCase())) return station;
return null;
}
public static void main(String[] args) {
System.out.println("=== Question 10 ===");
LinkedList<Station> stations =
new LinkedList<Station>(
Arrays.asList(
new Station("Vancouver", null, new Date(1546374600000L), 1),
new Station("Kamloops", new Date(1546408800000L), new Date(1546410900000L), 2),
new Station("Jasper", new Date(1546444800000L), new Date(1546450200000L), 2),
new Station("Edmonton", new Date(1546470000000L), new Date(1546473540000L), 2),
new Station("Saskatchewan", new Date(1546502400000L), new Date(1546503900000L), 3),
new Station("Winnipeg", new Date(1546548300000L), new Date(1546554600000L), 3),
new Station("Sioux Lookout", new Date(1546578120000L), new Date(1546580520000L), 4),
new Station("Hornepayne", new Date(1546616100000L), new Date(1546618200000L), 4),
new Station("Capreol", new Date(1546647480000L), new Date(1546649280000L), 5),
new Station("Toronto", new Date(1546680600000L), null, 5)));
TrainTimeTable schedule = new TrainTimeTable(stations);
Scanner in = new Scanner(System.in);
String selection = null;
while (true) {
if (selection == null) {
System.out.println();
System.out.println("Enter command (Show, Delay, Quit):");
selection = in.nextLine();
}
String[] tokens = selection.split(" ");
switch (tokens[0].toLowerCase()) {
case "quit":
System.exit(0);
return;
case "show":
schedule.displaySchedule(System.out);
break;
case "delay":
if (tokens.length < 3) break;
String city = "";
for (int i = 1; i < tokens.length - 1; i++) city += tokens[i];
try {
String enteredMinutes = tokens[tokens.length - 1];
if (!enteredMinutes.matches("\\d+")) {
System.out.println("Invalid minutes entered");
break;
}
int minutes = Integer.parseInt(enteredMinutes);
if (minutes > (48 * 60)) {
System.out.println("Invalid minutes entered");
break;
}
schedule.delay(city, minutes);
} catch (IllegalArgumentException error) {
System.out.println(error.getMessage());
}
break;
default:
System.out.println("Unknown command");
break;
}
selection = null;
}
}
}
|