master
1/**
2 * Assignment 2, COMP268 Class: TrainTimeTable.java
3 *
4 * @description Provides a class to manage a train schedule.
5 * @author: mo khan Student ID: 3431709
6 * @date August 5, 2019
7 * @version 1.0
8 */
9package Q10;
10
11import java.io.*;
12import java.util.*;
13
14public class TrainTimeTable {
15 private LinkedList<Station> schedule;
16
17 /** The default constructor. */
18 public TrainTimeTable() {
19 this(new LinkedList<Station>());
20 }
21
22 /**
23 * An overloaded constructor to allow injections of each of the stations in the schedule.
24 *
25 * @param schedule the list of stations
26 */
27 public TrainTimeTable(LinkedList<Station> schedule) {
28 this.schedule = schedule;
29 }
30
31 /**
32 * Adds a delay to the schedule starting at the given city.
33 *
34 * @param city The city where the delay begins.
35 * @param minutes The number of minutes to delay.
36 */
37 public void delay(String city, int minutes) {
38 Station station = stationIn(city);
39 if (station == null)
40 throw new IllegalArgumentException(String.format("`%s` is not on the schedule", city));
41
42 station.delayBy(minutes);
43 for (int i = schedule.indexOf(station) + 1; i < schedule.size(); i++) {
44 schedule.get(i).delayBy(minutes);
45 }
46 }
47
48 /** Prints the current schedule to standard out. */
49 public void displaySchedule() {
50 this.displaySchedule(System.out);
51 }
52
53 /**
54 * An overload of displaySchedule that accepts a PrintStream to write output to different
55 * printers.
56 *
57 * @param out the output stream to write to
58 */
59 public void displaySchedule(PrintStream out) {
60 out.println(String.format("%15s | %7s | %9s | %3s", "Station", "Arrival", "Departure", "Day"));
61
62 for (Station station : schedule)
63 out.println(
64 String.format(
65 "%15s | %7s | %9s | %3d",
66 station.getCity(), station.getArrival(), station.getDeparture(), station.getDay()));
67 }
68
69 /**
70 * Returns the station that is in a city.
71 *
72 * @param city The city to search for
73 * @return the Station if found or null.
74 */
75 public Station stationIn(String city) {
76 for (Station station : this.schedule)
77 if (station.getCity().toLowerCase().equals(city.toLowerCase())) return station;
78 return null;
79 }
80
81 /**
82 * This is the main entry point to the console application.
83 *
84 * @param args the argument vector to passed to the program.
85 */
86 public static void main(String[] args) {
87 System.out.println("=== Question 10 ===");
88 LinkedList<Station> stations =
89 new LinkedList<Station>(
90 Arrays.asList(
91 new Station("Vancouver", null, new Date(1546374600000L), 1),
92 new Station("Kamloops", new Date(1546408800000L), new Date(1546410900000L), 2),
93 new Station("Jasper", new Date(1546444800000L), new Date(1546450200000L), 2),
94 new Station("Edmonton", new Date(1546470000000L), new Date(1546473540000L), 2),
95 new Station("Saskatchewan", new Date(1546502400000L), new Date(1546503900000L), 3),
96 new Station("Winnipeg", new Date(1546548300000L), new Date(1546554600000L), 3),
97 new Station("Sioux Lookout", new Date(1546578120000L), new Date(1546580520000L), 4),
98 new Station("Hornepayne", new Date(1546616100000L), new Date(1546618200000L), 4),
99 new Station("Capreol", new Date(1546647480000L), new Date(1546649280000L), 5),
100 new Station("Toronto", new Date(1546680600000L), null, 5)));
101
102 TrainTimeTable schedule = new TrainTimeTable(stations);
103 Scanner in = new Scanner(System.in);
104 String selection = null;
105
106 while (true) {
107 if (selection == null) {
108 System.out.println();
109 System.out.println("Enter command (Show, Delay, Quit):");
110 selection = in.nextLine();
111 }
112 String[] tokens = selection.split(" ");
113 switch (tokens[0].toLowerCase()) {
114 case "quit":
115 System.exit(0);
116 return;
117 case "show":
118 schedule.displaySchedule(System.out);
119 break;
120 case "delay":
121 if (tokens.length < 3) break;
122
123 String city = "";
124 for (int i = 1; i < tokens.length - 1; i++) city += tokens[i];
125
126 try {
127 String enteredMinutes = tokens[tokens.length - 1];
128
129 if (!enteredMinutes.matches("\\d+")) {
130 System.out.println("Invalid minutes entered");
131 break;
132 }
133
134 int minutes = Integer.parseInt(enteredMinutes);
135 if (minutes > (48 * 60)) {
136 System.out.println("Invalid minutes entered");
137 break;
138 }
139 schedule.delay(city, minutes);
140 } catch (IllegalArgumentException error) {
141 System.out.println(error.getMessage());
142 }
143
144 break;
145 default:
146 System.out.println("Unknown command");
147 break;
148 }
149 selection = null;
150 }
151 }
152}