|
|
|
|
|
|
|
Discussion: When looking for birthdays, we are interested in month and day onlythe year is not important. If we were going through a conventional address book checking for birthdays by hand, we would write down the month and day of the date two weeks away and compare it to the month and day of each friend's birth date. |
|
|
|
|
|
|
|
|
We can use the same algorithm in our program. Using the DateType class that we developed, the member function Increment can be used to calculate the date two weeks (14 days) from the current date. We can use the class member function ComparedTo to determine whether a friend's birthday lies between the current date and the date two weeks away, inclusive. How do we ignore the year? We set the year of each friend's birth date to the current year for the comparison. However, if the current date plus 14 days is in the next year, and the friend's birthday is in January, then we must set the year to the current year plus one for the comparison to work correctly. |
|
|
|
 |
|
|
|
|
The DateType class, for manipulating dates. |
|
|
|
 |
|
|
|
|
A struct type PhoneType that stores an integer area code and an 8-character phone number. |
|
|
|
 |
|
|
|
|
A struct type EntryType that stores a 15-character first name, a 15-character last name, a telephone number of type PhoneType, and a birth date of type DateType. |
|
|
|
|
|
|
|
|
Open friendFile for input (and verify success)
Get current date into class object currentDate
Set targetDate = currentDate
FOR count going from 1 through 14
targetDate.Increment()
Get entry
WHILE NOT EOF on friendFile
IF targetDate.Year() isn't currentDate.Year() AND
entry.birthDate.Month() is 1
Set birthdayYear = targetDate.Year()
ELSE
Set birthdayYear = currentDate.Year()
birthday.Set(entry.birthDate.Month(), entry.birthDate.Day(), birthdayYear)
IF birthday.ComparedTo(currentDate) > SAME AND
birthday.ComparedTo(targetDate) < SAME
Print entry
Get entry |
|
|
|
|
|
|