Calendar: Difference between revisions

2,006 bytes added ,  4 years ago
Line 6,727:
To run it, evaluate: <lang smalltalk>CalendarPrinter printOnTranscriptForYearNumber: 1969</lang>
<lang Smalltalk>
"Instance Variables:
- yearToPrint: The year to print the calendar of
- stream: The stream used to print the calendar
- monthsOfYear: A collection of all the months of yearToPrint, for example January 1969, February 1969, and so on
- currentMonths: Group of 3 months under print, for example from January to March or April to June, and so on
- monthOfYearCurrentDate: A collection that associates a month with the date to print for that month. It is used to
know which day number should be printed for each month when iterating over the day of weeks of each row"
Object subclass: #CalendarPrinter
instanceVariableNames: 'yearToPrint stream monthsOfYear currentMonths monthOfYearCurrentDate'
Line 6,746 ⟶ 6,753:
 
value
"Prints the year number (header) and then its months"
self
printHeader;
Line 6,751 ⟶ 6,759:
 
printHeader
"Prints the year number centered"
self center: yearToPrint number printString in: self numberOfCharactersPerLine.
stream newLine; newLine.
 
printMonths
"Prints each group of 3 months, for example from January to March, then April to June and so on"
(January to: December by: 3*month) do: [ :aMonth | self printMonthsStartingOn: aMonth ].
 
printMonthsStartingOn: aMonth
"For each group of 3 months starting in aMonth, prints the name of the month (header),
the name of the days of the week (Mo Tu ...), and then the day numbers"
currentMonths := aMonth to: aMonth next next.
self
Line 6,764 ⟶ 6,776:
printDayNumbers.
 
printMonthsHeader
"Prints the current group of 3 months names, centered"
currentMonths
do: [ :currentMonth | self center: currentMonth printString in: self numberOfCharactersPerMonth ]
Line 6,771 ⟶ 6,784:
 
printDaysOfWeekHeader
"Prints the names of the days of week for each month of the current group of 3 months"
currentMonths
do: [ :currentMonth | self printOneMonthDaysOfWeekHeader ]
Line 6,777 ⟶ 6,791:
 
printOneMonthDaysOfWeekHeader
"Prints the name of the days of week"
(Sunday to: Saturday)
do: [ :aDayOfWeek | stream nextPutAll: (aDayOfWeek printString first: 2) ]
Line 6,782 ⟶ 6,797:
 
printDayNumbers
"While there are day numbers to print, prints them in a row"
[self hasDayNumbersToPrint] whileTrue: [ self printDayNumbersRow ].
 
hasDayNumbersToPrint
"If any of the group of 3 months currently printing has day numbers to print returns true, otherwise false"
^currentMonths anySatisfy: [ :currentMonth | self isCurrentDateAtSameMonthOfYearAs: currentMonth ]
isCurrentDateAtSameMonthOfYearAs: currentMonth
"Returns true if the date to print for currentMonth is actually for the currentMonth"
^self is: (self currentDateOf: currentMonth) atSameMonthOfYearAs:month = currentMonth
 
is: aDate atSameMonthOfYearAs: currentMonth
^aDate monthOfYear = (monthsOfYear at: currentMonth number)
 
printDayNumbersRow
"For each month of the group of 3, prints a row of day numbers"
currentMonths
do: [ :currentMonth | self printDayNumbersRowOf: currentMonth ]
Line 6,800 ⟶ 6,816:
 
printDayNumbersRowOf: currentMonth
"Prints the day numbers of the current week"
(Sunday to: Saturday)
do: [ :aDayOfWeek | self printDayNumberOf: currentMonth for: aDayOfWeek ]
Line 6,805 ⟶ 6,822:
 
printDayNumberOf: currentMonth for: aDayOfWeek
"If the current date of the current month corresponds to aDayOfWeeks, prints its day number,
if not, leaves a space
This is important to leave the spaces in the first row and last row for those day of week
that do not have a day number related"
| currentDate |
currentDate := self currentDateOf: currentMonth.
Line 6,814 ⟶ 6,835:
 
hasToPrint: aDate of: aCurrentMonth for: aDayOfWeek
^(self"Returns is:whether aDate atSameMonthOfYearAs:is part of aCurrentMonth) and: [aDateits day =of week is aDayOfWeek].
It is used to decide whether to print the date day number or leave an space"
^(aDate month = aCurrentMonth) and: [aDate day = aDayOfWeek]
 
currentDateOf: currentMonth
"Returns the date to print for currentMonth"
^monthOfYearCurrentDate at: currentMonth number
 
calculateNextCurrentDateOf: currentMonth
"Changes the date to print of the currentMonth to the next date"
monthOfYearCurrentDate at: currentMonth number put: (self currentDateOf: currentMonth) next
 
center: stringToCenter in: aNumberOfCharacters
"Prints stringToCenter centered in a line of aNumberOfCharacters total"
| centerStart |
centerStart := aNumberOfCharacters - stringToCenter size // 2.
Line 6,831 ⟶ 6,857:
 
numberOfCharactersPerLine
"Returns the number of characters per line"
^66
 
numberOfCharactersPerMonth
"Returns the number of character per month"
^20
</lang>