-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainProgramme.java
More file actions
178 lines (142 loc) · 5.19 KB
/
MainProgramme.java
File metadata and controls
178 lines (142 loc) · 5.19 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import java.util.List;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Scanner;
public class MainProgramme {
private static Scanner sc = new Scanner(System.in);
private static String next() {
if (!sc.hasNextLine()) {
System.out.println("Fatal error in scanner");
return null;
}
return sc.nextLine();
}
private static int nextInt(int max) {
System.out.print("Enter a number: ");
while (true) {
String str = next();
try {
int ret = Integer.parseInt(str);
if (ret > 0 && ret <= max)
return ret;
else
System.out.println("Please enter a number between 1 and " + max);
} catch (NumberFormatException e) {
System.out.println("Please enter a valid number...");
}
}
}
private static BusNetwork.Stop searchStop(BusNetwork network) {
while (true) {
System.out.print("Search for a stop: ");
String search_term = next().toUpperCase();
List<BusNetwork.Stop> search_results = network.searchStops(search_term);
if (search_results.size() <= 0) { // Failed to find any results
System.out.println("No results");
return null;
} else if (search_results.size() == 1) { // Only one search result
return search_results.get(0);
}
System.out.println("\nChoose from the following results:");
int i = 0;
for (BusNetwork.Stop stop: search_results) {
i++;
System.out.println(i + ". " + stop.name);
}
System.out.println();
int index = nextInt(search_results.size())-1;
return search_results.get(index);
}
}
public static void doStops(BusNetwork network) {
System.out.println();
BusNetwork.Stop stop = searchStop(network);
if (stop == null) return;
while (true) {
System.out.println();
System.out.println("Selected stop: " + stop.name);
System.out.println("Choose from the following options:\n1. See stop data\n2. Plot route\n3. Exit to main menu\n");
int selection = nextInt(3);
if (selection == 1) {
System.out.println();
System.out.println(stop.dataToString());
} else if (selection == 2) {
System.out.println();
BusNetwork.Stop new_stop = searchStop(network);
if (new_stop == null) continue;
BusNetwork.Path path = network.getPath(stop, new_stop);
if (path == null)
System.out.println("\nNo route found");
else
System.out.println("\n" + path.toString());
} else {
break;
}
}
}
private static int findFirstChar(String str, char c) {
for (int i = 0; i < str.length(); i++)
if (str.charAt(i) == c)
return i;
return -1;
}
public static void doArrivalTimes(BusNetwork network) {
while (true) {
System.out.print("\nType \"exit\" to return to the main menu.\nEnter a time: ");
String search_term = next();
if (search_term.length() >= 4 && search_term.substring(0,4).toLowerCase().equals("exit"))
return;
LocalTime time;
try {
if (findFirstChar(search_term,':') < 2)
search_term = "0" + search_term;
time = LocalTime.parse(search_term);
} catch (java.time.format.DateTimeParseException e) {
System.out.println("Time was formatted incorrectly.");
continue;
}
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT);
System.out.println("Searching for all trips arriving at " + time.format(formatter) + "...\n");
List<BusNetwork.Trip> trips = network.getTripsAtTime(time);
if (trips.size() > 0) {
if (trips.size() == 1) { // Handling the case of 1 result
System.out.println("Only one trip ends at " + time);
System.out.println("\n" + trips.get(0).toString());
} else { // Selecting from multiple results
System.out.println("Trips ending at " + time + ": ");
for (int i = 0; i < trips.size(); i++)
System.out.println((i+1) + ". Trip id " + trips.get(i).id);
System.out.println("\nEnter a number to see trip details.");
int index = nextInt(trips.size())-1;
BusNetwork.Trip trip = trips.get(index);
System.out.println("\n" + trip.toString());
}
} else // No results
System.out.println("No trips found ending at " + time);
}
}
public static void main(String[] args) {
String stops_file = args.length > 0 ? args[0] : "src/stops.txt";
String transfers_file = args.length > 1 ? args[1] : "src/transfers.txt";
String stop_times_file = args.length > 2 ? args[2] : "src/stop_times.txt";
BusNetwork network = BusNetwork.networkFromFiles(stops_file, transfers_file, stop_times_file);
if (network == null) {
System.out.println("Error finding file, exiting programme...");
next(); // Requires user to press enter
return;
}
while (true) {
System.out.println("\nChoose from the following options:\n1. Search for stop\n2. Find trips by arrival time\n3. Exit programme\n");
int selection = nextInt(3);
if (selection == 1)
doStops(network);
else if (selection == 2)
doArrivalTimes(network);
else
break;
}
System.out.println("Exiting programme...");
next();
}
}