// SoccerWorldCups.kt Copyright (c) Kari Laitinen // http://www.naturalprogramming.com // 2020-10-31 File created. class WorldCup( val year_when_played : Int, val host_country : String, val winning_country : String ) { fun print_world_cup_data() { print( String.format( "\n In %d, World Cup was played in %s. %s was the winner.", year_when_played, host_country, winning_country ) ) } } fun main() { // First we create an array that contains WorldCup objects. val all_world_cups = arrayOf( 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" ), WorldCup( 2018, "Russia", "France" ) ) 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. " ) 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 " ) selected_activity = readLine()!!.toLowerCase() 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: " ) val given_year = readLine()!!.toInt() val world_cups_of_given_year = all_world_cups.filter{ it.year_when_played == given_year } if ( world_cups_of_given_year.size == 0 ) { print( "\n Sorry, no Wold Cup was organized in " + given_year + ".\n" ) } else { world_cups_of_given_year[ 0 ].print_world_cup_data() } } else if ( selected_activity == "w" ) { print( "\n Give the name of a country: " ) val given_country_name = readLine()!!.toLowerCase().capitalize() val world_cups_with_certain_winner = all_world_cups.filter{ it.winning_country.contains( given_country_name ) } if ( world_cups_with_certain_winner.size == 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() } } } } print( "\n" ) }