// SafeDateTester.java // Based upon the UseSafeDate application by Dale/Joyce/Weems (Chapter 1) // // Authors: Dale/Joyce/Weems, R. McCloskey, // and lab team members ..... // Known Defects: ... // // This application prompts the user to enter a calendar date, as described // by integers identifying the date's month, day, and year. If the values // entered describe a valid calendar date, that date is displayed in the // standard M/D/Y format (e.g., "5/14/1972") along with a message indicating // that it is valid, and the program terminates. If the entered values do // not describe a valid calendar date, a message indicating what is wrong // with the input data is displayed and the user is prompted to try again. // Thus, the program does not terminate until the user finally enters valid // input. import java.util.Scanner; public class SafeDateTester { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int m, d, y; // inputs describing a calendar date boolean dateOK = false; // loop terminates when dateOK becomes true Date theDate; // reference to a SafeDate object while (!dateOK) { // Prompt the user to enter integer values describing the // month, day, and year of a calendar date. System.out.println("Enter a date..."); System.out.print(" Month: "); m = scanner.nextInt(); // read month # System.out.print(" Day: "); d = scanner.nextInt(); // read day # System.out.print(" Year: "); y = scanner.nextInt(); // read year # try { // Create a SafeDate object, using the inputs to determine // the calendar date that it represents. theDate = new SafeDate(m, d, y); // for the purpose of preventing any more loop iterations dateOK = true; // Report that the SafeDate object was created successfully. System.out.println(theDate + " is a safe date."); } catch(DateOutOfBoundsException dobExcept) { System.out.println(dobExcept.getMessage() + "\n"); } } // end of while loop body } // end of main() }