// OlympicsFiltered.kt Copyright (c) Kari Laitinen // http://www.naturalprogramming.com // 2020-10-31 File created. /* This program works in the same way as Olympics.kt. In this version the way how Olympics objects are searched is programmed by using the Array method filter() which takes a so-called lambda expression as a parameter. */ class Olympics( val olympic_year : Int, val olympic_city : String, val olympic_country : String ) { fun print_olympics_data() { print( String.format( "\n In %d, Olympic Games were held in %s, %s.\n\n", olympic_year, olympic_city, olympic_country ) ) } } fun main() { // First we create an array that contains Olympics objects. // Later on the program will go through this array searching // for given year. val olympics_table = arrayOf( Olympics( 1896, "Athens", "Greece" ), Olympics( 1900, "Paris", "France" ), Olympics( 1904, "St. Louis", "U.S.A." ), Olympics( 1906, "Athens", "Greece" ), Olympics( 1908, "London", "Great Britain"), Olympics( 1912, "Stockholm","Sweden" ), Olympics( 1920, "Antwerp", "Belgium" ), Olympics( 1924, "Paris", "France" ), Olympics( 1928, "Amsterdam","Netherlands"), Olympics( 1932, "Los Angeles", "U.S.A."), Olympics( 1936, "Berlin", "Germany" ), Olympics( 1948, "London", "Great Britain"), Olympics( 1952, "Helsinki","Finland" ), Olympics( 1956, "Melbourne","Australia" ), Olympics( 1960, "Rome", "Italy" ), Olympics( 1964, "Tokyo", "Japan" ), Olympics( 1968, "Mexico City","Mexico" ), Olympics( 1972, "Munich", "West Germany"), Olympics( 1976, "Montreal", "Canada" ), Olympics( 1980, "Moscow", "Soviet Union"), Olympics( 1984, "Los Angeles","U.S.A."), Olympics( 1988, "Seoul", "South Korea"), Olympics( 1992, "Barcelona","Spain" ), Olympics( 1996, "Atlanta", "U.S.A." ), Olympics( 2000, "Sydney", "Australia" ), Olympics( 2004, "Athens", "Greece" ), Olympics( 2008, "Beijing", "China" ), Olympics( 2012, "London", "Great Britain"), Olympics( 2016, "Rio de Janeiro", "Brazil" ) ) print( "\n This program can tell where the Olympic " + "\n Games were held in a given year. Give " + "\n a year by using four digits: " ) val given_year = readLine()!!.toInt() // 'it' is a special identifier in Kotlin. Used with Lambda expressions. val olympics_in_given_year = olympics_table.filter{ it.olympic_year == given_year } if ( olympics_in_given_year.size == 0 ) { print( "\n Sorry, no Olympic Games were held in " + given_year + ".\n" ) } else { olympics_in_given_year[ 0 ].print_olympics_data() } print( "\n" ) }