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

solution

parent d4d85109
Branches 2022_Wed_10-45
No related merge requests found
<project xmlns="http://maven.apache.org/POM/4.0.0" <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>vsb-cs-java1</groupId> <groupId>vsb-cs-java1</groupId>
<artifactId>lab08</artifactId> <artifactId>lab08</artifactId>
...@@ -12,6 +10,11 @@ ...@@ -12,6 +10,11 @@
<maven.compiler.target>17</maven.compiler.target> <maven.compiler.target>17</maven.compiler.target>
</properties> </properties>
<dependencies> <dependencies>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.16.1.1</version>
</dependency>
<dependency> <dependency>
<groupId>org.openjfx</groupId> <groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId> <artifactId>javafx-controls</artifactId>
......
package lab; package lab;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedList; import java.util.LinkedList;
...@@ -51,10 +47,13 @@ public class GameController { ...@@ -51,10 +47,13 @@ public class GameController {
private AnimationTimer animationTimer; private AnimationTimer animationTimer;
private ScoreDAO scoreDAO;
public GameController() { public GameController() {
} }
public void startGame() { public void startGame() {
scoreDAO = new ScoreDAO();
initScoreList(); initScoreList();
this.world = new World(canvas.getWidth(), canvas.getHeight()); this.world = new World(canvas.getWidth(), canvas.getHeight());
...@@ -110,24 +109,15 @@ public class GameController { ...@@ -110,24 +109,15 @@ public class GameController {
@FXML @FXML
private void savePressed() throws IOException { private void savePressed() throws IOException {
try(PrintWriter pw = new PrintWriter(new FileWriter("high_score.csv"))){ scoreDAO.save(highScores);
for(Score score: highScores) {
pw.printf("%s;%d\n", score.getName(), score.getScore());
}
}
} }
@FXML @FXML
private void loadPressed() throws IOException{ private void loadPressed() throws IOException{
highScores.clear(); highScores.clear();
try(BufferedReader br = new BufferedReader(new FileReader("high_score.csv"))){ highScores.addAll(scoreDAO.load());
String line; sortScores();
while(null != (line = br.readLine())) {
String [] tokens = line.split(";");
highScores.add(new Score(Integer.parseInt(tokens[1]), tokens[0]));
}
}
initScoreList(); initScoreList();
} }
......
...@@ -5,11 +5,18 @@ import java.io.Serializable; ...@@ -5,11 +5,18 @@ import java.io.Serializable;
public class Score implements Serializable{ public class Score implements Serializable{
private static final long serialVersionUID = 712213568495641939L; private static final long serialVersionUID = 712213568495641939L;
private Integer id;
private final int score; private final int score;
private final String name; private final String name;
public Score(int score, String name) { public Score(int score, String name) {
super(); this(null, score, name);
}
public Score(Integer id, int score, String name) {
this.id = id;
this.score = score; this.score = score;
this.name = name; this.name = name;
} }
...@@ -22,6 +29,14 @@ public class Score implements Serializable{ ...@@ -22,6 +29,14 @@ public class Score implements Serializable{
public int getScore() { public int getScore() {
return score; return score;
} }
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Override @Override
public String toString() { public String toString() {
......
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.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
public class ScoreDAO {
public ScoreDAO() {
try (Connection conn = getConnection()) {
createTable(conn);
} 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(3)));
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return result ;
}
public void save(List<Score> scores) {
/*Set<Integer> m = new HashSet<>();
for (Score score: scores) {
if (score.getId() == null) {
continue;
}
m.add(score.getId());
}*/
Set<Integer> m = scores.stream()
.filter(s -> s.getId() != null)
.map(Score::getId)
.collect(toSet());
Set<Integer> d = load().stream().map(Score::getId).collect(Collectors.toSet());
d.removeAll(m);
Set<Integer> idsOnlyInDB = d;
removeByIDS(idsOnlyInDB);
List<Score> scoreSaved = new LinkedList<>();
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.addBatch();
scoreSaved.add(score);
}
if (!scoreSaved.isEmpty()) {
pstmt.executeBatch();
ResultSet rs = pstmt.getGeneratedKeys();
Iterator<Score> iter = scoreSaved.iterator();
while (rs.next()) {
Score score = iter.next();
score.setId(rs.getInt(1));
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
private void removeByIDS(Set<Integer> idsOnlyInDB) {
try(Connection con = getConnection();
PreparedStatement pstmt = con.prepareStatement("DELETE FROM score WHERE id = ?")) {
for (Integer id: idsOnlyInDB) {
pstmt.setInt(1, id);
pstmt.addBatch();
}
pstmt.executeBatch();
} catch (SQLException e) {
e.printStackTrace();
}
}
private void createTable(Connection conn) {
try (Statement stmt = conn.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 this because table exists
return;
}
e.printStackTrace();
}
}
private Connection getConnection() {
try {
return DriverManager.getConnection("jdbc:derby:scoreDB;create=true");
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
}
module lab01 { module lab01 {
requires transitive javafx.controls; requires transitive javafx.controls;
requires javafx.fxml; requires javafx.fxml;
requires java.sql;
opens lab to javafx.fxml; opens lab to javafx.fxml;
exports lab; exports lab;
} }
\ 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