summaryrefslogtreecommitdiff
path: root/src/Q10/TrainTimeTable.java
blob: 5883983e1b749a28b75b6a06f3c972ccfba31904 (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
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) {
    System.out.println();
    System.out.println(city);
    System.out.println(String.format("Delay %s by %d minutes", city, minutes));
    System.out.println();

    Station station = stationIn(city);
    if (station != null) station.delayBy(minutes);
  }

  public void displaySchedule() {
    this.displaySchedule(System.out);
  }

  public void displaySchedule(PrintStream out) {
    out.println("Station | Arrival | Departure | Day");

    for (Station station : schedule)
      out.println(
          String.format(
              "%s | %s | %s | %d",
              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().toLowerCase();
      }

      if (selection.equals("quit")) return;

      if (selection.equals("show")) {
        schedule.displaySchedule(System.out);
      } else {
        String[] tokens = selection.split(" ");

        if (tokens.length < 3) {
          System.out.println("Could not parse command");
          continue;
        }

        String city = "";
        for (int i = 1; i < tokens.length - 1; i++) {
          System.out.println(tokens[i]);
          city += tokens[i];
        }
        schedule.delay(city, Integer.parseInt(tokens[tokens.length - 1]));
      }

      selection = null;
    }
  }
}