From b6619f03c058da1939d391577d8fe44df1e3f73b Mon Sep 17 00:00:00 2001 From: HSoreide <sofie.scisly@gmail.com> Date: Thu, 27 Apr 2023 15:49:07 +0200 Subject: [PATCH] Refactor towards external persistent storage --- .../demo/controller/CreateBudgetController.java | 8 +++++++- .../demo/controller/FirstMenuController.java | 14 ++++++++++---- .../demo/controller/IncomeExpenseController.java | 2 +- .../demo/controller/MainMenuController.java | 2 +- .../data/budget/FileHandlingSelectedBudget.java | 9 ++++++--- 5 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/main/java/no/ntnu/idatt1002/demo/controller/CreateBudgetController.java b/src/main/java/no/ntnu/idatt1002/demo/controller/CreateBudgetController.java index 5ae3fdc5..41b6d042 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/controller/CreateBudgetController.java +++ b/src/main/java/no/ntnu/idatt1002/demo/controller/CreateBudgetController.java @@ -158,9 +158,13 @@ public class CreateBudgetController { boolean empty; try { + String path = System.getProperty("user.home"); + filePath = path + "/BudgetBuddyFiles/budgets"; + //TODO: Fiks at måned kommer med i navnet. empty = FileHandlingSelectedBudget.createBudgetDirectory( filePath + "/" + currentMonth + budgetName); + FileHandlingSelectedBudget.createNewIncomeFile(filePath + "/" + currentMonth + budgetName, "Income"); FileHandlingSelectedBudget.createNewExpenseFile(filePath + "/" + currentMonth + budgetName, @@ -184,9 +188,11 @@ public class CreateBudgetController { * @param budgetName The name of the budget. */ public void updateCurrentFile(String currentMonth, String budgetName) { + System.out.println(currentMonth + budgetName); try { + FileHandlingSelectedBudget.updateSelectedBudget(currentMonth + budgetName, - "budgets/SelectedBudget"); + filePath + "/SelectedBudget"); } catch (IOException ioe) { showErrorMsgBox(ioe.getMessage(), ioe.getMessage(), ioe.getMessage()); } diff --git a/src/main/java/no/ntnu/idatt1002/demo/controller/FirstMenuController.java b/src/main/java/no/ntnu/idatt1002/demo/controller/FirstMenuController.java index cccb5f95..49527162 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/controller/FirstMenuController.java +++ b/src/main/java/no/ntnu/idatt1002/demo/controller/FirstMenuController.java @@ -29,15 +29,21 @@ public class FirstMenuController { * Initializes as soon as the scene is loaded. */ @FXML - public void initialize() { + public void initialize() throws IOException { String path = System.getProperty("user.home"); - String buddyPath = path + "/BudgetBuddy"; + String buddyPath = path + "/BudgetBuddyFiles"; filePath = buddyPath + "/budgets"; File budget = new File(buddyPath + "/budgets"); if(!budget.exists()){ budget.mkdirs(); } + + File archive = new File(buddyPath + "/budgets/Archive.archive"); + if(!archive.exists()){ + archive.createNewFile(); + } + File economics = new File(buddyPath + "/economics"); if(!economics.exists()){ economics.mkdirs(); @@ -86,7 +92,7 @@ public class FirstMenuController { try { //Only proceeds to next scene if there is a budget selected if (FileHandlingSelectedBudget - .readSelectedBudget(filePath + "SelectedBudget") != null) { + .readSelectedBudget(filePath + "/SelectedBudget") != null) { switchNext(event); } } catch (IOException ioe) { @@ -156,7 +162,7 @@ public class FirstMenuController { try { //Only switches scenes if there is a budget that can be selected. if (FileHandlingSelectedBudget - .readSelectedBudget(filePath + "SelectedBudget") != null) { + .readSelectedBudget(filePath + "/SelectedBudget") != null) { switchChosenBudget(event); } } catch (IOException ioe) { diff --git a/src/main/java/no/ntnu/idatt1002/demo/controller/IncomeExpenseController.java b/src/main/java/no/ntnu/idatt1002/demo/controller/IncomeExpenseController.java index 6550eda5..c241fe79 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/controller/IncomeExpenseController.java +++ b/src/main/java/no/ntnu/idatt1002/demo/controller/IncomeExpenseController.java @@ -150,7 +150,7 @@ public class IncomeExpenseController extends FinanceController { public void initialize() { String path = System.getProperty("user.home"); - filePath = path + "/BudgetBuddyFiles/budget/"; + filePath = path + "/BudgetBuddyFiles/budgets/"; //Initialize columns setColumns(); diff --git a/src/main/java/no/ntnu/idatt1002/demo/controller/MainMenuController.java b/src/main/java/no/ntnu/idatt1002/demo/controller/MainMenuController.java index 5c38b30c..41dfe970 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/controller/MainMenuController.java +++ b/src/main/java/no/ntnu/idatt1002/demo/controller/MainMenuController.java @@ -91,7 +91,7 @@ public class MainMenuController { public void initialize() { String path = System.getProperty("user.home"); - filePath = path + "/BudgetBuddyFiles/budget"; + filePath = path + "/BudgetBuddyFiles/budgets"; //Initialize all registers + overview //TODO: instantiate sub-directories? diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/budget/FileHandlingSelectedBudget.java b/src/main/java/no/ntnu/idatt1002/demo/data/budget/FileHandlingSelectedBudget.java index 68a8fe91..b0dd7c19 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/data/budget/FileHandlingSelectedBudget.java +++ b/src/main/java/no/ntnu/idatt1002/demo/data/budget/FileHandlingSelectedBudget.java @@ -52,11 +52,13 @@ public class FileHandlingSelectedBudget { System.out.print("Updating current file to: " + fileDestination + selectedBudgetFileType + "->" + budgetName); +/* File file = new File(String.format("%s%s", fileDestination, selectedBudgetFileType)); +*/ - if (!file.exists()){ +/* if (!file.exists()){ file.createNewFile(); - } + }*/ try (BufferedWriter bw = new BufferedWriter(new FileWriter(fileDestination + selectedBudgetFileType))) { @@ -108,7 +110,8 @@ public class FileHandlingSelectedBudget { public static boolean createBudgetDirectory(String budgetId) { System.out.println("Creating directory: " + budgetId); File f = new File(budgetId); - return f.mkdir(); + //f.mkdirs(); + return f.mkdirs(); } /** -- GitLab