Spaces:
Running
Running
TurkuBasicOOPinJava / Week 7: Enum, Generic Type, Streams, write to file, class diagram /08B. Print Players
| Note! This programming task can also be solved without using streams - as can actually all tasks related to streams. | |
| Thus, you are not forced to use streams in the tasks - however, it's advisable to 'try using streams at first'. | |
| In the program, the class 'HockeyPlayer' is defined. | |
| Familiarize yourself with the properties of the class and 'write the class method' | |
| public static void print(ArrayList<HockeyPlayer> players) | |
| which uses a stream to print the details of the players in the list exactly as shown in the example below. | |
| The points should include both the player's assists and goals. | |
| The program prints: | |
| Michael Johnson, 21 points | |
| James Smith, 50 points | |
| Oliver Williams, 5 points | |
| import java.util.Random; | |
| import java.util.ArrayList; | |
| public class Test { | |
| public static void main(String[] args) { | |
| final Random r = new Random(); | |
| String[] fn = "Oliver Harry Michael George Leo Jack James William Noah".split(" "); | |
| String[] ln = "Johnson Smith Davis Miller Wilson Clark Thompson Lewis".split(" "); | |
| int count = r.nextInt(5) + 5; | |
| ArrayList<HockeyPlayer> list = new ArrayList<>(); | |
| for (int i = 0; i < count; i++) { | |
| HockeyPlayer hp = new HockeyPlayer(fn[r.nextInt(fn.length)] + " " + | |
| ln[r.nextInt(ln.length)], r.nextInt(30), r.nextInt(30)); | |
| System.out.println("Added to list " + hp); | |
| list.add(hp); | |
| } | |
| System.out.println(""); | |
| System.out.println("Printing:"); | |
| print(list); | |
| } | |
| //ADD | |
| public static void print(ArrayList<HockeyPlayer> players) { | |
| // approach 1 forEach - works | |
| // players.stream().forEach(player -> System.out.println(player.getName() + ", " + (player.getGoals() + player.getAssists()) + " points")); | |
| // approach 2 for - works | |
| for (int i = 0; i < players.size(); i++) { | |
| System.out.println(players.get(i).getName() + ", " | |
| + (players.get(i).getGoals() + players.get(i).getAssists()) | |
| + " points"); | |
| } | |
| } | |
| } | |
| class HockeyPlayer { | |
| private String name; | |
| private int goals; | |
| private int assists; | |
| public HockeyPlayer(String name, int goals, int assists) { | |
| this.name = name; | |
| this.goals = goals; | |
| this.assists = assists; | |
| } | |
| public String getName() { | |
| return name; | |
| } | |
| public int getGoals() { | |
| return goals; | |
| } | |
| public int getAssists() { | |
| return assists; | |
| } | |
| public String toString() { | |
| return name + ", " + goals + " goals and " + assists + " assists."; | |
| } | |
| } | |
| Added to list James Miller, 10 goals and 1 assists. | |
| Added to list George Johnson, 14 goals and 18 assists. | |
| Added to list Noah Thompson, 18 goals and 13 assists. | |
| Added to list George Johnson, 18 goals and 20 assists. | |
| Added to list Leo Thompson, 26 goals and 18 assists. | |
| Added to list Leo Johnson, 10 goals and 29 assists. | |
| Added to list George Wilson, 14 goals and 28 assists. | |
| Added to list Leo Miller, 26 goals and 11 assists. | |
| Printing: | |
| James Miller, 11 points | |
| George Johnson, 32 points | |
| Noah Thompson, 31 points | |
| George Johnson, 38 points | |
| Leo Thompson, 44 points | |
| Leo Johnson, 39 points | |
| George Wilson, 42 points | |
| Leo Miller, 37 points | |