summaryrefslogtreecommitdiff
path: root/src/Q10/TrainTimeTable.java
blob: da556907a149cc7267914327606cf3bc693d5674 (plain)
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
/**
 * Assignment 2, COMP268 Class: TrainTimeTable.java
 *
 * @description Provides a class to manage a train schedule.
 * @author: mo khan Student ID: 3431709
 * @date August 5, 2019
 * @version 1.0
 */
package Q10;

import java.io.*;
import java.util.*;

public class TrainTimeTable {
  private LinkedList<Station> schedule;

  /** The default constructor. */
  public TrainTimeTable() {
    this(new LinkedList<Station>());
  }

  /**
   * An overloaded constructor to allow injections of each of the stations in the schedule.
   *
   * @param schedule the list of stations
   */
  public TrainTimeTable(LinkedList<Station> schedule) {
    this.schedule = schedule;
  }

  /**
   * Adds a delay to the schedule starting at the given city.
   *
   * @param city The city where the delay begins.
   * @param minutes The number of minutes to delay.
   */
  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);
    }
  }

  /** Prints the current schedule to standard out. */
  public void displaySchedule() {
    this.displaySchedule(System.out);
  }

  /**
   * An overload of displaySchedule that accepts a PrintStream to write output to different
   * printers.
   *
   * @param out the output stream to write to
   */
  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()));
  }

  /**
   * Returns the station that is in a city.
   *
   * @param city The city to search for
   * @return the Station if found or null.
   */
  public Station stationIn(String city) {
    for (Station station : this.schedule)
      if (station.getCity().toLowerCase().equals(city.toLowerCase())) return station;
    return null;
  }

  /**
   * This is the main entry point to the console application.
   *
   * @param args the argument vector to passed to the program.
   */
  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;
    }
  }
}