Commit a117798

mo khan <mo@mokhan.ca>
2019-08-11 21:59:50
add some error handling
1 parent a70558e
src/Q1/README.md
@@ -94,9 +94,8 @@ Tests run: 7, Failures: 0, Errors: 0, Skipped: 0
 * - Manually running the program with valid input.
 
 ```bash
-モ bundle exec rake run1
-javac ReversedSentence.java
-java ReversedSentence
+モ mvn package
+モ java -cp target/assignment2*.jar ca.mokhan.comp268.App 1
 Enter sentence 1: (max 80 characters)
 mary had a little lamb
 Enter sentence 2: (max 80 characters)
@@ -116,9 +115,8 @@ Bye
 * - Manually running the program with input greater than 80 characters.
 
 ```bash
-モ bundle exec rake run1
-javac ReversedSentence.java
-java ReversedSentence
+モ mvn package
+モ java -cp target/assignment2*.jar ca.mokhan.comp268.App 1
 Enter sentence 1: (max 80 characters)
 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
 Enter sentence 2: (max 80 characters)
@@ -138,9 +136,8 @@ Bye
 * - Manually running the program with no input
 
 ```bash
-モ bundle exec rake run1
-javac ReversedSentence.java
-java ReversedSentence
+モ mvn package
+モ java -cp target/assignment2*.jar ca.mokhan.comp268.App 1
 Enter sentence 1: (max 80 characters)
 
 Enter sentence 2: (max 80 characters)
src/Q10/README.md
@@ -51,6 +51,78 @@
   1. Quit - stop the program from accepting any more commands.
 
 1. Description of the Code
+  There are two classes. `TrainTimeTable` is responsible for add a delay in
+  minutes starting for a specific station. `Station` is responsible for managing
+  the arrival and departure time to/from a specific train station.
+
+  When a delay is added to the time table the code will add the delay to the
+  station, then to each station after it.
+
+  It would have been easier to represent dates as a unix timestamp, which is the
+  number of seconds since the UNIX epoch.
+  The Java Date class prefers to use millisecond precision.
+
+  All dates are represented using the UTC timezone.
+
+  I chose to add a method/constructor overloads to make the API of each of the
+  classes easier to unit test.
+  This also helped the class design to better adhere to the open/closed principle.
+
 1. Errors and Warnings
+
+  When an unknown command is entered, the prompt is shown again.
+
+  ```bash
+  モ mvn package
+  モ java -cp target/assignment2*.jar ca.mokhan.comp268.App 10
+  === Question 10 ===
+
+  Enter command (Show, Delay, Quit):
+  invalid
+  Unknown command
+
+  Enter command (Show, Delay, Quit):
+  ```
+
+  When an unknown city is entered, an error is displayed.
+
+  ```bash
+  モ mvn package
+  モ java -cp target/assignment2*.jar ca.mokhan.comp268.App 10
+  === Question 10 ===
+
+  Enter command (Show, Delay, Quit):
+  Delay Calgary 25
+  `Calgary` is not on the schedule
+
+  Enter command (Show, Delay, Quit):
+  ```
+
+  When a non-numeric value is entered for minutes, an error is displayed.
+
+  ```bash
+  java -cp target/assignment2*.jar ca.mokhan.comp268.App 10
+  === Question 10 ===
+
+  Enter command (Show, Delay, Quit):
+  Delay Edmonton blah
+  Invalid minutes entered
+
+  Enter command (Show, Delay, Quit):
+  ```
+
+  When a delay of greater than 48 hours is entered, an error is displayed.
+
+  ```bash
+  java -cp target/assignment2*.jar ca.mokhan.comp268.App 10
+  === Question 10 ===
+
+  Enter command (Show, Delay, Quit):
+  Delay Edmonton 9999
+  Invalid minutes entered
+
+  Enter command (Show, Delay, Quit):
+  ```
+
 1. Sample Input and Output
 1. Discussion
src/Q10/TrainTimeTable.java
@@ -16,11 +16,12 @@ public class TrainTimeTable {
 
   public void delay(String city, int minutes) {
     Station station = stationIn(city);
-    if (station != null) {
-      station.delayBy(minutes);
-      for (int i = schedule.indexOf(station) + 1; i < schedule.size(); i++) {
-        schedule.get(i).delayBy(minutes);
-      }
+    if (station == null)
+      throw new IllegalArgumentException(String.format("`%s` is not on the schedule", city));
+
+    station.delayBy(minutes);
+    for (int i = schedule.indexOf(station) + 1; i < schedule.size(); i++) {
+      schedule.get(i).delayBy(minutes);
     }
   }
 
@@ -68,23 +69,45 @@ public class TrainTimeTable {
       if (selection == null) {
         System.out.println();
         System.out.println("Enter command (Show, Delay, Quit):");
-        selection = in.nextLine().toLowerCase();
+        selection = in.nextLine();
       }
-
-      if (selection.equals("quit")) return;
-
-      if (selection.equals("show")) {
-        schedule.displaySchedule(System.out);
-      } else {
-        String[] tokens = selection.split(" ");
-
-        if (tokens.length < 3) continue;
-
-        String city = "";
-        for (int i = 1; i < tokens.length - 1; i++) city += tokens[i];
-        schedule.delay(city, Integer.parseInt(tokens[tokens.length - 1]));
+      String[] tokens = selection.split(" ");
+      switch (tokens[0].toLowerCase()) {
+        case "quit":
+          System.exit(0);
+          return;
+        case "show":
+          schedule.displaySchedule(System.out);
+          break;
+        case "delay":
+          if (tokens.length < 3) break;
+
+          String city = "";
+          for (int i = 1; i < tokens.length - 1; i++) city += tokens[i];
+
+          try {
+            String enteredMinutes = tokens[tokens.length - 1];
+
+            if (!enteredMinutes.matches("\\d+")) {
+              System.out.println("Invalid minutes entered");
+              break;
+            }
+
+            int minutes = Integer.parseInt(enteredMinutes);
+            if (minutes > (48 * 60)) {
+              System.out.println("Invalid minutes entered");
+              break;
+            }
+            schedule.delay(city, minutes);
+          } catch (IllegalArgumentException error) {
+            System.out.println(error.getMessage());
+          }
+
+          break;
+        default:
+          System.out.println("Unknown command");
+          break;
       }
-
       selection = null;
     }
   }