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

solution

parent 36f09ecf
No related merge requests found
......@@ -43,5 +43,10 @@
<version>5.5.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.16.1.1</version>
</dependency>
</dependencies>
</project>
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());
......@@ -95,9 +94,14 @@ public class GameController {
private void gameOverPressed() {
Score score = new Score(new Random().nextInt(11), nameTextField.getText());
scoreList.getItems().add(score);
Collections.sort(scoreList.getItems(), new ScoreComparator().reversed());
sortScores();
}
private void sortScores() {
Collections.sort(scoreList.getItems(), new ScoreComparator().reversed());
}
@FXML
private void highScoresPressed() {
Set<Score> tempScores = new HashSet<>();
......@@ -110,24 +114,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();
}
......
......@@ -5,15 +5,23 @@ import java.io.Serializable;
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 score, String name) {
this(null, score, name) ;
}
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() {
......
package lab;
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.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
public class ScoreDAO {
public ScoreDAO() {
/*Connection conn = null;
try {
conn = getConnection();
} finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}*/
try (Connection conn = getConnection()) {
initTable(conn);
} catch (SQLException e) {
e.printStackTrace();
}
}
public List<Score> load() {
List<Score> result = new LinkedList<>();
try(Connection connection = getConnection();
Statement stmt = connection.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) {
List<Score> existingRecors = load();
Set<Integer> inMemory = new HashSet<>();
for (Score score: scores) {
if (score.getId() == null) {
continue;
}
inMemory.add(score.getId());
}
Set<Integer> inDb = new HashSet<>();
for (Score score: existingRecors) {
if (score.getId() == null) {
continue;
}
inDb.add(score.getId());
}
inDb.removeAll(inMemory);
Set<Integer> idsForDelete = inDb;
delete(idsForDelete);
try(Connection conn = getConnection()){
try(PreparedStatement pstms = conn.prepareStatement("INSERT INTO score (value, name) VALUES (?,?)", Statement.RETURN_GENERATED_KEYS)){
for (Score score: scores) {
if (score.getId() == null) {
pstms.setInt(1, score.getScore());
pstms.setString(2, score.getName());
pstms.execute();
ResultSet rs = pstms.getGeneratedKeys();
if (rs.next()) {
score.setId(rs.getInt(1));
}
}
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
private void delete(Set<Integer> idsForDelete) {
try(Connection conn = getConnection();
PreparedStatement pstms = conn.prepareStatement("DELETE FROM score WHERE id = ?")){
for (Integer id: idsForDelete) {
pstms.setInt(1, id);
pstms.executeUpdate();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
private void initTable(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),"
+ "PRIMARY KEY (id)"
+ ")");
} catch (SQLException e) {
if (e.getSQLState().equals("X0Y32")) {
//ignore because table already 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 {
requires transitive javafx.controls;
requires javafx.fxml;
requires java.sql;
opens lab to javafx.fxml;
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