Calendar: Difference between revisions

5,838 bytes added ,  5 years ago
Implementation of Calendar in Smalltalk
(Added Prolog implementation)
(Implementation of Calendar in Smalltalk)
Line 6,721:
30
 
</pre>
=={{header|Smalltalk}}==
 
This implementation has been developed using '''Cuis Smalltalk''' [https://github.com/Cuis-Smalltalk/Cuis-Smalltalk-Dev] with '''Aconcagua''' loaded.
To run it evaluate: <lang smalltalk>CalendarPrinter printOnTranscriptForYearNumber: 1969</lang>
<lang Smalltalk>
Object subclass: #CalendarPrinter
instanceVariableNames: 'yearToPrint stream monthsOfYear currentMonths monthOfYearCurrentDate'
classVariableNames: ''
poolDictionaries: ''
category: 'CalendarPrinter'
 
CalendarPrinter class>>printOnTranscriptForYearNumber: aYearNumber
(self for: (GregorianYear number: aYearNumber) on: Transcript) value
CalendarPrinter class>>for: aYear on: aStream
^self new initializeFor: aYear on: aStream
initializeFor: aYear on: aStream
yearToPrint := aYear.
stream := aStream.
monthsOfYear := yearToPrint months.
monthOfYearCurrentDate := monthsOfYear collect: [ :aMonthOfYear | aMonthOfYear firstDate ].
 
value
self
printHeader;
printMonths.
 
printHeader
self center: yearToPrint number printString in: self numberOfCharactersPerLine.
stream newLine; newLine.
 
printMonths
(January to: December by: 3*month) do: [ :aMonth | self printMonthsStartingOn: aMonth ].
 
printMonthsStartingOn: aMonth
currentMonths := aMonth to: aMonth next next.
self
printMonthsHeader;
printDaysOfWeekHeader;
printDayNumbers.
 
printMonthsHeader
currentMonths
do: [ :currentMonth | self center: currentMonth printString in: self numberOfCharactersPerMonth ]
separatedBy: [ stream space: 3 ].
stream newLine.
 
printDaysOfWeekHeader
currentMonths
do: [ :currentMonth | self printOneMonthDaysOfWeekHeader ]
separatedBy: [ stream space: 3 ].
stream newLine.
 
printOneMonthDaysOfWeekHeader
(Sunday to: Saturday)
do: [ :aDayOfWeek | stream nextPutAll: (aDayOfWeek printString first: 2) ]
separatedBy: [ stream space ]
 
printDayNumbers
[self hasDayNumbersToPrint] whileTrue: [ self printDayNumbersRow ].
 
hasDayNumbersToPrint
^currentMonths anySatisfy: [ :currentMonth | self isCurrentDateAtSameMonthOfYearAs: currentMonth ]
isCurrentDateAtSameMonthOfYearAs: currentMonth
^self is: (self currentDateOf: currentMonth) atSameMonthOfYearAs: currentMonth
 
is: aDate atSameMonthOfYearAs: currentMonth
^aDate monthOfYear = (monthsOfYear at: currentMonth number)
 
printDayNumbersRow
currentMonths
do: [ :currentMonth | self printDayNumbersRowOf: currentMonth ]
separatedBy: [ stream space: 3 ].
stream newLine
 
printDayNumbersRowOf: currentMonth
(Sunday to: Saturday)
do: [ :aDayOfWeek | self printDayNumberOf: currentMonth for: aDayOfWeek ]
separatedBy: [ stream space ]
 
printDayNumberOf: currentMonth for: aDayOfWeek
| currentDate |
currentDate := self currentDateOf: currentMonth.
(self hasToPrint: currentDate of: currentMonth for: aDayOfWeek)
ifTrue: [
currentDate dayNumber printOn: stream length: 2 zeroPadded: false.
self calculateNextCurrentDateOf: currentMonth ]
ifFalse: [ stream space: 2 ]! !
 
hasToPrint: aDate of: aCurrentMonth for: aDayOfWeek
^(self is: aDate atSameMonthOfYearAs: aCurrentMonth) and: [aDate day = aDayOfWeek]
 
currentDateOf: currentMonth
^monthOfYearCurrentDate at: currentMonth number
 
calculateNextCurrentDateOf: currentMonth
monthOfYearCurrentDate at: currentMonth number put: (self currentDateOf: currentMonth) next
 
center: stringToCenter in: aNumberOfCharacters
| centerStart |
centerStart := aNumberOfCharacters - stringToCenter size // 2.
stream
space: centerStart;
nextPutAll: stringToCenter;
space: aNumberOfCharacters - centerStart - stringToCenter size
 
numberOfCharactersPerLine
^66
 
numberOfCharactersPerMonth
^20
</lang>
{{out}}
<pre>
1969
January February March
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 1 1
5 6 7 8 9 10 11 2 3 4 5 6 7 8 2 3 4 5 6 7 8
12 13 14 15 16 17 18 9 10 11 12 13 14 15 9 10 11 12 13 14 15
19 20 21 22 23 24 25 16 17 18 19 20 21 22 16 17 18 19 20 21 22
26 27 28 29 30 31 23 24 25 26 27 28 23 24 25 26 27 28 29
30 31
April May June
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 5 1 2 3 1 2 3 4 5 6 7
6 7 8 9 10 11 12 4 5 6 7 8 9 10 8 9 10 11 12 13 14
13 14 15 16 17 18 19 11 12 13 14 15 16 17 15 16 17 18 19 20 21
20 21 22 23 24 25 26 18 19 20 21 22 23 24 22 23 24 25 26 27 28
27 28 29 30 25 26 27 28 29 30 31 29 30
July August September
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 5 1 2 1 2 3 4 5 6
6 7 8 9 10 11 12 3 4 5 6 7 8 9 7 8 9 10 11 12 13
13 14 15 16 17 18 19 10 11 12 13 14 15 16 14 15 16 17 18 19 20
20 21 22 23 24 25 26 17 18 19 20 21 22 23 21 22 23 24 25 26 27
27 28 29 30 31 24 25 26 27 28 29 30 28 29 30
31
October November December
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 1 1 2 3 4 5 6
5 6 7 8 9 10 11 2 3 4 5 6 7 8 7 8 9 10 11 12 13
12 13 14 15 16 17 18 9 10 11 12 13 14 15 14 15 16 17 18 19 20
19 20 21 22 23 24 25 16 17 18 19 20 21 22 21 22 23 24 25 26 27
26 27 28 29 30 31 23 24 25 26 27 28 29 28 29 30 31
30
</pre>