//---------------------------------------------------------------------------- // TDIncDate241ArrayHmk4.java by Dale/Joyce/Weems Chapter 1 //test driver for homework that tests multiple constructors, exceptions, equals, isValidDay //---------------------------------------------------------------------------- import javax.swing.*; public class TDIncDate241ArrayHmk4 { public static void main(String[] args) { String command; IncDate [] theDates = new IncDate[100]; int month, day, year; do { command = JOptionPane.showInputDialog("Enter command: \n" + " IncDate0\n IncDate\n IncDate2\n IncDate3\n yearIs\n monthIs\n dayIs\n" + " increment\n toString\n equals\n compareTo\n isValidDay\n quit"); if (command.equalsIgnoreCase("IncDate")) { month = Integer.parseInt(JOptionPane.showInputDialog("Enter month")); day = Integer.parseInt(JOptionPane.showInputDialog("Enter day")); year = Integer.parseInt(JOptionPane.showInputDialog("Enter year")); System.out.println("Constructor invoked with " + month + " " + day + " " + year); try { IncDate theDate = new IncDate(month, day, year); //get index after so if exception is thrown, get index won't happen. int i = getIndex(); theDates[i] = theDate; } catch (DateOutOfBoundsException e) { System.out.println(e.getMessage() + " that date is rejected. try again"); } } else if (command.equalsIgnoreCase("IncDate2")) { // string ctor. String stringDate = JOptionPane.showInputDialog("Enter date as mm/dd/yyyy"); System.out.println("Constructor invoked with " + stringDate); try { //**HMK IncDate theDate = new IncDate(stringDate); //get index after so if exception is thrown, get index won't happen. int i = getIndex(); theDates[i] = theDate; } catch (DateOutOfBoundsException e) { System.out.println(e.getMessage() + " that date is rejected. try again"); } } else if (command.equalsIgnoreCase("IncDate3")) { // copy ctor int fromIndex = Integer.parseInt(JOptionPane.showInputDialog( "Enter index of object to copy from")); System.out.println("copy Constructor invoked with " + theDates[fromIndex]); theDates[getIndex()] = new IncDate(theDates[fromIndex]); } else if (command.equalsIgnoreCase("IncDate0")) { //**** HMK no-arg ctor System.out.println("no-arg Constructor invoked "); theDates[getIndex()] = new IncDate(); } else if (command.equalsIgnoreCase("yearIs")) System.out.println("Year is " + theDates[getIndex()].yearIs()); else if (command.equalsIgnoreCase("monthIs")) System.out.println("Month is " + theDates[getIndex()].monthIs()); else if (command.equalsIgnoreCase("dayIs")) System.out.println("Day is " + theDates[getIndex()].dayIs()); else if (command.equalsIgnoreCase("increment")) { int i = getIndex(); theDates[i].increment(); System.out.println("increment invoked: " + theDates[i]); //new value } else if (command.equalsIgnoreCase("toString")) { System.out.println("toString invoked: " + theDates[getIndex()]); } else if (command.equalsIgnoreCase("equals")) { int thisIndex = Integer.parseInt(JOptionPane.showInputDialog( "Enter index of object whose equals method to invoke")); int thatIndex = Integer.parseInt(JOptionPane.showInputDialog( "Enter index of object to be parameter to equals")); System.out.println("equals invoked: " + theDates[thisIndex].equals(theDates[thatIndex])); } else if (command.equalsIgnoreCase("compareTo")) { int thisIndex = Integer.parseInt(JOptionPane.showInputDialog( "Enter index of object whose compareTo method to invoke")); int thatIndex = Integer.parseInt(JOptionPane.showInputDialog( "Enter index of object to be parameter to compareTo")); System.out.println("compareTo invoked: " + theDates[thisIndex].compareTo(theDates[thatIndex])); } //*** HMK else if (command.equalsIgnoreCase("isValidDay")) { month = Integer.parseInt(JOptionPane.showInputDialog("Enter month")); day = Integer.parseInt(JOptionPane.showInputDialog("Enter day")); year = Integer.parseInt(JOptionPane.showInputDialog("Enter year")); //** static class method invoked via class name if (Date.isValidDay(month,day,year)) System.out.println("Valid " + month + "/" + day + "/" + year); else System.out.println("Invalid " + month + "/" + day + "/" + year); } } while(!command.equalsIgnoreCase("quit")); } //input int from user //could add some checking to ensure index is valid...to avoid subsequent crash public static int getIndex() { return Integer.parseInt(JOptionPane.showInputDialog("Enter index of object to access")); } }