Skip to content
Snippets Groups Projects
Commit 3a7fc2aa authored by jez04's avatar jez04
Browse files

feat: add factory

parent 30479247
No related merge requests found
Pipeline #2493 failed with stages
...@@ -29,7 +29,6 @@ public class App extends Application { ...@@ -29,7 +29,6 @@ public class App extends Application {
GameController gameController = gameLoader.getController(); GameController gameController = gameLoader.getController();
Scene scene = new Scene(root); Scene scene = new Scene(root);
primaryStage.setScene(scene); primaryStage.setScene(scene);
// primaryStage.resizableProperty().set(false);
primaryStage.setTitle("Java 2 - 2nd laboratory"); primaryStage.setTitle("Java 2 - 2nd laboratory");
primaryStage.show(); primaryStage.show();
// Exit program when main window is closed // Exit program when main window is closed
......
...@@ -13,15 +13,15 @@ public class DbConnector { ...@@ -13,15 +13,15 @@ public class DbConnector {
private static final String JDBC_CONECTIN_STRING = "jdbc:h2:file:./scoreDB"; private static final String JDBC_CONECTIN_STRING = "jdbc:h2:file:./scoreDB";
public static List<Score> getAll() { public List<Score> getAll() {
return queryScore("select * from scores;"); return queryScore("select * from scores;");
} }
public static List<Score> getFirstTen() { public List<Score> getFirstTen() {
return queryScore("select * from scores order by points desc limit 10;"); return queryScore("select * from scores order by points desc limit 10;");
} }
private static List<Score> queryScore(String query) { private List<Score> queryScore(String query) {
List<Score> result = new ArrayList<>(); List<Score> result = new ArrayList<>();
try (Connection con = DriverManager.getConnection(JDBC_CONECTIN_STRING); try (Connection con = DriverManager.getConnection(JDBC_CONECTIN_STRING);
Statement stm = con.createStatement(); Statement stm = con.createStatement();
...@@ -35,7 +35,7 @@ public class DbConnector { ...@@ -35,7 +35,7 @@ public class DbConnector {
return result; return result;
} }
public static void createTable() { public void init() {
try (Connection con = DriverManager.getConnection(JDBC_CONECTIN_STRING); try (Connection con = DriverManager.getConnection(JDBC_CONECTIN_STRING);
Statement stm = con.createStatement();) { Statement stm = con.createStatement();) {
stm.executeUpdate("CREATE TABLE if not exists scores (nick VARCHAR(50) NOT NULL, points INT NOT NULL);"); stm.executeUpdate("CREATE TABLE if not exists scores (nick VARCHAR(50) NOT NULL, points INT NOT NULL);");
...@@ -44,7 +44,7 @@ public class DbConnector { ...@@ -44,7 +44,7 @@ public class DbConnector {
} }
} }
public static void insertScore(Score score) { public void insertScore(Score score) {
try (Connection con = DriverManager.getConnection(JDBC_CONECTIN_STRING); try (Connection con = DriverManager.getConnection(JDBC_CONECTIN_STRING);
PreparedStatement stm = con.prepareStatement("INSERT INTO scores VALUES (?, ?)");) { PreparedStatement stm = con.prepareStatement("INSERT INTO scores VALUES (?, ?)");) {
stm.setString(1, score.getName()); stm.setString(1, score.getName());
......
...@@ -71,17 +71,17 @@ public class GameController { ...@@ -71,17 +71,17 @@ public class GameController {
void btnGenerateScoreAction(ActionEvent event) { void btnGenerateScoreAction(ActionEvent event) {
Score score = Score.generate(); Score score = Score.generate();
this.scores.getItems().add(score); this.scores.getItems().add(score);
DbConnector.insertScore(score); ScoreStorageFactory.getInstance().insertScore(score);
} }
@FXML @FXML
void btnLoadAllAction(ActionEvent event) { void btnLoadAllAction(ActionEvent event) {
updateScoreTable(DbConnector.getAll()); updateScoreTable(ScoreStorageFactory.getInstance().getAll());
} }
@FXML @FXML
void btnLoadFirstTenAction(ActionEvent event) { void btnLoadFirstTenAction(ActionEvent event) {
updateScoreTable(DbConnector.getFirstTen()); updateScoreTable(ScoreStorageFactory.getInstance().getFirstTen());
} }
private void updateScoreTable(List<Score> scores) { private void updateScoreTable(List<Score> scores) {
...@@ -117,8 +117,8 @@ public class GameController { ...@@ -117,8 +117,8 @@ public class GameController {
private void initDB() { private void initDB() {
//Stream.generate(Score::generate).limit(10).toList(); //Stream.generate(Score::generate).limit(10).toList();
DbConnector.createTable(); ScoreStorageFactory.getInstance().init();
scores.getItems().addAll(DbConnector.getAll()); scores.getItems().addAll(ScoreStorageFactory.getInstance().getAll());
} }
public void stop() { public void stop() {
......
package lab;
public class ScoreStorageFactory {
private static DbConnector instance;
public static DbConnector getInstance() {
if(instance == null) {
instance = new DbConnector();
}
return instance;
}
}
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
<?import javafx.scene.layout.VBox?> <?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?> <?import javafx.scene.text.Font?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="421.0" prefWidth="849.0" xmlns="http://javafx.com/javafx/21" xmlns:fx="http://javafx.com/fxml/1" fx:controller="lab.GameController"> <BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="388.0" prefWidth="911.0" xmlns="http://javafx.com/javafx/21" xmlns:fx="http://javafx.com/fxml/1" fx:controller="lab.GameController">
<bottom> <bottom>
<HBox alignment="TOP_CENTER" prefHeight="66.0" prefWidth="866.0" BorderPane.alignment="CENTER"> <HBox alignment="TOP_CENTER" prefHeight="66.0" prefWidth="866.0" BorderPane.alignment="CENTER">
<children> <children>
...@@ -49,7 +49,7 @@ ...@@ -49,7 +49,7 @@
<center> <center>
<VBox maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" BorderPane.alignment="CENTER"> <VBox maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" BorderPane.alignment="CENTER">
<children> <children>
<TableView fx:id="scores" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308"> <TableView fx:id="scores" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="2000.0">
<columns> <columns>
<TableColumn fx:id="nickColumn" prefWidth="75.0" text="Nick" /> <TableColumn fx:id="nickColumn" prefWidth="75.0" text="Nick" />
<TableColumn fx:id="pointsColumn" prefWidth="75.0" text="Points" /> <TableColumn fx:id="pointsColumn" prefWidth="75.0" text="Points" />
......
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