Skip to content

Commit

Permalink
Making DataManager methods static
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmethalac committed Dec 20, 2020
1 parent 66f90f5 commit 4ac6577
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/controllers/modelcontrollers/GameManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void setGame(Game saveGame){
// setup a game
public void initGame(ArrayList<Player> players) {
ArrayList<Region> regions = SettingImporter.getRegions();
ArrayList<String> chanceCardNames = DataManager.getInstance().getChanceCardNames();
ArrayList<String> chanceCardNames = DataManager.getChanceCardNames();
game = new Game(players,regions,chanceCardNames);
}

Expand Down
2 changes: 1 addition & 1 deletion src/controllers/scenecontrollers/GameSceneController.java
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ public void handleEndTurnButton() {
}

public void handleSaveGameButton() {
DataManager.getInstance().saveGame("SaveGame1");
DataManager.saveGame("SaveGame1");

}

Expand Down
5 changes: 2 additions & 3 deletions src/controllers/scenecontrollers/LoadGameSceneController.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ public void getSavedGames() {
buttonBox.setPrefHeight(700);
buttonBox.setAlignment(Pos.TOP_CENTER);
buttonBox.setSpacing(15.0);
DataManager dataManager = DataManager.getInstance();
List<String> savedNames = dataManager.getSavedNames();
List<String> 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");
Expand All @@ -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();
}

Expand Down
27 changes: 6 additions & 21 deletions src/storage/filemanager/DataManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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<String> getSavedNames(){
public static ArrayList<String> getSavedNames(){
ArrayList<String> 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();
Expand Down Expand Up @@ -88,7 +73,7 @@ public ArrayList<String> 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<String> getChanceCardNames(){
public static ArrayList<String> getChanceCardNames(){
ArrayList<String> names = new ArrayList<>();
String currentDir = System.getProperty("user.dir");
String folder = currentDir + "/src/models/chanceCards/";
Expand Down

0 comments on commit 4ac6577

Please sign in to comment.