Commit 4bb8e0f

mo khan <mo@mokhan.ca>
2019-08-06 00:06:05
concat tokens back into city name
1 parent fc18b37
Changed files (2)
src/Q10/Station.java
@@ -67,6 +67,8 @@ public class Station {
   }
 
   private String formatDate(Date date) {
+    if (date == null) return "-";
+
     DateFormat format = new SimpleDateFormat("HH:mm");
     format.setTimeZone(TimeZone.getTimeZone("UTC"));
     return format.format(date);
src/Q10/TrainTimeTable.java
@@ -15,7 +15,11 @@ public class TrainTimeTable {
   }
 
   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);
   }
@@ -42,22 +46,23 @@ public class TrainTimeTable {
 
   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));
+    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 = args.length > 0 ? args[0] : null;
+    String selection = null;
 
     while (true) {
       if (selection == null) {
@@ -73,11 +78,17 @@ public class TrainTimeTable {
       } else {
         String[] tokens = selection.split(" ");
 
-        if (tokens.length == 3) {
-          schedule.delay(tokens[1], Integer.parseInt(tokens[2]));
-        } else {
+        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;