// SoccerWorldCups.java by Kari Laitinen // http://www.naturalprogramming.com // 2014-11-09 File created. // 2023-01-06 New WorldCup objects were added. /* To compile and run this program, you need JDK 8 or a later version. This program is an example that contains information about soccer World Cups. This information is stored as WorldCup objects in an ArrayList array. The user of the program can filter some of this information. This program demonstrates - conversion of an ArrayList to a Stream - filtering objects in the Stream with filter() method and a lambda expression - calling the forEach() method for every filtered object */ import java.util.* ; class WorldCup { int year_when_played ; String host_country ; String winning_country ; public WorldCup( int given_year, String given_host_country, String given_winning_country ) { year_when_played = given_year ; host_country = given_host_country ; winning_country = given_winning_country ; } public int get_year() { return year_when_played ; } public String get_host_country() { return host_country ; } public String get_winning_country() { return winning_country ; } public void print_world_cup_data() { System.out.print( "\n In " + year_when_played + ", World Cup was played in " + host_country + ". " + winning_country + " was the winner.\n" ) ; } } public class SoccerWorldCups { public static void main( String... not_in_use ) { Scanner keyboard = new Scanner( System.in ) ; // First, we'll create a traditional array of WorldCup objects. WorldCup[] array_of_world_cups = { new WorldCup( 1930, "Uruguay", "Uruguay" ), new WorldCup( 1934, "Italy", "Italy" ), new WorldCup( 1938, "France", "Italy" ), new WorldCup( 1950, "Brazil", "Uruguay" ), new WorldCup( 1954, "Switzerland", "West Germany" ), new WorldCup( 1958, "Sweden", "Brazil" ), new WorldCup( 1962, "Chile", "Brazil" ), new WorldCup( 1966, "England", "England" ), new WorldCup( 1970, "Mexico", "Brazil" ), new WorldCup( 1974, "West Germany", "West Germany" ), new WorldCup( 1978, "Argentina", "Argentina" ), new WorldCup( 1982, "Spain", "Italy" ), new WorldCup( 1986, "Mexico", "Argentina" ), new WorldCup( 1990, "Italy", "West Germany" ), new WorldCup( 1994, "United States", "Brazil" ), new WorldCup( 1998, "France", "France" ), new WorldCup( 2002, "South Korea & Japan", "Brazil" ), new WorldCup( 2006, "Germany", "Italy" ), new WorldCup( 2010, "South Africa", "Spain" ), new WorldCup( 2014, "Brazil", "Germany" ), new WorldCup( 2018, "Russia", "France" ), new WorldCup( 2022, "Qatar", "Argentina" ) } ; // Next we'll create an ArrayList-based array and add all WorldCup // objects to it. ArrayList all_world_cups = new ArrayList() ; Collections.addAll( all_world_cups, array_of_world_cups ) ; String selected_activity = "????" ; System.out.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.charAt( 0 ) != 'e' ) { System.out.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 = keyboard.nextLine().toLowerCase() ; if ( selected_activity.charAt( 0 ) == 'a' ) { // Here the ArrayList will be converted to a Stream and // the method print_world_cup_data() will be called for each // WorldCup object in the Stream. all_world_cups.stream() .forEach( world_cup -> world_cup.print_world_cup_data() ) ; } else if ( selected_activity.charAt( 0 ) == 'y' ) { System.out.print( "\n Give a year using four digits: " ) ; int given_year = Integer.parseInt( keyboard.nextLine() ) ; all_world_cups.stream() .filter( world_cup -> world_cup.get_year() == given_year ) .forEach( world_cup -> world_cup.print_world_cup_data() ) ; } else if ( selected_activity.charAt( 0 ) == 'w' ) { System.out.print( "\n Give the name of a country: " ) ; String given_country_name = keyboard.nextLine() ; Object[] world_cups_with_certain_winner = all_world_cups.stream() .filter( world_cup -> world_cup.get_winning_country().contains( given_country_name ) ) .toArray() ; if ( world_cups_with_certain_winner.length == 0 ) { System.out.print( "\n " + given_country_name + " has never won a World Cup. \n" ) ; } else { for ( Object world_cup : world_cups_with_certain_winner ) { ((WorldCup) world_cup).print_world_cup_data() ; } } } } } } /*** // This is another possible implementation for the last // part of this program. ArrayList world_cups_with_certain_winner = new ArrayList<>() ; all_world_cups.stream() .filter( world_cup -> world_cup.get_winning_country().contains( given_country_name ) ) .forEach( world_cup -> world_cups_with_certain_winner.add( world_cup ) ) ; if ( world_cups_with_certain_winner.size() == 0 ) { System.out.print( "\n " + given_country_name + " has never won a World Cup. \n" ) ; } else { for ( WorldCup world_cup : world_cups_with_certain_winner ) { world_cup.print_world_cup_data() ; } } ***/