// OlympicsFiltered.swift Copyright (c) Kari Laitinen // http://www.naturalprogramming.com // 2015-05-22 File created. // 2018-03-17 Last modification. import Foundation /* This program works in the same way as Olympics.swift. In this version the way how Olympics objects are searched is programmed by using the Array method filter() which takes a so-called closure as a parameter. */ class Olympics { let olympic_year : Int let olympic_city : String let olympic_country : String init( _ given_olympic_year : Int, _ given_olympic_city : String, _ given_olympic_country : String ) { olympic_year = given_olympic_year olympic_city = given_olympic_city olympic_country = given_olympic_country } func get_year() -> Int { return olympic_year } func print_olympics_data() { print( String( format: "\n In %d, Olympic Games were held in %@, %@.\n", olympic_year, olympic_city, olympic_country ) ) } } // The main program begins. // First we create an array that contains Olympics objects. // Later on the program will go through this array searching // for given year. let olympics_table = [ 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: ", terminator: "" ) let given_year = Int( readLine()! )! // The following statement creates an array that contains // all Olympics objects in which the olympic year matches // the given year. In practice the array will be empty // or it will contain a single element. The expression // inside the braces is a closure. let olympics_in_given_year = olympics_table.filter { $0.get_year() == given_year } if olympics_in_given_year.isEmpty { print( "\n Sorry, no Olympic Games were held in " + String( given_year ) + ".\n" ) } else { olympics_in_given_year[ 0 ].print_olympics_data() } /*** // The following is another possibility to find an Olympics object. // This approach works well when there is only one Olympics object // for each year. if let index = olympics_table.index( where: { $0.get_year() == given_year } ) { olympics_table[ index ].print_olympics_data() } else { print( "\n Sorry, no Olympic Games were held in " + String( given_year ) + ".\n" ) } */