import java.util.*; import java.io.*; public class TimeTest { public static void main(String[] args) { printMenu(); doMenu(); } //---------------------------------------- // prints the menu choices public static void printMenu() { System.out.println("Menu choices are:"); System.out.println("q: quit the testing program"); System.out.println("?: print this menu"); System.out.println("+: add time to the current time span"); System.out.println("p: print the current time span"); System.out.println("r: reset the time span"); System.out.println("s: set the time span"); System.out.println(); } //---------------------------------------- // reads commands from the user and handles them public static void doMenu() { Scanner scan = new Scanner(System.in); TimeSpan t = new TimeSpan(); String theCommand; try { System.out.print("\nEnter command: "); while (scan.hasNext()) { theCommand = scan.next(); System.out.println(); if (theCommand.length() > 0) { if (theCommand.length() == 1) { char command = theCommand.charAt(0); if (command == 'q') { throw new IOException(); } else if (command == '?') { printMenu(); } else if (command == '+') { addValue(scan, t); } else if (command == 'p') { System.out.println("Current Time Span: " + t); } else if (command == 'r') { t.reset(); } else if (command == 's') { testSet(scan, t); } } else { System.out.println("Only one letter commands allowed. " + "Your longer one was ignored."); } System.out.println("Enter command:"); } } } catch (IllegalStateException e) { System.out.println(e.getMessage()); System.out.println("Thank you for using TimeSpan."); return; } catch (IOException e) { System.out.println("Thank you for using TimeSpan."); return; } } //---------------------------------------- // Get input to test the set operation public static void testSet(Scanner scan, TimeSpan t) { int hrs; int min; System.out.print("New hours: "); hrs = scan.nextInt(); System.out.print("New minutes: "); min = scan.nextInt(); t.set(hrs, min); } //---------------------------------------- // Get input to test the add operation public static void addValue(Scanner scan, TimeSpan t) { int hrs; int min; System.out.print("Add hours: "); hrs = scan.nextInt(); System.out.print("Add minutes: "); min = scan.nextInt(); t.add(hrs, min); } }