This project depends upon Chapters 1-6, and provides experience with writing loops, files, and robust input/output.
Add a child package DayWeek.IO to the package DayWeek. (See Section 6.8 for an example of writing a child package.) This package will have the following specification:
WITH Ada.Calendar;
PACKAGE DayWeek.IO 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 date
END DayWeek.IO;
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
Now use the DayWeek and DayWeek.IO packages to create a disk file called calendar.dat. Your program will prompt the user for a starting date and an ending date, then write, into the external file, a line for each day in the given range. For example, if the user enters
APR 2 1993 MAY 29 1993
as the starting and ending months, the file will contain, after the program is done, 58 lines. The first line will say
FRI APR 1 1993
and the last line will say
WED MAY 29 1993
Of course, if the user enters different years for the starting and ending values, the file will be much larger! Design your algorithm carefully before even thinking about code.
Read the starting and ending months using DayWeek.IO.Get.
To create a file into which your program will write, your program will contain a variable declaration
MyFile: Ada.Text_IO.File_Type;
To associate the file name with a file in the file system, include this statement after the BEGIN of your program:
Ada.Text_IO.Create (File=>MyFile,Mode=>Ada.Text_IO.Out_File,Name=>"calendar.dat");
To write an integer value into this file, use the file-oriented Ada.Text_IO operations, for example, if Today is an integer variable, use
Ada.Integer_Text_IO.Put(File=>MyFile,Item=>Today,Width=>2);