Skip to content
Snippets Groups Projects
Commit 4e462219 authored by koz01's avatar koz01
Browse files

solution

parent 03bf36ce
No related merge requests found
......@@ -4,3 +4,4 @@
.classpath
.DS_Store
/tournament.obj
package cz.jezek.lab11;
import java.io.Serializable;
import java.util.Objects;
public class Match {
public class Match implements Serializable {
private static final long serialVersionUID = 1L;
private Oponents oponents;
private int player1Scorel;
private int player2Scorel;
......
package cz.jezek.lab11;
import java.io.Serializable;
import java.util.Objects;
public class Oponents {
public class Oponents implements Serializable{
private static final long serialVersionUID = 1L;
private Player player1;
private Player player2;
public Oponents(Player player1, Player player2) {
super();
this.player1 = player1;
this.player2 = player2;
}
......
package cz.jezek.lab11;
public class Player {
import java.io.Serializable;
public class Player implements Serializable{
private static final long serialVersionUID = 1L;
private String firstName;
private String lastName;
......
package cz.jezek.lab11;
import static java.lang.System.out;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
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.
}
public static void main(String[] args) {
Tournament tournament = loadTournament();
printStatAboutTournament(tournament);
saveTournament(tournament);
}
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)
out.println(tournament.getMatches().stream().filter(m -> m.getOponents().contains(player)).count());
System.out.print("Goals scored: ");
// TODO 3.b Show the number of goals scored by a given player (help Streams)
// IntStream use mapToInt
// sum()
out.println(tournament.getMatches().stream().filter(m -> m.getOponents().contains(player))
.mapToInt(m -> m.getScoreForPlayer(player)).sum());
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.println(tournament.getMatches().stream().filter(m -> m.getOponents().equals(oponents)).toList());
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)
System.out.println(tournament.getMatches().stream().filter(m -> m.getOponents().equals(oponents))
.mapToInt(m -> m.getPlayer1Scorel() + m.getPlayer2Scorel()).max().orElse(0));
}
public static Tournament loadTournament() {
// TODO 5.b Load a serialized tournament from a file
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(("tournament.obj")))) {
return (Tournament) ois.readObject();
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (ClassNotFoundException e) {
e.printStackTrace();
return null;
}
}
public static void saveTournament(Tournament tournament) {
try (ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("tournament.obj"))) {
os.writeObject(tournament);
} catch (IOException e) {
e.printStackTrace();
}
}
}
package cz.jezek.lab11;
import java.util.Collections;
import java.util.List;
public class Tournament {
private List<Player> players;
private List<Match> matches;
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();
// 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();
return new Tournament(players, matches);
}
public Tournament(List<Player> players, List<Match> matches) {
this.players = players;
this.matches = matches;
}
import static cz.jezek.lab11.RandomGenerator.random;
import static cz.jezek.lab11.RandomGenerator.selectRandom;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
public List<Player> getPlayers() {
return players;
}
public List<Match> getMatches() {
return matches;
}
public Player getRandomPlayer() {
return RandomGenerator.selectRandom(players);
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Player count ").append(players.size())
.append(" Match count: ").append(matches.size());
builder.append("\n");
builder.append("\n\nPlayers:\n");
// TODO 2.a Use the stream to add a listing of all players to the builder. Individual players are separated by a "\ n" character
builder.append(/* list of all players */"");
builder.append("\n\nMatches:\n");
// TODO 2.b Use the stream to add a listing of all entries to the builder. Individual matches are separated by a "\ n" character
builder.append(/* list of all matches */"");
return builder.toString();
}
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Stream;
public class Tournament implements Serializable {
private static final long serialVersionUID = 1L;
private List<Player> players;
private List<Match> matches;
private transient LocalDateTime dateTime;
public LocalDateTime getDateTime() {
return dateTime;
}
public void setDateTime(LocalDateTime dateTime) {
this.dateTime = dateTime;
}
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 = Stream.generate(Player::generateRandom).distinct().limit(10).collect(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 = Stream.generate(() -> generateRandomMatch(players))
.filter(m -> m.getOponents().getPlayer1() != m.getOponents().getPlayer2()).limit(150).toList();
return new Tournament(players, matches);
}
private static Match generateRandomMatch(List<Player> players) {
return new Match(selectRandom(players), random.nextInt(10), selectRandom(players), random.nextInt(10));
}
public Tournament(List<Player> players, List<Match> matches) {
this.players = players;
this.matches = matches;
}
public List<Player> getPlayers() {
return players;
}
public List<Match> getMatches() {
return matches;
}
public Player getRandomPlayer() {
return RandomGenerator.selectRandom(players);
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Player count ").append(players.size()).append(" Match count: ").append(matches.size());
builder.append("\n");
builder.append("\n\nPlayers:\n");
// TODO 2.a Use the stream to add a listing of all players to the builder.
// Individual players are separated by a "\n" character
builder.append(players.stream().map(Object::toString).collect(joining("\n")));
builder.append("\n\nMatches:\n");
// TODO 2.b Use the stream to add a listing of all entries to the builder.
// Individual matches are separated by a "\n" character
builder.append(matches.stream().map(Object::toString).collect(joining("\n")));
return builder.toString();
}
}
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