/* Presidents.kt Copyright (c) Kari Laitinen http://www.naturalprogramming.com 2024-01-19 File created. This program shows a menu through which the user can get information related to the presidents of the U.S.A. There is a large array that contains President objects that contain data for each American president. Standard class LocalDate is used in this program to handle date-related information. */ import java.time.LocalDate import java.time.Period fun string_to_LocalDate( date_as_string : String ) : LocalDate { // This method accepts date strings in two formats: // // MM/DD/YYYY is the American format. // DD.MM.YYYY is the format used in Europe. // if ( date_as_string[ 2 ] == '/' ) { val day = Integer.parseInt( date_as_string.substring( 3, 5 ) ) val month = Integer.parseInt( date_as_string.substring( 0, 2 ) ) val year = Integer.parseInt( date_as_string.substring( 6, 10 ) ) return LocalDate.of( year, month, day ) } else { val day = Integer.parseInt( date_as_string.substring( 0, 2 ) ) val month = Integer.parseInt( date_as_string.substring( 3, 5 ) ) val year = Integer.parseInt( date_as_string.substring( 6, 10 ) ) return LocalDate.of( year, month, day ) } } class President( given_president_name : String, birth_date_as_string : String, given_birth_state : String, given_party_name : String, inauguration_date_as_string : String, last_day_in_office_as_string : String, given_vice_president_name : String ) { val president_name = given_president_name val birth_date_of_president = string_to_LocalDate( birth_date_as_string ) val birth_state_of_president = given_birth_state val party_name = given_party_name val inauguration_date = string_to_LocalDate( inauguration_date_as_string ) val last_day_in_office = string_to_LocalDate( last_day_in_office_as_string ) val vice_president_name = given_vice_president_name fun get_president_name() : String { return president_name ; } fun was_president_on( given_date : LocalDate ) : Boolean { if ( given_date.isEqual( inauguration_date ) || given_date.isEqual( last_day_in_office ) || ( given_date.isAfter( inauguration_date ) && given_date.isBefore( last_day_in_office ) ) ) { return true } else { return false } } fun get_brief_president_info() : String { return ( String.format( "\n %-25s", president_name ) + " president from " + inauguration_date + " to " + last_day_in_office ) } fun get_full_president_data() : String { val time_in_office = inauguration_date.until( last_day_in_office ) return ( "\n " + president_name + " born " + birth_date_of_president + ", " + birth_state_of_president + "\n Inauguration date : " + inauguration_date + "\n Last day in office : " + last_day_in_office + "\n Total time in office: " + time_in_office.years + " years, " + time_in_office.months + " months, and " + time_in_office.days + " days." + "\n Party: " + party_name + "\n Vice president(s): " + vice_president_name ) } } class PresidentInfoApplication() { var index_of_last_printing = -1 val SEARCH_NOT_READY = 1 val SEARCH_IS_READY = 2 val SEARCH_IS_SUCCESSFUL = 3 val SEARCH_NOT_SUCCESSFUL = 4 val president_table = arrayOf( President( "George Washington", "02/22/1732", "Virginia", "Federalist", "04/30/1789", "03/03/1797", "John Adams"), President("John Adams", "10/30/1735", "Massachusetts", "Federalist", "03/04/1797", "03/03/1801", "Thomas Jefferson"), President("Thomas Jefferson", "04/13/1743", "Virginia", "Dem.-Rep.", "03/04/1801", "03/03/1809", "Aaron Burr + George Clinton"), President("James Madison", "03/16/1751", "Virginia", "Dem.-Rep.", "03/04/1809", "03/03/1817", "George Clinton + Elbridge Gerry" ), President( "James Monroe", "04/28/1758", "Virginia", "Dem.-Rep.", "03/04/1817", "03/03/1825", "Daniel D. Tompkins" ), President( "John Quincy Adams", "07/11/1767", "Massachusetts", "Dem.-Rep.", "03/04/1825", "03/03/1829", "John C. Calhoun" ), President( "Andrew Jackson", "03/15/1767", "South Carolina","Democrat", "03/04/1829", "03/03/1837", "John C. Calhoun + Martin Van Buren" ), President( "Martin Van Buren", "12/05/1782", "New York", "Democrat", "03/04/1837", "03/03/1841", "Richard M. Johnson" ), President( "William Henry Harrison", "02/09/1773", "Virginia", "Whig", "03/04/1841", "04/04/1841", "John Tyler" ), President( "John Tyler", "03/29/1790", "Virginia", "Whig", "04/06/1841", "03/03/1845", "" ), President( "James Knox Polk", "11/02/1795", "North Carolina", "Democrat", "03/04/1845", "03/03/1849", "George M. Dallas" ), President( "Zachary Taylor", "11/24/1784", "Virginia", "Whig", "03/05/1849", "07/09/1850", "Millard Fillmore" ), President( "Millard Fillmore", "01/07/1800", "New York", "Whig", "07/10/1850", "03/03/1853", "" ), President( "Franklin Pierce", "11/23/1804", "New Hampshire", "Democrat", "03/04/1853", "03/03/1857", "William R. King" ), President( "James Buchanan", "04/23/1791", "Pennsylvania", "Democrat", "03/04/1857", "03/03/1861", "John C. Breckinridge"), President( "Abraham Lincoln", "02/12/1809", "Kentucky", "Republican", "03/04/1861", "04/15/1865", "Hannibal Hamlin + Andrew Johnson" ), President( "Andrew Johnson", "12/29/1808", "North Carolina", "Democrat", "04/15/1865", "03/03/1869", "" ), President( "Ulysses Simpson Grant", "04/27/1822", "Ohio", "Republican", "03/04/1869", "03/03/1877", "Schuyler Colfax + Henry Wilson" ), President( "Rutherford Birchard Hayes", "10/04/1822", "Ohio", "Republican", "03/04/1877", "03/03/1881", "William A. Wheeler"), President( "James Abram Garfield", "11/19/1831", "Ohio", "Republican", "03/04/1881", "09/19/1881", "Chester Alan Arthur"), President( "Chester Alan Arthur", "10/05/1829", "Vermont", "Republican", "09/20/1881", "03/03/1885", "" ), President( "Grover Cleveland", "03/18/1837", "New Jersey", "Democrat", "03/04/1885", "03/03/1889", "Thomas A. Hendrics" ), President( "Benjamin Harrison", "08/20/1933", "Ohio", "Republican", "03/04/1889", "03/03/1893", "Levi P. Morton" ), President( "Grover Cleveland", "03/18/1837", "New Jersey", "Democrat", "03/04/1893", "03/03/1897", "Adlai E. Stevenson" ), President( "William McKinley", "01/29/1843", "Ohio", "Republican", "03/04/1897", "09/14/1901", "Garret A. Hobart + Theodore Roosevelt" ), President( "Theodore Roosevelt", "10/27/1858", "New York", "Republican", "09/14/1901","03/03/1909","Charles W. Fairbanks"), President( "William Howard Taft", "09/15/1857", "Ohio", "Republican", "03/04/1909", "03/03/1913", "James S. Sherman"), President( "Woodrow Wilson", "12/28/1856", "Virginia", "Democrat", "03/04/1913", "03/03/1921", "Thomas R. Marshall" ), President( "Warren Gamaliel Harding", "11/02/1865", "Ohio", "Republican", "03/04/1921", "08/02/1923", "Calvin Coolidge" ), President( "Calvin Coolidge", "07/04/1872", "Vermont", "Republican", "08/03/1923", "03/03/1929", "Charles G. Dawes" ), President( "Herbert Clark Hoover", "08/10/1874", "Iowa", "Republican", "03/04/1929", "03/03/1933", "Charles Curtis" ), President( "Franklin Delano Roosevelt","01/30/1882","New York", "Democrat", "03/04/1933", "04/12/1945", "John N. Garner + Henry A. Wallace + Harry S. Truman" ), President( "Harry S. Truman", "05/08/1884", "Missouri", "Democrat", "04/12/1945", "01/20/1953", "Alben W. Barkley" ), President( "Dwight David Eisenhover", "10/14/1890", "Texas", "Republican","01/20/1953","01/20/1961","Richard Milhous Nixon"), President( "John Fitzgerald Kennedy", "05/29/1917", "Massachusetts", "Democrat", "01/20/1961", "11/22/1963", "Lyndon Baines Johnson" ), President( "Lyndon Baines Johnson", "08/27/1908", "Texas", "Democrat", "11/22/1963", "01/20/1969", "Hubert H. Humphrey"), President( "Richard Milhous Nixon", "01/09/1913", "California", "Republican", "01/20/1969", "08/09/1974", "Spiro T. Agnew + Gerald Rudolph Ford"), President( "Gerald Rudolph Ford", "07/14/1913", "Nebraska", "Republican","08/09/1974","01/20/1977","Nelson A. Rockefeller"), President( "Jimmy (James Earl) Carter", "10/01/1924", "Georgia", "Democrat", "01/20/1977", "01/20/1981", "Walter F. Mondale" ), President( "Ronald Wilson Reagan", "02/06/1911", "Illinois", "Republican", "01/20/1981", "01/20/1989", "George Bush" ), President( "George Bush", "06/12/1924", "Massachusetts", "Republican", "01/20/1989", "01/20/1993", "Dan Quayle" ), President( "Bill Clinton", "08/19/1946", "Arkansas", "Democrat", "01/20/1993", "01/20/2001", "Albert Gore" ), President( "George W. Bush", "07/06/1946", "Connecticut", "Republican", "01/20/2001", "01/20/2009", "Richard Cheney" ), President( "Barack Obama", "08/04/1961", "Hawaii", "Democrat", "01/20/2009", "01/20/2017", "Joe Biden" ) // It is your exercise to find data of Donald Trump and Joe Biden // and add those records to this array. // Wikipedia is a good source for this. ) fun search_president_by_name() { print( "\n Enter first, last, or full name of president: " ) val given_president_name = readLine()!! var president_index = 0 var array_search_status = SEARCH_NOT_READY while ( array_search_status == SEARCH_NOT_READY ) { if ( president_table[ president_index ].get_president_name() .contains( given_president_name ) ) { array_search_status = SEARCH_IS_SUCCESSFUL } else if ( president_index >= president_table.size - 1 ) { array_search_status = SEARCH_NOT_SUCCESSFUL } else { president_index ++ } } if ( array_search_status == SEARCH_IS_SUCCESSFUL ) { print( "\n\n THE #" + ( president_index + 1 ) + " PRESIDENT OF THE UNITED STATES: \n" + president_table[ president_index ].get_full_president_data() ) index_of_last_printing = president_index } else { print( "\n\n Sorry, could not find \"" + given_president_name + "\" in table.\n" ) } } fun search_president_for_given_date() { print( "\n Please, type in a date in form MM/DD/YYYY " + "\n Use two digits for days and months, and " + "\n four digits for year: " ) val date_as_string = readLine()!! val date_of_interest = string_to_LocalDate( date_as_string ) var president_index = 0 var array_search_status = SEARCH_NOT_READY while ( array_search_status == SEARCH_NOT_READY ) { if ( president_table[ president_index ].was_president_on( date_of_interest ) ) { array_search_status = SEARCH_IS_SUCCESSFUL } else if ( president_index >= president_table.size - 1) { array_search_status = SEARCH_NOT_SUCCESSFUL } else { president_index ++ ; } } if ( array_search_status == SEARCH_IS_SUCCESSFUL ) { print( "\n\n ON " + date_of_interest + ", THE PRESIDENT OF THE UNITED STATES WAS: \n" + president_table[ president_index ].get_full_president_data() ) index_of_last_printing = president_index } else { print( "\n\n Sorry, no president was on duty on " + date_of_interest + ".\n" ) } } fun print_data_of_next_president() { if ( index_of_last_printing < president_table.size - 1 ) { index_of_last_printing ++ print( "\n\n THE #" + ( index_of_last_printing + 1 ) + " PRESIDENT OF THE UNITED STATES: \n" + president_table[ index_of_last_printing ].get_full_president_data() ) } else { print( "\n Sorry, no more presidents in table." ) } } fun print_list_of_all_presidents() { var president_index = 0 while ( president_index < president_table.size ) { print( president_table[ president_index ].get_brief_president_info() ) president_index ++ ; if ( ( president_index % 15 ) == 0 ) { print( "\nPress to continue ....." ) val any_string_from_keyboard = readLine()!! } } } fun run() { var user_selection = "????" print( "\n This program provides information about all" + "\n presidents of the U.S.A. Please, select from" + "\n the following menu by typing in a letter. " ) while ( user_selection[ 0 ] != 'e' ) { print("\n\n p Search president by name." + "\n d Search president for a given date." + "\n n Print data of next president." + "\n a Print list of all presidents." + "\n e Exit the program.\n\n " ) user_selection = readLine()!! if ( user_selection[ 0 ] == 'p' ) { search_president_by_name() } else if ( user_selection[ 0 ] == 'd' ) { search_president_for_given_date() } else if ( user_selection[ 0 ] == 'n' ) { print_data_of_next_president() } else if ( user_selection[ 0 ] == 'a' ) { print_list_of_all_presidents() } } } } fun main() { val president_info_application = PresidentInfoApplication() president_info_application.run() }