From 4ac657776a0348634ff05e7cfa1fa396d6f96cbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Feyzi=20Hala=C3=A7?= Date: Sun, 20 Dec 2020 23:03:19 +0300 Subject: [PATCH] Making DataManager methods static --- .../modelcontrollers/GameManager.java | 2 +- .../scenecontrollers/GameSceneController.java | 2 +- .../LoadGameSceneController.java | 5 ++-- src/storage/filemanager/DataManager.java | 27 +++++-------------- 4 files changed, 10 insertions(+), 26 deletions(-) diff --git a/src/controllers/modelcontrollers/GameManager.java b/src/controllers/modelcontrollers/GameManager.java index 9b71ba3..3b9df94 100644 --- a/src/controllers/modelcontrollers/GameManager.java +++ b/src/controllers/modelcontrollers/GameManager.java @@ -41,7 +41,7 @@ public void setGame(Game saveGame){ // setup a game public void initGame(ArrayList players) { ArrayList regions = SettingImporter.getRegions(); - ArrayList chanceCardNames = DataManager.getInstance().getChanceCardNames(); + ArrayList chanceCardNames = DataManager.getChanceCardNames(); game = new Game(players,regions,chanceCardNames); } diff --git a/src/controllers/scenecontrollers/GameSceneController.java b/src/controllers/scenecontrollers/GameSceneController.java index ec1831f..8f0001c 100644 --- a/src/controllers/scenecontrollers/GameSceneController.java +++ b/src/controllers/scenecontrollers/GameSceneController.java @@ -259,7 +259,7 @@ public void handleEndTurnButton() { } public void handleSaveGameButton() { - DataManager.getInstance().saveGame("SaveGame1"); + DataManager.saveGame("SaveGame1"); } diff --git a/src/controllers/scenecontrollers/LoadGameSceneController.java b/src/controllers/scenecontrollers/LoadGameSceneController.java index 515fcf3..1fee6ae 100644 --- a/src/controllers/scenecontrollers/LoadGameSceneController.java +++ b/src/controllers/scenecontrollers/LoadGameSceneController.java @@ -33,8 +33,7 @@ public void getSavedGames() { buttonBox.setPrefHeight(700); buttonBox.setAlignment(Pos.TOP_CENTER); buttonBox.setSpacing(15.0); - DataManager dataManager = DataManager.getInstance(); - List savedNames = dataManager.getSavedNames(); + List savedNames = DataManager.getSavedNames(); for ( String saveName: savedNames ) { Button newButton = new Button(saveName); newButton.setStyle("-fx-background-color: #5b4b4b; -fx-text-fill: #ffffff; -fx-font-size: 2em"); @@ -56,7 +55,7 @@ public void getSavedGames() { } public void handleLoadGameButton(ActionEvent actionEvent) { - DataManager.getInstance().loadGame(((Button)actionEvent.getSource()).getText()); + DataManager.loadGame(((Button)actionEvent.getSource()).getText()); SceneManager.getInstance().showGameScene(); } diff --git a/src/storage/filemanager/DataManager.java b/src/storage/filemanager/DataManager.java index 1683797..4a5d14b 100644 --- a/src/storage/filemanager/DataManager.java +++ b/src/storage/filemanager/DataManager.java @@ -9,25 +9,10 @@ import java.util.List; public class DataManager { - // properties - private static DataManager dataManager = null; - private String saveGameFolder; - - // constructor - public DataManager(){ - String currentDir = System.getProperty("user.dir"); - this.saveGameFolder = currentDir + "/saves/"; - } + private static final String saveGameFolder = System.getProperty("user.dir") + "/saves/"; // methods - public static DataManager getInstance(){ - if(dataManager == null){ - dataManager = new DataManager(); - } - return dataManager; - } - - public void saveGame(String saveName){ + public static void saveGame(String saveName){ try{ FileOutputStream fout = new FileOutputStream(saveGameFolder + saveName + ".ser"); ObjectOutputStream out = new ObjectOutputStream(fout); @@ -40,7 +25,7 @@ public void saveGame(String saveName){ } } - public void loadGame(String saveName){ + public static void loadGame(String saveName){ try{ FileInputStream fin = new FileInputStream(saveGameFolder + saveName + ".ser"); ObjectInputStream in = new ObjectInputStream(fin); @@ -56,10 +41,10 @@ public void loadGame(String saveName){ * gets filenames of saves in a List * @return the List of saved game file names, null if directory path is wrong or caught an exception */ - public ArrayList getSavedNames(){ + public static ArrayList getSavedNames(){ ArrayList savedNames = new ArrayList<>(); //Creating a File object for directory - File directoryPath = new File(this.saveGameFolder); + File directoryPath = new File(saveGameFolder); try{ String[] temp = directoryPath.list(); @@ -88,7 +73,7 @@ public ArrayList getSavedNames(){ * gets classNames of chance card classes in a List * @return the List of chance card classes file names, null if directory path is wrong or caught an exception */ - public ArrayList getChanceCardNames(){ + public static ArrayList getChanceCardNames(){ ArrayList names = new ArrayList<>(); String currentDir = System.getProperty("user.dir"); String folder = currentDir + "/src/models/chanceCards/";