Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
No results found
Show changes
Commits on Source (3)
...@@ -43,5 +43,39 @@ ...@@ -43,5 +43,39 @@
<version>5.5.2</version> <version>5.5.2</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>3.0.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.activation/activation -->
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>3.0.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.eclipse.persistence/org.eclipse.persistence.moxy -->
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.moxy</artifactId>
<version>3.0.2</version>
</dependency>
</dependencies> </dependencies>
</project> </project>
package cz.jezek.lab11; package cz.jezek.lab11;
import java.io.Serializable;
import java.util.Objects; import java.util.Objects;
public class Match { import jakarta.xml.bind.annotation.XmlAttribute;
import jakarta.xml.bind.annotation.XmlElement;
public class Match implements Serializable{
private static final long serialVersionUID = 1L;
@XmlElement
private Oponents oponents; private Oponents oponents;
@XmlAttribute
private int player1Scorel; private int player1Scorel;
@XmlAttribute
private int player2Scorel; private int player2Scorel;
public Match() {
}
public Match(Player player1, int player1Scorel, Player player2, int player2Scorel) { public Match(Player player1, int player1Scorel, Player player2, int player2Scorel) {
this.oponents = new Oponents(player1, player2); this.oponents = new Oponents(player1, player2);
this.player1Scorel = player1Scorel; this.player1Scorel = player1Scorel;
...@@ -22,7 +38,7 @@ public class Match { ...@@ -22,7 +38,7 @@ public class Match {
} }
throw new IllegalArgumentException("Player is not participant of this match."); throw new IllegalArgumentException("Player is not participant of this match.");
} }
public Oponents getOponents() { public Oponents getOponents() {
return oponents; return oponents;
} }
...@@ -34,6 +50,10 @@ public class Match { ...@@ -34,6 +50,10 @@ public class Match {
public int getPlayer2Scorel() { public int getPlayer2Scorel() {
return player2Scorel; return player2Scorel;
} }
public int getTotalScore() {
return getPlayer1Scorel() + getPlayer2Scorel();
}
@Override @Override
public String toString() { public String toString() {
......
package cz.jezek.lab11; package cz.jezek.lab11;
import java.io.Serializable;
import java.util.Objects; import java.util.Objects;
public class Oponents { import jakarta.xml.bind.annotation.XmlElement;
public class Oponents implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@XmlElement
private Player player1; private Player player1;
@XmlElement
private Player player2; private Player player2;
public Oponents() {
}
public Oponents(Player player1, Player player2) { public Oponents(Player player1, Player player2) {
super(); super();
this.player1 = player1; this.player1 = player1;
......
package cz.jezek.lab11; 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 firstName;
private String lastName; private String lastName;
...@@ -10,6 +13,9 @@ public class Player { ...@@ -10,6 +13,9 @@ public class Player {
RandomGenarator.selectRandom(RandomGenarator.SURNAMES)); RandomGenarator.selectRandom(RandomGenarator.SURNAMES));
} }
public Player() {
}
public Player(String firstName, String lastName) { public Player(String firstName, String lastName) {
super(); super();
this.firstName = firstName; this.firstName = firstName;
......
package cz.jezek.lab11; package cz.jezek.lab11;
import static java.util.Comparator.comparingInt;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Arrays;
import java.util.Optional;
import org.eclipse.persistence.jaxb.MarshallerProperties;
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.Marshaller;
import jakarta.xml.bind.Unmarshaller;
public class StreamMain { public class StreamMain {
public static void main(String[] args) { public static void main(String[] args) {
Tournament tournament = Tournament.generate(); System.setProperty("jakarta.xml.bind.JAXBContextFactory", "org.eclipse.persistence.jaxb.JAXBContextFactory");
/*Tournament tournament = loadTournament();
if (tournament == null) {
tournament = Tournament.generate();
}
printStatAboutTournament(tournament); printStatAboutTournament(tournament);
saveTournament(tournament); saveTournament(tournament);
loadTournament(); printTournamentAsXML(tournament);
printTournamentAsJSON(tournament);
Tournament tournament2 = loadTournamentFromXML();
*/
Player p1 = new Player("Jonh", "Doe");
Player p2 = new Player("Marry", "Jane");
Match m = new Match(p1, 0, p2, 0);
Tournament t = new Tournament(Arrays.asList(p1,p2), Arrays.asList(m));
saveTournament(t);
Tournament t2 = loadTournament();
System.out.print("p1 == m.oponents.player1 " +
(t2.getPlayers().get(0) == t2.getMatches().get(0).getOponents().getPlayer1()));
printTournamentAsXML(t);
t2 = loadTournamentFromXML();
System.out.print("p1 == m.oponents.player1 " +
(t2.getPlayers().get(0) == t2.getMatches().get(0).getOponents().getPlayer1()));
} }
public static void printStatAboutTournament(Tournament tournament) { public static void printStatAboutTournament(Tournament tournament) {
System.out.println(tournament); System.out.println(tournament);
...@@ -18,9 +66,15 @@ public class StreamMain { ...@@ -18,9 +66,15 @@ public class StreamMain {
System.out.println(player); System.out.println(player);
System.out.print("Number of matches: "); System.out.print("Number of matches: ");
//TODO 3.a Show the number of matches played by a given player (using Streams) //TODO 3.a Show the number of matches played by a given player (using Streams)
System.out.println(tournament.getMatches().stream().filter(m -> m.getOponents().contains(player)).count());
System.out.print("Goals scored: "); System.out.print("Goals scored: ");
//TODO 3.b Show the number of goals scored by a given player (help Streams) //TODO 3.b Show the number of goals scored by a given player (help Streams)
System.out.println(tournament.getMatches().stream()
.filter(m -> m.getOponents().contains(player))
.mapToInt( m -> m.getScoreForPlayer(player))
.sum());
Oponents oponents = new Oponents(tournament.getRandomPlayer(), Oponents oponents = new Oponents(tournament.getRandomPlayer(),
tournament.getRandomPlayer()); tournament.getRandomPlayer());
...@@ -28,17 +82,83 @@ public class StreamMain { ...@@ -28,17 +82,83 @@ public class StreamMain {
System.out.print("Matches: "); System.out.print("Matches: ");
//TODO 4.a List the matches played between the given players (using the stream) //TODO 4.a List the matches played between the given players (using the stream)
tournament.getMatches().stream().filter(m -> m.getOponents().equals(oponents)).forEach(System.out::println);
System.out.print("Number of mathes: "); System.out.print("Number of mathes: ");
//TODO 4.b Print the number of matches played between the given players (using the stream) //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:"); 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) //TODO 4.c Show match with the largest total number (sum of both players) scored goals between relevant players (help Streams)
Optional<Match> maxMatch =
tournament.getMatches()
.stream()
.filter(m -> m.getOponents().equals(oponents))
//.reduce((a,b) -> a.getPlayer1Scorel() + a.getPlayer2Scorel() < b.getPlayer1Scorel() + b.getPlayer2Scorel() ? b : a);
.max(comparingInt(Match::getTotalScore));
System.out.println(maxMatch.map(Object::toString).orElse("N/A"));
} }
public static void loadTournament() {
//TODO 5.b Load a serialized tournament from a file
public static Tournament loadTournament() {
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("tournament.obj"))) {
return (Tournament) ois.readObject();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
return null;
} }
public static void saveTournament(Tournament tournament) { public static void saveTournament(Tournament tournament) {
//TODO 5.a Save the tournament to a file using object serialization. //TODO 5.a Save the tournament to a file using object serialization.
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("tournament.obj"))) {
oos.writeObject(tournament);
} catch ( IOException e) {
e.printStackTrace();
}
}
private static void printTournamentAsXML(Tournament tournament) {
try {
JAXBContext context = JAXBContext.newInstance(Tournament.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(tournament, System.out);
marshaller.marshal(tournament, new File("tournament.xml"));
} catch (JAXBException e) {
e.printStackTrace();
}
} }
private static void printTournamentAsJSON(Tournament tournament) {
try {
JAXBContext context = JAXBContext.newInstance(Tournament.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, true);
marshaller.marshal(tournament, System.out);
} catch (JAXBException e) {
e.printStackTrace();
}
}
private static Tournament loadTournamentFromXML() {
try {
JAXBContext context = JAXBContext.newInstance(Tournament.class);
Unmarshaller unmarshaller= context.createUnmarshaller();
return (Tournament) unmarshaller.unmarshal(new File("tournament.xml"));
} catch (JAXBException e) {
e.printStackTrace();
}
return null;
} }
}
package cz.jezek.lab11; package cz.jezek.lab11;
import java.util.Collections; import static cz.jezek.lab11.RandomGenarator.selectRandom;
import java.io.Serializable;
import java.util.List; import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import jakarta.xml.bind.annotation.XmlAttribute;
import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlElementWrapper;
import jakarta.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Tournament implements Serializable{
public class Tournament {
private static final long serialVersionUID = 0L;
@XmlElement(name = "player")
@XmlElementWrapper(name = "players")
private List<Player> players; private List<Player> players;
@XmlElement(name = "match")
@XmlElementWrapper(name = "matches")
private List<Match> matches; private List<Match> matches;
private String name = "turnaj";
private static Random rnd = new Random();
public static Match generateRandomMatch(List<Player> player) {
return new Match(selectRandom(player), rnd.nextInt(11), selectRandom(player), rnd.nextInt(11));
}
public static Tournament generate() { 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 // 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. // 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(() -> generateRandomMatch(players))
.filter(m -> m.getOponents().getPlayer1() != m.getOponents().getPlayer2())
.limit(30)
.collect(Collectors.toList());
return new Tournament(players, matches); return new Tournament(players, matches);
} }
public Tournament() {
}
public Tournament(List<Player> players, List<Match> matches) { public Tournament(List<Player> players, List<Match> matches) {
this.players = players; this.players = players;
this.matches = matches; this.matches = matches;
} }
public List<Player> getPlayers() { public List<Player> getPlayers() {
return players; return players;
} }
...@@ -45,11 +86,11 @@ public class Tournament { ...@@ -45,11 +86,11 @@ public class Tournament {
builder.append("\n\nPlayers:\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 // 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(players.stream().map(Object::toString).collect(Collectors.joining("\n")));
builder.append("\n\nMatches:\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 // 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 */""); builder.append(matches.stream().map(Object::toString).collect(Collectors.joining("\n")));
return builder.toString(); return builder.toString();
} }
......
module cz.jezek.lab10 { module cz.jezek.lab10 {
requires transitive javafx.controls; requires transitive javafx.controls;
requires javafx.fxml; requires javafx.fxml;
opens cz.jezek.lab11 to javafx.fxml; requires jakarta.xml.bind;
requires java.sql;
requires org.eclipse.persistence.moxy;
opens cz.jezek.lab11;
exports cz.jezek.lab11; exports cz.jezek.lab11;
} }
\ No newline at end of file
File added
<?xml version="1.0" encoding="UTF-8"?>
<tournament>
<players>
<player>
<firstName>Jonh</firstName>
<lastName>Doe</lastName>
</player>
<player>
<firstName>Marry</firstName>
<lastName>Jane</lastName>
</player>
</players>
<matches>
<match player1Scorel="0" player2Scorel="0">
<oponents>
<player1>
<firstName>Jonh</firstName>
<lastName>Doe</lastName>
</player1>
<player2>
<firstName>Marry</firstName>
<lastName>Jane</lastName>
</player2>
</oponents>
</match>
</matches>
</tournament>