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

solution

parent 03bf36ce
Branches 2022_Wed_7-15
No related merge requests found
Pipeline #46 failed with stages
in 0 seconds
...@@ -4,3 +4,5 @@ ...@@ -4,3 +4,5 @@
.classpath .classpath
.DS_Store .DS_Store
/*.xml
/*.json
...@@ -33,5 +33,63 @@ ...@@ -33,5 +33,63 @@
<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.1</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>4.0.1</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>4.0.0</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>4.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-xjc -->
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-xjc</artifactId>
<version>4.0.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/jakarta.json/jakarta.json-api -->
<dependency>
<groupId>jakarta.json</groupId>
<artifactId>jakarta.json-api</artifactId>
<version>2.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.eclipse.parsson/parsson -->
<dependency>
<groupId>org.eclipse.parsson</groupId>
<artifactId>parsson</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies> </dependencies>
</project> </project>
...@@ -2,12 +2,23 @@ package cz.jezek.lab11; ...@@ -2,12 +2,23 @@ package cz.jezek.lab11;
import java.util.Objects; import java.util.Objects;
import jakarta.xml.bind.annotation.XmlAttribute;
import jakarta.xml.bind.annotation.XmlElement;
public class Match { public class Match {
@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;
......
...@@ -2,13 +2,23 @@ package cz.jezek.lab11; ...@@ -2,13 +2,23 @@ package cz.jezek.lab11;
import java.util.Objects; import java.util.Objects;
import jakarta.xml.bind.annotation.XmlAttribute;
import jakarta.xml.bind.annotation.XmlIDREF;
public class Oponents { public class Oponents {
@XmlAttribute
@XmlIDREF
private Player player1; private Player player1;
@XmlAttribute
@XmlIDREF
private Player player2; private Player player2;
public Oponents() {
}
public Oponents(Player player1, Player player2) { public Oponents(Player player1, Player player2) {
super();
this.player1 = player1; this.player1 = player1;
this.player2 = player2; this.player2 = player2;
} }
......
package cz.jezek.lab11; package cz.jezek.lab11;
import jakarta.xml.bind.annotation.XmlAttribute;
import jakarta.xml.bind.annotation.XmlID;
public class Player { public class Player {
private String firstName; private String firstName;
...@@ -9,9 +12,11 @@ public class Player { ...@@ -9,9 +12,11 @@ public class Player {
return new Player(RandomGenerator.selectRandom(RandomGenerator.NAMES), return new Player(RandomGenerator.selectRandom(RandomGenerator.NAMES),
RandomGenerator.selectRandom(RandomGenerator.SURNAMES)); RandomGenerator.selectRandom(RandomGenerator.SURNAMES));
} }
public Player() {
}
public Player(String firstName, String lastName) { public Player(String firstName, String lastName) {
super();
this.firstName = firstName; this.firstName = firstName;
this.lastName = lastName; this.lastName = lastName;
} }
...@@ -31,6 +36,12 @@ public class Player { ...@@ -31,6 +36,12 @@ public class Player {
public void setLastName(String lastName) { public void setLastName(String lastName) {
this.lastName = lastName; this.lastName = lastName;
} }
@XmlID
@XmlAttribute
public String getFullName() {
return toString();
}
@Override @Override
public int hashCode() { public int hashCode() {
......
package cz.jezek.lab11; package cz.jezek.lab11;
import static java.util.Arrays.asList;
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 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");
printStatAboutTournament(tournament); Player player1 = new Player("John", "Doe");
Player player2 = new Player("Marry", "Doe");
saveTournament(tournament); Match match = new Match(player1, 5, player2, 3);
loadTournament(); Tournament tournament = new Tournament(asList(player1, player2), asList(match));
}
saveTournamentsAsXML(tournament);
public static void printStatAboutTournament(Tournament tournament) {
System.out.println(tournament); System.out.println("tournamen.player1 == tournamen.match.player1: "
Player player = tournament.getRandomPlayer(); + (tournament.getPlayers().get(0) == tournament.getMatches().get(0).getOponents().getPlayer1()));
System.out.println(player); Tournament tournament2 = loadTournamentFromXML();
System.out.print("Number of matches: ");
//TODO 3.a Show the number of matches played by a given player (using Streams) System.out.println(tournament2);
System.out.print("Goals scored: "); System.out.println("tournament2.player1 == tournament2.match.player1: "
//TODO 3.b Show the number of goals scored by a given player (help Streams) + (tournament2.getPlayers().get(0) == tournament2.getMatches().get(0).getOponents().getPlayer1()));
Oponents oponents = new Oponents(tournament.getRandomPlayer(),
tournament.getRandomPlayer()); saveTournamentsAsJSON(tournament);
System.out.println("Mutual statistics of oponents:" + oponents); tournament2 = loadTournamentFromJSON();
System.out.println(tournament2);
System.out.print("Matches: "); System.out.println("tournament2.player1 == tournament2.match.player1: "
//TODO 4.a List the matches played between the given players (using the stream) + (tournament2.getPlayers().get(0) == tournament2.getMatches().get(0).getOponents().getPlayer1()));
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) private static Tournament loadTournamentFromXML() {
} try {
JAXBContext context = JAXBContext.newInstance(Tournament.class);
public static void loadTournament() { Unmarshaller unmarshaller = context.createUnmarshaller();
//TODO 5.b Load a serialized tournament from a file return (Tournament) unmarshaller.unmarshal(new File("tournament.xml"));
} } catch (JAXBException e) {
e.printStackTrace();
public static void saveTournament(Tournament tournament) { }
//TODO 5.a Save the tournament to a file using object serialization.
} return null;
}
private static Tournament loadTournamentFromJSON() {
try {
JAXBContext context = JAXBContext.newInstance(Tournament.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
unmarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
return (Tournament) unmarshaller.unmarshal(new File("tournament.json"));
} catch (JAXBException e) {
e.printStackTrace();
}
return null;
}
private static void saveTournamentsAsXML(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 saveTournamentsAsJSON(Tournament tournament) {
try {
JAXBContext context = JAXBContext.newInstance(Tournament.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, true);
marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
marshaller.marshal(tournament, System.out);
marshaller.marshal(tournament, new File("tournament.json"));
} catch (JAXBException e) {
e.printStackTrace();
}
}
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.
}
} }
...@@ -3,11 +3,24 @@ package cz.jezek.lab11; ...@@ -3,11 +3,24 @@ package cz.jezek.lab11;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlElementWrapper;
import jakarta.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Tournament { public class Tournament {
@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;
public Tournament() {
}
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 = Collections.emptyList();
...@@ -39,17 +52,9 @@ public class Tournament { ...@@ -39,17 +52,9 @@ public class Tournament {
@Override @Override
public String toString() { public String toString() {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
builder.append("Player count ").append(players.size()) builder.append("players = ").append(players);
.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"); builder.append(", mathes = ").append(matches);
// 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(); return builder.toString();
} }
......
module cz.jezek.lab10 { module cz.jezek.lab10 {
opens cz.jezek.lab11 to javafx.fxml; opens cz.jezek.lab11 to jakarta.xml.bind;
exports cz.jezek.lab11; 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