
This project depends upon Chapters 1-7, and provides experience with writing loops and robust input/output.
Write a package Date_Ops2. This package will have the following specification:
WITH Ada.Calendar;
PACKAGE Date_Ops2 IS
TYPE Months IS (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec); PROCEDURE Get (MonthName: OUT Months; Day: OUT Ada.Calendar.Day_Number;
Year: OUT Ada.Calendar.Year_Number);-- Pre: None
-- Post: The 3 values MonthName, Day, Year represent a valid dateEND Date_Op2;
In the body of this package, the procedure Get will
A date like Feb. 30, or even Feb. 29 in a non-leap year, is invalid. As it happens, Ada.Calendar provides a built-in way to check the validity of a date. The function Ada.Calendar.Time_Of has the specification
FUNCTION Time_Of (Year : Year_Number; Month : Month_Number; Day : Day_Number; Seconds : Day_Duration:=0.0) RETURN Time;
and returns a value of type Ada.Calendar.Time, if and only if the month/day/year combination forms a valid date. Otherwise, Time_Of raises the exception Ada.Calendar.Time_Error. Let's use this to check the validity of a date in the Get procedure: declare a variable in this procedure:
TestDate: Ada.Calendar.Time;
then call Ada.Calendar.Time_Of with the 3 parameters received by Get from the user.
Test.Date :=
Ada.Calendar.Time_Of(Month => Month, Day => Day, Year => Year);
(Of course you need to convert MonthName to a month number, but you know how to do that!)
Now, put an exception loop in the body of Get whose handler looks like
EXCEPTION
WHEN Ada.Calendar.Time_Error => -- write message to user