Calendar - for "REAL" programmers: Difference between revisions

m
Line 875:
 
=={{header|Elena}}==
ELENA 3.4.x :
<lang elena>import system'text.;
import system'routines.;
import system'calendar.;
import extensions.;
import extensions'routines.;
 
const MonthNames = (new string[] {"JANUARY","FEBRUARY","MARCH","APRIL","MAY","JUNE","JULY","AUGUST","SEPTEMBER","OCTOBER","NOVEMBER","DECEMBER").};
const DayNames = (new string[] {"MO", "TU", "WE", "TH", "FR", "SA", "SU").};
 
class CalendarMonthPrinter
{
objectDate theDate.;
objectTextBuilder theLine.;
objectint theMonth.;
objectint theYear.;
objectref<int> theRow.;
constructor (year:aYear, month:aMonth)
[{
theMonth := aMonth.month;
theYear := aYear.year;
theLine := TextBuilder new. TextBuilder();
theRow := Integer new(0).;
]}
 
writeTitle()
[ {
theRow value.Value := 0.;
theDate := Date .new(theYear, theMonth, 1).;
DayNames .forEach(:aName(name)
[{ theLine .print(" ",aNamename) ]}
]}
writeLine()
[{
theLine clear.clear();
if (theDate month.Month == theMonth)
[{
theLine write:.writeCopies(" " length((,theDate dayOfWeek.DayOfWeek == 0)iif ? 7 : (7,theDate dayOfWeek).DayOfWeek - 1).);
[
repeatUntil:$(doUntil(theDate month.Month != theMonth) ||( theDate dayOfWeek.DayOfWeek == 1)).
].{
theLine.writePaddingLeft(theDate.Day.Printable, $32, 3);
[theDate := theDate.addDays:1
theLine write:(theDate day; literal) paddingLeft:3 with:$32. }
].};
theDate := theDate addDays:1.
]
repeatUntil:$((theDate month != theMonth)||(theDate dayOfWeek == 1)).
].
int aLengthlength := theLine length.Length;
if (aLengthlength < 21)
[{ theLine write:.writeCopies(" ", length(21 - aLengthlength). ].};
theRow .append(1).
]}
indexer() = BaseIndexer:new Indexer:
{
bool availableAvailable = theRow < 7.;
 
readIndexTo(ref< int> anIndexretVal) [{ theRow .readValueTo(anIndexref retVal) ]}
 
writeIndex(int anIndexindex)
[{
if (anIndexindex <= theRow)
[{ self .writeTitle() ].};
while (anIndexindex > theRow)
[{ self .writeLine() ]}
]}
 
appendIndex(int anIndexindex)
<= writeIndex(theRow value.Value + anIndexindex).;
readLengthTo(ref< int> retVal) [{ retVal value := 7 ]}
 
get() = self.;
set : (o) [{ NotSupportedException new; .raise() ]}
}.;
printTitleTo : anOutput(output)
[{
anOutput writeoutput.writePadding(MonthNames[theMonth - 1]) padding:21, with:$32., 21)
]}
printTo : anOutput(output)
[{
anOutput output.write(theLine literal).Value)
]}
}
 
class Calendar
{
int theYear.;
int theRowLength.;
constructor new(int : aYearyear)
[{
theYear := aYear.year;
theRowLength := 3.
]}
printTo:anOutput(output)
[{
anOutput write:output.writePadding("[SNOOPY]", $32, padding:(theRowLength * 25) with:$32.;
anOutput writeLineoutput. writeLine();
anOutput write:output.writePadding(theYear.Printable, literal)$32, padding:(theRowLength * 25) with:$32.;
anOutput writeLine; output.writeLine().writeLine();
var aRowCountrowCount := 12 / theRowLength.;
var Monthsmonths := Array new.allocate(aRowCountrowCount); .populate:(:i) =>
(Array new:.allocate(theRowLength;)
.populate(:(j) =>
( new CalendarMonthPrinter year:(theYear, month(i * theRowLength + j + 1))).;
Months months.forEach(:aRow(row)
[{
aRowvar r forEach(:aMonth)= row;
[
aMonth printTitleTo:anOutput.
anOutput write:" ".
].
anOutput writeLinerow.forEach:(month)
]. {
aMonth month.printTitleTo:anOutput. output;
theDate := theDate addDays:1.
anOutput output.write:" ".
};
ParallelEnumerator new:aRow; forEachoutput.writeLine(:aLine);
[
aLine forEachParallelEnumerator.new(row).forEach:aPrinter(line)
[{
aPrinter printTo:anOutputline.forEach:(printer)
anOutput write:" "{
] printer.printTo:output;
 
anOutput writeLine output.write:" "
]. };
 
].
output.writeLine()
]
}
] }
]}
}
 
public program()
{
[
var aCalendercalender := Calendar .new(console .write:"ENTER THE YEAR:"; readLineTo.readLine(Integer new)).toInt());
aCalender calender.printTo:console.;
console .readChar()
]}</lang>
{{out}}
<pre>
Anonymous user