summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2019-08-05 17:39:36 -0600
committermo khan <mo@mokhan.ca>2019-08-05 17:39:36 -0600
commit18b8556cc3520aacff7f96cacdc74c30ee73e95d (patch)
treee6db34769f49b8635d5fe899800c60e3905bb81f /src
parentf59d4c758d8edb2b538e77e342365d256d85d55c (diff)
date time arithmetic is insane
Diffstat (limited to 'src')
-rw-r--r--src/Q10/Station.java9
-rw-r--r--src/Q10/StationTest.java7
-rw-r--r--src/Q10/TrainTimeTable.java6
-rw-r--r--src/Q10/TrainTimeTableTest.java20
4 files changed, 30 insertions, 12 deletions
diff --git a/src/Q10/Station.java b/src/Q10/Station.java
index 3fcca70..f895282 100644
--- a/src/Q10/Station.java
+++ b/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));
+ }
}
diff --git a/src/Q10/StationTest.java b/src/Q10/StationTest.java
index a32191a..e3e64f6 100644
--- a/src/Q10/StationTest.java
+++ b/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());
+ }
}
diff --git a/src/Q10/TrainTimeTable.java b/src/Q10/TrainTimeTable.java
index 3e952e6..6ef6abb 100644
--- a/src/Q10/TrainTimeTable.java
+++ b/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() {
diff --git a/src/Q10/TrainTimeTableTest.java b/src/Q10/TrainTimeTableTest.java
index ac25c2d..c0ab35a 100644
--- a/src/Q10/TrainTimeTableTest.java
+++ b/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);