// MonthCalendars.kt Copyright (c) Kari Laitinen // http://www.naturalprogramming.com // 2024-01-20 File created. /* This program can print month calendars to a Terminal or Command Prompt window. There is a menu through which you can select printing operations. Standard class LocalDate is used to handle date information. Class EnglishCalendar is marked 'open' so that a new class can be derived from it. */ import java.time.LocalDate import java.time.temporal.WeekFields open class EnglishCalendar( var this_year : Int, var this_month : Int ) { var names_of_months = arrayOf( "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ) var week_description = " Week Mon Tue Wed Thu Fri Sat Sun" fun get_calendar_year() : Int { return this_year } fun get_calendar_month() : Int { return this_month } fun increment_calendar_month() { this_month ++ if ( this_month > 12 ) { this_month = 1 this_year ++ } } fun print() { var a_day_in_this_month = LocalDate.of( this_year, this_month, 1 ) ; // Days of week are numbered from 1 to 7. // The first day of week is Monday. var day_of_week_value = 1 var day_of_week_of_first_day = a_day_in_this_month.getDayOfWeek().getValue() print( "\n\n " + names_of_months[ this_month - 1 ] + " " + this_year + "\n\n" + week_description + "\n\n" ) print( String.format( "%4d ", a_day_in_this_month.get( WeekFields.ISO.weekOfWeekBasedYear() ) ) ) // The first week of a month is often an incomplete week, // i.e., the first part of week belongs to the previous // month. In place of the days that belong to the previous // month we print just spaces. while ( day_of_week_value != day_of_week_of_first_day ) { print( " " ) day_of_week_value ++ } while ( this_month == a_day_in_this_month.getMonthValue() ) { if ( day_of_week_value >= 8 ) { print( String.format( "\n%4d ", a_day_in_this_month.get( WeekFields.ISO.weekOfWeekBasedYear() ) ) ) day_of_week_value = 1 } print( String.format( "%5d", a_day_in_this_month.getDayOfMonth() ) ) a_day_in_this_month = a_day_in_this_month.plusDays( 1 ) day_of_week_value ++ } System.out.print( "\n" ) } } // SpanishCalendar will inherit from EnglishCalendar. class SpanishCalendar( given_year : Int, given_month : Int ) : EnglishCalendar( given_year, given_month ) { init { names_of_months = arrayOf( "Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre" ) week_description = "Semana Lun Mar Mie Jue Vie Sab Dom" } } fun main() { val a_spanish_calendar = SpanishCalendar( 2023, 12 ) a_spanish_calendar.print() var calendar_to_print = EnglishCalendar( 2024, 1 ) calendar_to_print.print() var user_selection = "????" print( "\n This program prints calendars. Please, select from" + "\n the following menu by typing in a letter. " ) while ( user_selection[ 0 ] != 'e' ) { print( "\n\n p Print current calendar." + "\n n Print next calendar." + "\n s Switch to Spanish calendars." + "\n e Exit the program.\n\n " ) user_selection = readLine()!! if ( user_selection[ 0 ] == 'p' ) { calendar_to_print.print() } else if ( user_selection[ 0 ] == 'n' ) { calendar_to_print.increment_calendar_month() calendar_to_print.print() } else if ( user_selection[ 0 ] == 's' ) { // We'll take the calendar year and month from the old calendar // object and use them to create a SpanishCalendar object. calendar_to_print = SpanishCalendar( calendar_to_print.get_calendar_year(), calendar_to_print.get_calendar_month() ) calendar_to_print.print() } } }