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

Solution

parent ee99d049
No related merge requests found
......@@ -2,5 +2,5 @@
.settings/
.project
.classpath
tournament.*
.DS_Store
......@@ -33,5 +33,41 @@
<version>5.5.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>4.0.4</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.moxy</artifactId>
<version>4.0.2</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-xjc</artifactId>
<version>4.0.4</version>
</dependency>
<dependency>
<groupId>jakarta.json</groupId>
<artifactId>jakarta.json-api</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>org.eclipse.parsson</groupId>
<artifactId>parsson</artifactId>
<version>1.1.5</version>
</dependency>
</dependencies>
</project>
......@@ -2,12 +2,24 @@ package cz.jezek.lab11;
import java.util.Objects;
import jakarta.xml.bind.annotation.XmlAttribute;
import jakarta.xml.bind.annotation.XmlElement;
public class Match {
@XmlElement
private Oponents oponents;
@XmlAttribute
private int player1Scorel;
@XmlAttribute
private int player2Scorel;
@SuppressWarnings("unused")
private Match() {
}
public Match(Player player1, int player1Scorel, Player player2, int player2Scorel) {
this.oponents = new Oponents(player1, player2);
this.player1Scorel = player1Scorel;
......
......@@ -2,11 +2,23 @@ package cz.jezek.lab11;
import java.util.Objects;
import jakarta.xml.bind.annotation.XmlAttribute;
import jakarta.xml.bind.annotation.XmlIDREF;
public class Oponents {
@XmlIDREF
@XmlAttribute
private Player player1;
@XmlIDREF
@XmlAttribute
private Player player2;
@SuppressWarnings("unused")
private Oponents() {
}
public Oponents(Player player1, Player player2) {
super();
this.player1 = player1;
......
package cz.jezek.lab11;
import jakarta.xml.bind.annotation.XmlAttribute;
import jakarta.xml.bind.annotation.XmlID;
public class Player {
private String firstName;
......@@ -10,6 +13,10 @@ public class Player {
RandomGenerator.selectRandom(RandomGenerator.SURNAMES));
}
@SuppressWarnings("unused")
private Player() {
}
public Player(String firstName, String lastName) {
super();
this.firstName = firstName;
......@@ -31,6 +38,12 @@ public class Player {
public void setLastName(String lastName) {
this.lastName = lastName;
}
@XmlID
@XmlAttribute
public String getId() {
return toString();
}
@Override
public int hashCode() {
......
package cz.jezek.lab11;
import java.io.File;
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 static void main(String[] args) {
......@@ -8,7 +17,19 @@ public class StreamMain {
printStatAboutTournament(tournament);
saveTournament(tournament);
loadTournament();
Tournament tournament2 = loadTournament();
System.out.println();
System.out.println("First player from first match is between players: "
+ tournament2.getPlayers().stream()
.anyMatch(p -> p == tournament2.getMatches().get(0).getOponents().getPlayer1()));
saveTournamentJSON(tournament);
Tournament tournament3 = loadTournamentJSON();
System.out.println(tournament3);
System.out.println("First player from first match is between players: "
+ tournament3.getPlayers().stream()
.anyMatch(p -> p == tournament3.getMatches().get(0).getOponents().getPlayer1()));
}
public static void printStatAboutTournament(Tournament tournament) {
......@@ -34,11 +55,61 @@ public class StreamMain {
//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() {
public static Tournament loadTournament() {
//TODO 5.b Load a serialized tournament from a file
try {
JAXBContext jaxbContext = JAXBContext.newInstance(Tournament.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
return (Tournament) unmarshaller.unmarshal(new File("tournament.xml"));
} catch (JAXBException e) {
e.printStackTrace();
return null;
}
}
public static void saveTournament(Tournament tournament) {
//TODO 5.a Save the tournament to a file using object serialization.
try {
JAXBContext jaxbContext = JAXBContext.newInstance(Tournament.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
System.out.println();
System.out.println();
marshaller.marshal(tournament, System.out);
marshaller.marshal(tournament, new File("tournament.xml"));
} catch (JAXBException e) {
e.printStackTrace();
}
}
public static void saveTournamentJSON(Tournament tournament) {
try {
System.setProperty("jakarta.xml.bind.JAXBContextFactory", "org.eclipse.persistence.jaxb.JAXBContextFactory");
JAXBContext jaxbContext = JAXBContext.newInstance(Tournament.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, true);
System.out.println();
System.out.println();
marshaller.marshal(tournament, System.out);
marshaller.marshal(tournament, new File("tournament.json"));
} catch (JAXBException e) {
e.printStackTrace();
}
}
public static Tournament loadTournamentJSON() {
//TODO 5.b Load a serialized tournament from a file
try {
JAXBContext jaxbContext = JAXBContext.newInstance(Tournament.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
unmarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
unmarshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, true);
return (Tournament) unmarshaller.unmarshal(new File("tournament.json"));
} catch (JAXBException e) {
e.printStackTrace();
return null;
}
}
}
......@@ -9,13 +9,27 @@ import java.io.Serializable;
import java.util.List;
import java.util.stream.Stream;
import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlElementWrapper;
import jakarta.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Tournament implements Serializable{
private static final long serialVersionUID = 1L;
@XmlElement(name = "player")
@XmlElementWrapper(name = "players")
private List<Player> players;
@XmlElement(name = "match")
@XmlElementWrapper(name = "matches")
private List<Match> matches;
@SuppressWarnings("unused")
private Tournament() {
}
public static Tournament generate() {
// TO DO 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)
......@@ -26,7 +40,7 @@ public class Tournament implements Serializable{
// 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(500)
.limit(50)
.toList();
......
module cz.jezek.lab10 {
opens cz.jezek.lab11 to javafx.fxml;
opens cz.jezek.lab11 to jakarta.xml.bind;
exports cz.jezek.lab11;
requires jakarta.xml.bind;
requires org.eclipse.persistence.moxy;
requires jakarta.json;
requires com.sun.tools.xjc;
}
\ No newline at end of file
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