Skip to content
Snippets Groups Projects
Verified Commit 2ae6b063 authored by Jan Kožusznik's avatar Jan Kožusznik
Browse files

Preparo for lab 13

parent a7b1e813
Branches
No related merge requests found
package cz.jezek.lab11;
public class SortedSetMain {
public static void main(String[] args) {
Tournament tournament = Tournament.generate();
}
}
package cz.jezek.lab11;
public class StreamMain {
public static void main(String[] args) {
Tournament tournament = Tournament.generate();
printStatAboutTournament(tournament);
saveTournament(tournament);
loadTournament();
}
public static void printStatAboutTournament(Tournament tournament) {
System.out.println(tournament);
Player player = tournament.getRandomPlayer();
System.out.println(player);
System.out.print("Number of matches: ");
//TODO 3.a Show the number of matches played by a given player (using Streams)
System.out.print("Goals scored: ");
//TODO 3.b Show the number of goals scored by a given player (help Streams)
Oponents oponents = new Oponents(tournament.getRandomPlayer(),
tournament.getRandomPlayer());
System.out.println("Mutual statistics of oponents:" + oponents);
System.out.print("Matches: ");
//TODO 4.a List the matches played between the given players (using the stream)
System.out.print("Number of mathes: ");
//TODO 4.b Print the number of matches played between the given players (using the stream)
System.out.print("Match with the largest total number:");
//TODO 4.c Show match with the largest total number (sum of both players) scored goals between relevant players (help Streams)
}
public static void loadTournament() {
//TODO 5.b Load a serialized tournament from a file
}
public static void saveTournament(Tournament tournament) {
//TODO 5.a Save the tournament to a file using object serialization.
}
}
package cz.jezek.lab11;
import java.util.Collections;
import static cz.jezek.lab11.RandomGenarator.random;
import static cz.jezek.lab11.RandomGenarator.selectRandom;
import static java.util.stream.Collectors.toList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Tournament {
......@@ -10,11 +15,11 @@ public class Tournament {
public static Tournament generate() {
// TODO 1.a Generate a list of 10 random players using the stream and then filter so that it does not contain players with the same name
List<Player> players = Collections.emptyList();
List<Player> players = Stream.generate(Player::generateRandom).distinct().limit(10).collect(Collectors.toList());
// TODO 1.b Use the stream to generate a list of 50 matches between random players (from the list of players) with a random result. Make sure the player does not play with himself.
List<Match> matches = Collections.emptyList();
List<Match> matches = Stream.generate(() -> generateRandom(players)).filter(Tournament::differentOponents)
.limit(500).collect(toList());
return new Tournament(players, matches);
}
......@@ -35,7 +40,7 @@ public class Tournament {
public Player getRandomPlayer() {
return RandomGenarator.selectRandom(players);
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
......@@ -54,4 +59,13 @@ public class Tournament {
return builder.toString();
}
private static boolean differentOponents(Match m) {
return m.getOponents().getPlayer1() != m.getOponents().getPlayer2();
}
private static Match generateRandom(List<Player> players) {
Match result = new Match(selectRandom(players), random.nextInt(11), selectRandom(players), random.nextInt(11));
return result;
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment