// OlympicsDictionary.swift Copyright (c) Kari Laitinen // http://www.naturalprogramming.com // 2018-02-06 File created. import Foundation /* This is a modified version of program Olympics.swift In this version Olympics objects are stored in a dictionary, which makes finding the objects much more easier than in the original version of the program. */ 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. 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" ) ] // Next we create a dictionary into which we put // all the Olympics objects from the above array. var olympics_dictionary = [ Int : Olympics ] () for olympics in olympics_table { olympics_dictionary[ olympics.get_year() ] = olympics } 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()! )! // With an if let consctruct we can check whether an // Olympics object exists for the given year. if let olympics_in_given_year = olympics_dictionary[ given_year ] { olympics_in_given_year.print_olympics_data() } else { print( "\n Sorry, no Olympic Games were held in " + String( given_year ) + ".\n" ) } /** // Another possible way to create the dictionary would // be the following. let olympics_dictionary = [ 1896 : Olympics( 1896, "Athens", "Greece" ), 1900 : Olympics( 1900, "Paris", "France" ), 1904 : Olympics( 1904, "St. Louis", "U.S.A." ), 1906 : Olympics( 1906, "Athens", "Greece" ), 1908 : Olympics( 1908, "London", "Great Britain"), 1912 : Olympics( 1912, "Stockholm","Sweden" ), 1920 : Olympics( 1920, "Antwerp", "Belgium" ), 1924 : Olympics( 1924, "Paris", "France" ), 1928 : Olympics( 1928, "Amsterdam","Netherlands"), 1932 : Olympics( 1932, "Los Angeles", "U.S.A."), 1936 : Olympics( 1936, "Berlin", "Germany" ), 1948 : Olympics( 1948, "London", "Great Britain"), 1952 : Olympics( 1952, "Helsinki","Finland" ), 1956 : Olympics( 1956, "Melbourne","Australia" ), 1960 : Olympics( 1960, "Rome", "Italy" ), 1964 : Olympics( 1964, "Tokyo", "Japan" ), 1968 : Olympics( 1968, "Mexico City","Mexico" ), 1972 : Olympics( 1972, "Munich", "West Germany"), 1976 : Olympics( 1976, "Montreal", "Canada" ), 1980 : Olympics( 1980, "Moscow", "Soviet Union"), 1984 : Olympics( 1984, "Los Angeles","U.S.A."), 1988 : Olympics( 1988, "Seoul", "South Korea"), 1992 : Olympics( 1992, "Barcelona","Spain" ), 1996 : Olympics( 1996, "Atlanta", "U.S.A." ), 2000 : Olympics( 2000, "Sydney", "Australia" ), 2004 : Olympics( 2004, "Athens", "Greece" ), 2008 : Olympics( 2008, "Beijing", "China" ), 2012 : Olympics( 2012, "London", "Great Britain"), 2016 : Olympics( 2016, "Rio de Janeiro", "Brazil" ) ] **/