From 3b7751e927ce2feccff59ac25722b27cfc1d5c59 Mon Sep 17 00:00:00 2001 From: koz01 <koz01@PCFEIB213-100.msad.vsb.cz> Date: Wed, 16 Nov 2022 10:23:55 +0100 Subject: [PATCH] solution --- pom.xml | 5 + src/main/java/lab/GameController.java | 22 ++--- src/main/java/lab/Score.java | 20 +++- src/main/java/lab/ScoreDAO.java | 137 ++++++++++++++++++++++++++ src/main/java/module-info.java | 1 + 5 files changed, 167 insertions(+), 18 deletions(-) create mode 100644 src/main/java/lab/ScoreDAO.java diff --git a/pom.xml b/pom.xml index 6bd5f99..d3447f5 100644 --- a/pom.xml +++ b/pom.xml @@ -12,6 +12,11 @@ <maven.compiler.target>17</maven.compiler.target> </properties> <dependencies> + <dependency> + <groupId>org.apache.derby</groupId> + <artifactId>derby</artifactId> + <version>10.16.1.1</version> + </dependency> <dependency> <groupId>org.openjfx</groupId> <artifactId>javafx-controls</artifactId> diff --git a/src/main/java/lab/GameController.java b/src/main/java/lab/GameController.java index 2fee0eb..d06114d 100644 --- a/src/main/java/lab/GameController.java +++ b/src/main/java/lab/GameController.java @@ -1,10 +1,6 @@ package lab; -import java.io.BufferedReader; -import java.io.FileReader; -import java.io.FileWriter; import java.io.IOException; -import java.io.PrintWriter; import java.util.Collections; import java.util.HashSet; import java.util.LinkedList; @@ -51,10 +47,13 @@ public class GameController { private AnimationTimer animationTimer; + private ScoreDAO scoreDAO; + public GameController() { } public void startGame() { + scoreDAO = new ScoreDAO(); initScoreList(); this.world = new World(canvas.getWidth(), canvas.getHeight()); @@ -110,24 +109,15 @@ public class GameController { @FXML private void savePressed() throws IOException { - try(PrintWriter pw = new PrintWriter(new FileWriter("high_score.csv"))){ - for(Score score: highScores) { - pw.printf("%s;%d\n", score.getName(), score.getScore()); - } - } + scoreDAO.save(highScores); } @FXML private void loadPressed() throws IOException{ highScores.clear(); - try(BufferedReader br = new BufferedReader(new FileReader("high_score.csv"))){ - String line; - while(null != (line = br.readLine())) { - String [] tokens = line.split(";"); - highScores.add(new Score(Integer.parseInt(tokens[1]), tokens[0])); - } - } + highScores.addAll(scoreDAO.load()); + sortScores(); initScoreList(); } diff --git a/src/main/java/lab/Score.java b/src/main/java/lab/Score.java index e7f8bc7..c238f8c 100644 --- a/src/main/java/lab/Score.java +++ b/src/main/java/lab/Score.java @@ -6,15 +6,23 @@ public class Score implements Serializable{ private static final long serialVersionUID = 712213568495641939L; + private Integer id; + private final int score; private final String name; - public Score(int score, String name) { - super(); + + public Score(Integer id, int score, String name) { + this.id = id; this.score = score; this.name = name; } + public Score(int value, String text) { + this(null, value, text); + } + + public String getName() { return name; } @@ -22,6 +30,14 @@ public class Score implements Serializable{ public int getScore() { return score; } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } @Override public String toString() { diff --git a/src/main/java/lab/ScoreDAO.java b/src/main/java/lab/ScoreDAO.java new file mode 100644 index 0000000..2073553 --- /dev/null +++ b/src/main/java/lab/ScoreDAO.java @@ -0,0 +1,137 @@ +package lab; + +import static java.util.stream.Collectors.toSet; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.LinkedList; +import java.util.List; +import java.util.Set; + +public class ScoreDAO { + public ScoreDAO() { + /*Connection con = null; + try { + con = getConnection(); + } finally { + try { + con.close(); + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + }*/ + + try (Connection con = getConnection()) { + createTable(con); + } catch (SQLException e) { + e.printStackTrace(); + } + + } + + public List<Score> load() { + List<Score> result = new LinkedList<>(); + try(Connection con = getConnection(); Statement stmt = con.createStatement()) { + try (ResultSet rs = stmt.executeQuery("SELECT id, value, name FROM score")){ + while (rs.next()) { + result.add(new Score(rs.getInt(1), rs.getInt(2), rs.getString("name"))); + } + } + } catch (SQLException e) { + e.printStackTrace(); + } + return result; + } + + public void save(List<Score> scores) { + /*Set<Integer> mSet = new HashSet<>(); + for (Score score: scores) { + if (score.getId() == null) { + continue; + } + mSet.add(score.getId()); + } + + Set<Integer> dSet = new HashSet<>(); + for (Score score: load()) { + dSet.add(score.getId()); + }*/ + Set<Integer> mSet = scores.stream() + .filter(s -> s.getId() != null) + .map(Score::getId) + .collect(toSet()); + Set<Integer> dSet = load().stream().map(Score::getId).collect(toSet()); + + dSet.removeAll(mSet); + + deleteScoresById(dSet); + + try (Connection con = getConnection(); + PreparedStatement pstmt = con.prepareStatement("INSERT INTO score(value, name) VALUES(?,?)", Statement.RETURN_GENERATED_KEYS)) { + + for(Score score : scores) { + if (score.getId() != null) { + continue; + } + + pstmt.setInt(1, score.getScore()); + pstmt.setString(2, score.getName()); + pstmt.execute(); + ResultSet rs = pstmt.getGeneratedKeys(); + if (rs.next()) { + score.setId(rs.getInt(1)); + } + } + + + } catch (SQLException e) { + e.printStackTrace(); + } + } + + private void deleteScoresById(Set<Integer> ids) { + try (Connection conn = getConnection(); + PreparedStatement pstmt = conn.prepareStatement("DELETE FROM score WHERE id = ?")){ + for (Integer id: ids) { + pstmt.setInt(1, id); + pstmt.addBatch(); + } + pstmt.executeBatch(); + } catch (SQLException e) { + e.printStackTrace(); + } + + } + + private static void createTable(Connection con) { + try (Statement stmt = con.createStatement()) { + stmt.execute("CREATE TABLE score (" + + "id INT NOT NULL GENERATED ALWAYS AS IDENTITY," + + "value INT NOT NULL," + + "name VARCHAR(255) NOT NULL," + + "PRIMARY KEY (id)" + + ")"); + } catch (SQLException e) { + if (e.getSQLState().equals("X0Y32")) { + //ignore table already exists + return; + } + e.printStackTrace(); + } + + } + + private static Connection getConnection() { + try { + return DriverManager.getConnection("jdbc:derby:scoreDB;create=true"); + } catch (SQLException e) { + e.printStackTrace(); + return null; + } + } +} diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index 7006c78..94ff2e3 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -1,6 +1,7 @@ module lab01 { requires transitive javafx.controls; requires javafx.fxml; + requires java.sql; opens lab to javafx.fxml; exports lab; } \ No newline at end of file -- GitLab