//---------------------------------------------------------------------------- // TDIncDate241HmkArray.java by Dale/Joyce/Weems Chapter 1 // modified to be interactive. further modified for homework. ch01 //further modified to have array of IncDates //also discarded some clutter e.g. frame //Uncomment lines 32-43 to test your Date and IncDate //---------------------------------------------------------------------------- import javax.swing.*; public class TDIncDate241HmkArray { public static void main(String[] args) { String command; //***array of 100 IncDate objects. array elements automatically initialized to null IncDate [] theDates = new IncDate[100]; int month, day, year; do { command = JOptionPane.showInputDialog("Enter command: \n" + " IncDate\n IncDate2\n IncDate3\n yearIs\n monthIs\n dayIs\n" + " increment\n toString\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); theDates[getIndex()] = new IncDate(month, day, year); } /* else if (command.equalsIgnoreCase("IncDate2")) { //**** HMK string ctor String stringDate = JOptionPane.showInputDialog("Enter date as mm/dd/yyyy"); System.out.println("Constructor invoked with " + stringDate); theDates[getIndex()] = new IncDate(stringDate); } else if (command.equalsIgnoreCase("IncDate3")) { //**** HMK 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("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()]); } } 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")); } }