summaryrefslogtreecommitdiff
path: root/src/Q10/TrainTimeTable.java
blob: 6ef6abb944b2e481ef9dd97fff40d2f6c5d255d5 (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
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(String.format("Delay %s by %d minutes", city, minutes));
    Station station = stationIn(city);
    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() == city) return station;
    return null;
  }

  public static void main(String[] args) {
    System.out.println("=== Question 10 ===");
    LinkedList<Station> stations = new LinkedList<Station>();
    stations.add(new Station("Vancouver", new Date(), new Date(), 1));
    stations.add(new Station("Kamloops", new Date(), new Date(), 2));
    stations.add(new Station("Jasper", new Date(), new Date(), 2));
    stations.add(new Station("Edmonton", new Date(), new Date(), 2));
    stations.add(new Station("Saskatchewan", new Date(), new Date(), 3));
    stations.add(new Station("Winnipeg", new Date(), new Date(), 3));
    stations.add(new Station("Sioux Lookout", new Date(), new Date(), 4));
    stations.add(new Station("Hornepayne", new Date(), new Date(), 4));
    stations.add(new Station("Capreol", new Date(), new Date(), 5));
    stations.add(new Station("Toronto", new Date(), new Date(), 5));

    TrainTimeTable schedule = new TrainTimeTable(stations);

    Scanner in = new Scanner(System.in);
    String selection = args.length > 0 ? args[0] : 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) {
          schedule.delay(tokens[1], Integer.parseInt(tokens[2]));
        } else {
          System.out.println("Could not parse command");
        }
      }

      selection = null;
    }
  }
}