Commit 18b8556

mo khan <mo@mokhan.ca>
2019-08-05 23:39:36
date time arithmetic is insane
1 parent f59d4c7
src/Q10/Station.java
@@ -56,9 +56,18 @@ public class Station {
     return formatDate(this.departure);
   }
 
+  public void delayBy(int minutes) {
+    this.setArrivalDate(advanceDate(this.getArrivalDate(), minutes));
+    this.setDepartureDate(advanceDate(this.getDepartureDate(), minutes));
+  }
+
   private String formatDate(Date date) {
     DateFormat format = new SimpleDateFormat("HH:mm");
     format.setTimeZone(TimeZone.getTimeZone("UTC"));
     return format.format(date);
   }
+
+  private Date advanceDate(Date original, int minutes) {
+    return new Date(original.getTime() + (minutes * 60000));
+  }
 }
src/Q10/StationTest.java
@@ -27,4 +27,11 @@ public class StationTest extends TestCase {
   public void test_getDeparture() {
     assertEquals("23:59", subject.getDeparture());
   }
+
+  public void test_delayBy() {
+    subject.delayBy(30);
+    assertEquals("23:30", subject.getArrival());
+    assertEquals("00:29", subject.getDeparture());
+    assertEquals(3, subject.getDay());
+  }
 }
src/Q10/TrainTimeTable.java
@@ -14,8 +14,10 @@ public class TrainTimeTable {
     this.schedule = schedule;
   }
 
-  public void delay(String station, int minutes) {
-    System.out.println(String.format("Delay %s by %d minutes", station, minutes));
+  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() {
src/Q10/TrainTimeTableTest.java
@@ -13,16 +13,16 @@ public class TrainTimeTableTest extends TestCase {
   private 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)));
+              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)));
 
   public TrainTimeTableTest(String testName) {
     super(testName);