Example 12.7.1. A Top-Down Design.
A top-down design for our date program would start by identifying the high-level tasks:
-
β Get user input for two dates.
-
β Calculate the number of days between two dates. Inputs: two strings (like
"3/4/2023"
and "4/5/2024"); Output: an integer representing the number of days between the two dates.int daysBetween(string date1, string date2)
Note 12.7.2.
We will use β
to mark new functions at each step of the process. Each time you see a list of function ideas, look for that symbol to find the new functions. Anything without that symbol is a function we already identified in a previous step.
That sounds too complicated to implement in a few lines of code. So letβs break it down.
If we can convert each date into a number of days since the start of year 0000, we can then find the difference between those two numbers. So we will need a function that converts a date string into a number of days. That function will take a string like
"3/4/2023"
and return an integer representing the number of days since 0/0/0. Letβs call it dateToDays
. The existing daysBetween
function will use dateToDays
to help do its job.-
Get user input for two dates.
-
Calculate the number of days between two dates. Inputs: two strings (like
"3/4/2023"
and"4/5/2024"
); Output: an integer representing the number of days between the two dates.int daysBetween(string date1, string date2)
-
β Convert each date string into a number of days since 0/0/0. Inputs: a string like
"3/4/2023"
; Output: an integer representing the number of days since 0/0/0.int dateToDays(string date)
-
The
dateToDays
function is going to need to work with the month, day and year as numbers. So maybe we should write functions to get those parts of the string as integers:-
Get user input for two dates.
-
Calculate the number of days between two dates. Inputs: two strings (like
"3/4/2023"
and "4/5/2024"); Output: an integer representing the number of days between the two dates.int daysBetween(string date1, string date2)
-
Convert each date string into a number of days since 0/0/0. Inputs: a string like
"3/4/2023"
; Output: an integer representing the number of days since 0/0/0.int dateToDays(string date)
-
β Get the month as an integer. Inputs: a string like
"3/4/2023"
; Output: an integer representing the month (3 in this case).int getMonth(string date)
-
β Get the day as an integer. Inputs: a string like
"3/4/2023"
; Output: an integer representing the day (4 in this case).int getDay(string date)
-
β Get the year as an integer. Inputs: a string like
"3/4/2023"
; Output: an integer representing the year (2023 in this case).int getYear(string date)
-
-
Those all sound easy enough to implement, they donβt need any more refinement.
However, calculating the total number of days still sounds a bit complex. Working with the days and years should be easy. We know each year is 365 days (recall that we are cheating a little and ignoring leap years) and that each day is 1 day. But the months will complicate things.
What would make the job easier? Well, what if I could ask someone βhow many days are there before month X starts?β. If I could ask βhow many days were there before the start of Marchβ and get the answer 59 (31 for Jan + 28 for Feb), it would be easy to do the rest of
dateToDays
.
So letβs add a function
daysBeforeMonth
that answers that question. Our design now looks like:-
Get user input for two dates.
-
Calculate the number of days between two dates. Inputs: two strings (like
"3/4/2023"
and "4/5/2024"); Output: an integer representing the number of days between the two dates.int daysBetween(string date1, string date2)
-
Convert each date string into a number of days since 0/0/0. Inputs: a string like
"3/4/2023"
; Output: an integer representing the number of days since 0/0/0.int dateToDays(string date)
-
Get the month as an integer. Inputs: a string like
"3/4/2023"
; Output: an integer representing the month (3 in this case).int getMonth(string date)
-
Get the day as an integer. Inputs: a string like
"3/4/2023"
; Output: an integer representing the day (4 in this case).int getDay(string date)
-
Get the year as an integer. Inputs: a string like
"3/4/2023"
; Output: an integer representing the year (2023 in this case).int getYear(string date)
-
β Get the number of days before the given month. Inputs: an integer month; Output: an integer representing the number of days before the given month started.
int daysBeforeMonth(int month)
-
-
At this point, each of the listed functions seems simple enough to implement on its own. (The new function could just be a big if/else chain (if the month is 0, return 0; if the month is 1, return 31; if the month is 2, return 59; etc...). So now we can start implementation.