Spaces:
Running
Running
forEach
Browse files
Week 7: Enum, Generic Type, Streams, write to file, class diagram/08B. Print Players
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Note! This programming task can also be solved without using streams - as can actually all tasks related to streams.
|
| 2 |
+
Thus, you are not forced to use streams in the tasks - however, it's advisable to 'try using streams at first'.
|
| 3 |
+
|
| 4 |
+
In the program, the class 'HockeyPlayer' is defined.
|
| 5 |
+
Familiarize yourself with the properties of the class and 'write the class method'
|
| 6 |
+
public static void print(ArrayList<HockeyPlayer> players)
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
which uses a stream to print the details of the players in the list exactly as shown in the example below.
|
| 10 |
+
The points should include both the player's assists and goals.
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
The program prints:
|
| 14 |
+
Michael Johnson, 21 points
|
| 15 |
+
James Smith, 50 points
|
| 16 |
+
Oliver Williams, 5 points
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
import java.util.Random;
|
| 23 |
+
import java.util.ArrayList;
|
| 24 |
+
|
| 25 |
+
public class Test {
|
| 26 |
+
public static void main(String[] args) {
|
| 27 |
+
final Random r = new Random();
|
| 28 |
+
|
| 29 |
+
String[] fn = "Oliver Harry Michael George Leo Jack James William Noah".split(" ");
|
| 30 |
+
String[] ln = "Johnson Smith Davis Miller Wilson Clark Thompson Lewis".split(" ");
|
| 31 |
+
|
| 32 |
+
int count = r.nextInt(5) + 5;
|
| 33 |
+
ArrayList<HockeyPlayer> list = new ArrayList<>();
|
| 34 |
+
for (int i = 0; i < count; i++) {
|
| 35 |
+
HockeyPlayer hp = new HockeyPlayer(fn[r.nextInt(fn.length)] + " " +
|
| 36 |
+
ln[r.nextInt(ln.length)], r.nextInt(30), r.nextInt(30));
|
| 37 |
+
System.out.println("Added to list " + hp);
|
| 38 |
+
list.add(hp);
|
| 39 |
+
}
|
| 40 |
+
System.out.println("");
|
| 41 |
+
System.out.println("Printing:");
|
| 42 |
+
print(list);
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
//ADD
|
| 48 |
+
public static void print(ArrayList<HockeyPlayer> players) {
|
| 49 |
+
// approach 1 forEach - works
|
| 50 |
+
// players.stream().forEach(player -> System.out.println(player.getName() + ", " + (player.getGoals() + player.getAssists()) + " points"));
|
| 51 |
+
// approach 2 for - works
|
| 52 |
+
for (int i = 0; i < players.size(); i++) {
|
| 53 |
+
System.out.println(players.get(i).getName() + ", "
|
| 54 |
+
+ (players.get(i).getGoals() + players.get(i).getAssists())
|
| 55 |
+
+ " points");
|
| 56 |
+
}
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
class HockeyPlayer {
|
| 71 |
+
private String name;
|
| 72 |
+
private int goals;
|
| 73 |
+
private int assists;
|
| 74 |
+
|
| 75 |
+
public HockeyPlayer(String name, int goals, int assists) {
|
| 76 |
+
this.name = name;
|
| 77 |
+
this.goals = goals;
|
| 78 |
+
this.assists = assists;
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
public String getName() {
|
| 82 |
+
return name;
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
public int getGoals() {
|
| 86 |
+
return goals;
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
public int getAssists() {
|
| 90 |
+
return assists;
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
public String toString() {
|
| 94 |
+
return name + ", " + goals + " goals and " + assists + " assists.";
|
| 95 |
+
}
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
Added to list James Miller, 10 goals and 1 assists.
|
| 103 |
+
Added to list George Johnson, 14 goals and 18 assists.
|
| 104 |
+
Added to list Noah Thompson, 18 goals and 13 assists.
|
| 105 |
+
Added to list George Johnson, 18 goals and 20 assists.
|
| 106 |
+
Added to list Leo Thompson, 26 goals and 18 assists.
|
| 107 |
+
Added to list Leo Johnson, 10 goals and 29 assists.
|
| 108 |
+
Added to list George Wilson, 14 goals and 28 assists.
|
| 109 |
+
Added to list Leo Miller, 26 goals and 11 assists.
|
| 110 |
+
|
| 111 |
+
Printing:
|
| 112 |
+
James Miller, 11 points
|
| 113 |
+
George Johnson, 32 points
|
| 114 |
+
Noah Thompson, 31 points
|
| 115 |
+
George Johnson, 38 points
|
| 116 |
+
Leo Thompson, 44 points
|
| 117 |
+
Leo Johnson, 39 points
|
| 118 |
+
George Wilson, 42 points
|
| 119 |
+
Leo Miller, 37 points
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
|