// Olympics.swift Copyright (c) Kari Laitinen // http://www.naturalprogramming.com // 2014-10-02 File created. // 2015-10-17 Last modification. import Foundation /* This program demonstrates the use of an array of objects, or, more precisely, an array that contains references to objects. The program first introduces a class named Olympics. An Olympics object can contain the data of olympic games. By using the Olympics class, an array (Olympics table) is defined in the 'main' program. The array is used to search for data of olympic games. The end of data in the Olympics table is detected in a somewhat rude way. It will be your duty to improve this program so that the end of data will be detected in a more 'official' manner, i.e., there is a standard mechanism to detect how many objects are contained in an array. */ 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" ), Olympics( 9999, "end of", "data" ) ] 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()! )! var olympics_index = 0 var table_search_ready = false while table_search_ready == false { if olympics_table[ olympics_index ].get_year() == given_year { olympics_table[ olympics_index ].print_olympics_data() table_search_ready = true } else if olympics_table[ olympics_index ].get_year() == 9999 { print( "\n Sorry, no Olympic Games were held in " + String( given_year ) + ".\n" ) table_search_ready = true } else { olympics_index += 1 } }