// SoccerWorldCups.swift Copyright (c) Kari Laitinen // http://www.naturalprogramming.com // 2015-09-02 File created. // 2016-11-09 Last modification. import Foundation class WorldCup { let year_when_played : Int let host_country : String let winning_country : String init( _ given_year : Int, _ given_host_country : String, _ given_winning_country : String ) { year_when_played = given_year host_country = given_host_country winning_country = given_winning_country } func get_year() -> Int { return year_when_played } func get_host_country() -> String { return host_country } func get_winning_country() -> String { return winning_country } func print_world_cup_data() { print( String( format: "\n In %d World Cup was played in %@. %@ was the winner.", year_when_played, host_country, winning_country ) ) } } // The main program begins. // First we create an array that contains WorldCup objects. let all_world_cups = [ WorldCup( 1930, "Uruguay", "Uruguay" ), WorldCup( 1934, "Italy", "Italy" ), WorldCup( 1938, "France", "Italy" ), WorldCup( 1950, "Brazil", "Uruguay" ), WorldCup( 1954, "Switzerland", "West Germany" ), WorldCup( 1958, "Sweden", "Brazil" ), WorldCup( 1962, "Chile", "Brazil" ), WorldCup( 1966, "England", "England" ), WorldCup( 1970, "Mexico", "Brazil" ), WorldCup( 1974, "West Germany", "West Germany" ), WorldCup( 1978, "Argentina", "Argentina" ), WorldCup( 1982, "Spain", "Italy" ), WorldCup( 1986, "Mexico", "Argentina" ), WorldCup( 1990, "Italy", "West Germany" ), WorldCup( 1994, "United States", "Brazil" ), WorldCup( 1998, "France", "France" ), WorldCup( 2002, "South Korea & Japan", "Brazil" ), WorldCup( 2006, "Germany", "Italy" ), WorldCup( 2010, "South Africa", "Spain" ), WorldCup( 2014, "Brazil", "Germany" ) ] var selected_activity = "????" print( "\n This program can provide information about" + "\n Soccer World Cups. Please, select from" + "\n the following menu by typing in a letter. ", terminator: "" ) while selected_activity != "e" { print("\n\n a Print data of all world cups." + "\n y Find record of certain year." + "\n w Find records of certain winning country." + "\n e Exit the program.\n\n ", terminator: "" ) selected_activity = readLine()! if selected_activity == "a" { // The method print_world_cup_data() will be called for each // WorldCup object in the array. for world_cup in all_world_cups { world_cup.print_world_cup_data() } } else if selected_activity == "y" { print( "\n Give a year using four digits: ", terminator: "" ) var given_year = Int( readLine()! )! var world_cups_of_given_year = all_world_cups.filter{ $0.get_year() == given_year } for world_cup in world_cups_of_given_year { world_cup.print_world_cup_data() } } else if selected_activity == "w" { print( "\n Give the name of a country: ", terminator: "" ) var given_country_name = readLine()! var world_cups_with_certain_winner = all_world_cups.filter{ $0.get_winning_country().range( of: given_country_name ) != nil } if ( world_cups_with_certain_winner.count == 0 ) { print( "\n " + given_country_name + " has never won a World Cup." ) } else { for world_cup in world_cups_with_certain_winner { world_cup.print_world_cup_data() } } } }