package Q10; import java.io.*; import java.util.*; public class TrainTimeTable { private LinkedList schedule; public TrainTimeTable() { this(new LinkedList()); } public TrainTimeTable(LinkedList 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 stations = new LinkedList( 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; } } }