Commit 63e281a

mo khan <mo@mokhan.ca>
2019-08-05 21:16:22
connect Q10 console application
1 parent fc63be3
src/Q10/Station.java
@@ -8,7 +8,12 @@ public class Station {
   private int day;
   private String city;
 
-  public Station(String city, Date arrival, Date departure, int day) {}
+  public Station(String city, Date arrival, Date departure, int day) {
+    this.city = city;
+    this.arrival = arrival;
+    this.departure = departure;
+    this.day = day;
+  }
 
   public Date getArrivalDate() {
     return this.arrival;
@@ -23,22 +28,30 @@ public class Station {
   }
 
   public String getArrival() {
-    return "";
+    return this.getArrivalDate().toString();
   }
 
   public String getCity() {
-    return "";
+    return this.city;
   }
 
   public String getDeparture() {
-    return "";
+    return getDepartureDate().toString();
   }
 
-  public void setArrivalDate(Date arrival) {}
+  public void setArrivalDate(Date arrival) {
+    this.arrival = arrival;
+  }
 
-  public void setCity(String city) {}
+  public void setCity(String city) {
+    this.city = city;
+  }
 
-  public void setDay(int day) {}
+  public void setDay(int day) {
+    this.day = day;
+  }
 
-  public void setDepartureDate(Date departure) {}
+  public void setDepartureDate(Date departure) {
+    this.departure = departure;
+  }
 }
src/Q10/TrainTimeTable.java
@@ -1,11 +1,70 @@
 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 station, int minutes) {}
 
-  public void displaySchedule() {}
+  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 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.next().toLowerCase();
+      }
+
+      if (selection.equals("quit")) return;
+
+      if (selection.equals("show")) {
+        schedule.displaySchedule(System.out);
+      } else {
+        System.out.println("Delay schedule");
+      }
+
+      selection = null;
+    }
+  }
 }
src/App.java
@@ -42,6 +42,9 @@ public class App {
         case 9:
           Q9.MovingRobot.main(args);
           break;
+        case 10:
+          Q10.TrainTimeTable.main(args);
+          break;
         default:
           System.out.println("Bye");
           System.exit(0);