diff --git a/src/main/java/no/ntnu/idatt1002/demo/controller/AddIngredientController.java b/src/main/java/no/ntnu/idatt1002/demo/controller/AddIngredientController.java index 5710b82b51c8dda2906074d9a8a29d680d0450e0..df9e639d1c029eb0033766bc3184e722716ba0d0 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/controller/AddIngredientController.java +++ b/src/main/java/no/ntnu/idatt1002/demo/controller/AddIngredientController.java @@ -42,6 +42,7 @@ public class AddIngredientController implements Initializable { private Label status; private String statusText = "Added: "; + private String filePath; /** * The initialize method of the controller takes in a URL (location) and ResourceBundle(resources) @@ -55,6 +56,9 @@ public class AddIngredientController implements Initializable { */ @Override public void initialize(URL url, ResourceBundle resourceBundle) { + String path = System.getProperty("user.home"); + filePath = path + "/BudgetBuddyFiles/recipes/"; + listView.setItems(FXCollections.observableArrayList(Arrays .stream(FoodItem.values()).map(value -> value.label).toList())); Platform.runLater(() -> searchBar.requestFocus()); @@ -105,12 +109,12 @@ public class AddIngredientController implements Initializable { item = null; return; } - IngredientsAtHand ingredientsAtHand = FileHandler.readIngredientsAtHand("Fridge"); + IngredientsAtHand ingredientsAtHand = FileHandler.readIngredientsAtHand(filePath + "Fridge"); if (ingredientsAtHand != null) { if (!ingredientsAtHand.atHand(item)) { ingredientsAtHand.addIngredient(item); - FileHandler.writeIngredientsAtHand(ingredientsAtHand, "Fridge"); + FileHandler.writeIngredientsAtHand(ingredientsAtHand, filePath + "Fridge"); if (status.isVisible() && status.getText().isBlank()) { statusText += String.format("%s", item.label); diff --git a/src/main/java/no/ntnu/idatt1002/demo/controller/AllRecipesController.java b/src/main/java/no/ntnu/idatt1002/demo/controller/AllRecipesController.java index 6468496c95f7cca038f2816d21e67dbe1012e895..51e6a2a58db49b696009cf6e5a2ba8292b8e7678 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/controller/AllRecipesController.java +++ b/src/main/java/no/ntnu/idatt1002/demo/controller/AllRecipesController.java @@ -45,6 +45,7 @@ public class AllRecipesController implements Initializable { private ListView<String> allList; private String selectedRecipeName; + private String filePath; /** @@ -62,8 +63,15 @@ public class AllRecipesController implements Initializable { @Override public void initialize(URL url, ResourceBundle resourceBundle) { - ingredientsAtHand = FileHandler.readIngredientsAtHand("Fridge"); - recipeRegister = FileHandler.readRecipeRegister("Recipes"); + String path = System.getProperty("user.home"); + filePath = path + "/BudgetBuddyFiles/recipes/"; + + try { + ingredientsAtHand = FileHandler.readIngredientsAtHand(filePath + "Fridge"); + recipeRegister = FileHandler.readRecipeRegister(filePath + "Recipes"); + } catch (IOException e) { + throw new RuntimeException(e); + } ObservableList<String> recipes; diff --git a/src/main/java/no/ntnu/idatt1002/demo/controller/BudgetController.java b/src/main/java/no/ntnu/idatt1002/demo/controller/BudgetController.java index afb060102df6da01d9eb8bc6de2df322f254db3d..c2b6156d15b377cfaa4af516fdb99888d6635a83 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/controller/BudgetController.java +++ b/src/main/java/no/ntnu/idatt1002/demo/controller/BudgetController.java @@ -92,6 +92,7 @@ public class BudgetController extends FinanceController { private Label amountLeft; BudgetRegister budgetRegister; + private String filePath; /** * Initializes the budget register, the observable budget list and the tableview, along with the @@ -104,27 +105,30 @@ public class BudgetController extends FinanceController { amountCol.setCellValueFactory(new PropertyValueFactory<>("budgetAmount")); descriptionCol.setCellValueFactory(new PropertyValueFactory<>("budgetDescription")); + String path = System.getProperty("user.home"); + filePath = path + "/BudgetBuddyFiles/budgets"; + try { //Attempt to load budget from file general = loadBudgetDataFromFile( - "budgets/" + FileHandlingSelectedBudget - .readSelectedBudget("budgets/SelectedBudget") + "/Budget"); + filePath + "/" + FileHandlingSelectedBudget + .readSelectedBudget(filePath + "/SelectedBudget") + "/Budget"); //Set observable list and table view to generalbudget budgetList = FXCollections.observableArrayList(general.getBudgetItems()); budgetTableView.setItems(budgetList); //Instantiate budget register - if (FileHandlingBudgetArchive.isBudgetRegisterEmpty("budgets/Archive")) { + if (FileHandlingBudgetArchive.isBudgetRegisterEmpty(filePath + "/Archive")) { budgetRegister = new BudgetRegister(); } else { budgetRegister = FileHandlingBudgetArchive - .readBudgetArchive("budgets/Archive"); + .readBudgetArchive(filePath + "/Archive"); } //Refresh pie charts only if the budget is old if (!FileHandlingBudget.isNewBudget( - "budgets/" + FileHandlingSelectedBudget - .readSelectedBudget("budgets/SelectedBudget") + "/Budget")) { + filePath + "/" + FileHandlingSelectedBudget + .readSelectedBudget(filePath + "/SelectedBudget") + "/Budget")) { refreshPieChart(); } } catch (IOException ioe) { @@ -341,8 +345,8 @@ public class BudgetController extends FinanceController { @Override public void saveDataToFile() throws IOException { FileHandlingBudget.writeGeneralBudgetToFile( - "budgets/" + FileHandlingSelectedBudget - .readSelectedBudget("budgets/SelectedBudget") + "/Budget", general); + filePath + "/" + FileHandlingSelectedBudget + .readSelectedBudget( filePath + "/SelectedBudget") + "/Budget", general); } /** @@ -354,7 +358,7 @@ public class BudgetController extends FinanceController { try { budgetRegister.addBudgetName(budgetFolderName); FileHandlingBudgetArchive - .writeBudgetRegisterToArchive(budgetRegister, "budgets/Archive"); + .writeBudgetRegisterToArchive(budgetRegister, filePath + "/Archive"); } catch (IOException ioe) { showErrorDialogBox(ioe.getMessage(), ioe.getMessage(), ioe.getMessage()); } @@ -380,17 +384,20 @@ public class BudgetController extends FinanceController { "By pressing OK you will complete the budget setup?", "" + "Have you done all the changes you wished to make?"); if (isConfirmed.isPresent() && isConfirmed.get() == ButtonType.OK) { - loader.setLocation(getClass().getResource("/view/dualList.fxml")); + //Adds unused categories to the table + general.addUnusedCategories(); + updateBudgetRegister(FileHandlingSelectedBudget + .readSelectedBudget(filePath + "/SelectedBudget")); + //Always saving the data when switching to main menu + saveDataToFile(); + + loader.setLocation(getClass().getResource("/view/MainMenuNew.fxml")); + + //loader.setLocation(getClass().getResource("/view/dualList.fxml")); } else { return; } - //Adds unused categories to the table - general.addUnusedCategories(); - updateBudgetRegister(FileHandlingSelectedBudget - .readSelectedBudget("budgets/SelectedBudget")); - loader.setLocation(getClass().getResource("/view/MainMenuNew.fxml")); - //Always saving the data when switching to main menu - saveDataToFile(); + } else if (event.getSource() == backBtn) { Optional<ButtonType> isConfirmed = showConfirmationDialog("Return confirmation", "Are you sure you want to go back?", "The changes made in this and the previous" 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 5fab912a63a81cdd5047485983106c0f95ffc26e..41b6d0425ba7f50e1bda34d1fd76e747e39a17c3 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/controller/CreateBudgetController.java +++ b/src/main/java/no/ntnu/idatt1002/demo/controller/CreateBudgetController.java @@ -47,12 +47,18 @@ public class CreateBudgetController { @FXML private Label errorMsg; + private String filePath; + /** * Initializes the window. Finds the current month, used to make the created budget's name. Adds * an event filter to the okBtn. */ @FXML public void initialize() { + + String path = System.getProperty("user.home"); + filePath = path + "/BudgetBuddyFiles/budgets"; + currentMonth = String.valueOf(LocalDate.now().getMonth()); okBtn.addEventFilter( ActionEvent.ACTION, event -> { @@ -151,13 +157,19 @@ public class CreateBudgetController { public boolean createNewFiles(String budgetName) { 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( - "budgets/" + currentMonth + budgetName); - FileHandlingSelectedBudget.createNewIncomeFile("budgets/" + currentMonth + budgetName, + filePath + "/" + currentMonth + budgetName); + + FileHandlingSelectedBudget.createNewIncomeFile(filePath + "/" + currentMonth + budgetName, "Income"); - FileHandlingSelectedBudget.createNewExpenseFile("budgets/" + currentMonth + budgetName, + FileHandlingSelectedBudget.createNewExpenseFile(filePath + "/" + currentMonth + budgetName, "Expense"); - FileHandlingSelectedBudget.createNewBudgetFile("budgets/" + currentMonth + budgetName, + FileHandlingSelectedBudget.createNewBudgetFile(filePath + "/" + currentMonth + budgetName, "Budget"); } catch (IOException ioe) { empty = false; @@ -176,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/FinanceController.java b/src/main/java/no/ntnu/idatt1002/demo/controller/FinanceController.java index 51fe14adaf62274534256579dbe36d746d1127be..dfc9714f788d60cad5ab9fedaf6f3c7c023a5f7e 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/controller/FinanceController.java +++ b/src/main/java/no/ntnu/idatt1002/demo/controller/FinanceController.java @@ -25,6 +25,7 @@ public abstract class FinanceController { private GeneralBudget general; + /** * Method for handling the adding of new entries in the tableview. * @@ -94,23 +95,24 @@ public abstract class FinanceController { alert.showAndWait(); } + //TODO: May need to alter path? /** * Method that either reads data from a file with which it fills a budget register, if this is an * old budget, or instantiates a budget register if this is a new budget. * - * @param fileName The name of the file that is being read from. + * @param fileDestination The name of the file that is being read from. * @return An object of type GeneralBudget. * @throws IOException If an error occurs while reading from the file. */ - public GeneralBudget loadBudgetDataFromFile(String fileName) throws IOException { + public GeneralBudget loadBudgetDataFromFile(String fileDestination) throws IOException { //Instantiate new budget System.out.println("this is just beofre isempty"); - System.out.println(fileName); - if (FileHandlingBudget.isEmpty(fileName)) { + System.out.println(fileDestination); + if (FileHandlingBudget.isEmpty(fileDestination)) { general = new GeneralBudget(1000); } else { //Load previous budget try { - general = FileHandlingBudget.readGeneralBudgetFromFile(fileName); + general = FileHandlingBudget.readGeneralBudgetFromFile(fileDestination); } catch (IOException e) { showErrorDialogBox("File error", "Error in reading from fil", "An error occurred" + "when reading GeneralBudget from the file"); @@ -123,16 +125,16 @@ public abstract class FinanceController { * Method that either reads data from a file with which it fills an income register, if older * changes exist, or instantiates an income register if the file is empty. * - * @param fileName The name of the file that is being read from. + * @param fileDestination The name of the file that is being read from. * @return An object of type IncomeRegister. */ - public IncomeRegister loadIncomeDataFromFile(String fileName) { + public IncomeRegister loadIncomeDataFromFile(String fileDestination) { //Instantiate new incomeRegister try { - if (FileHandling.isEmpty(fileName)) { + if (FileHandling.isEmpty(fileDestination)) { incomeRegister = new IncomeRegister(); } else { //Load previous income register - incomeRegister = FileHandling.readIncomeRegisterFromFile(fileName); + incomeRegister = FileHandling.readIncomeRegisterFromFile(fileDestination); } } catch (IOException ioe) { ioe.printStackTrace(); @@ -146,16 +148,16 @@ public abstract class FinanceController { * Method that either reads data from a file with which it fills an expense register, if older * changes exist, or instantiates an expense register if the file is empty. * - * @param fileName The name of the file that is being read from. + * @param fileDestination The name of the file that is being read from. * @return An object of type IncomeRegister. */ - public ExpenseRegister loadExpenseDataFromFile(String fileName) { + public ExpenseRegister loadExpenseDataFromFile(String fileDestination) { //Instantiate expense register try { - if (FileHandling.isEmpty(fileName)) { + if (FileHandling.isEmpty(fileDestination)) { expenseRegister = new ExpenseRegister(); } else { //Load previous income register - expenseRegister = FileHandling.readExpenseRegisterFromFile(fileName); + expenseRegister = FileHandling.readExpenseRegisterFromFile(fileDestination); } } catch (IOException ioe) { showErrorDialogBox("File reading error", "Error in reading from file", "Could not" 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 135d365b1b252336deaea4e476a5d5d17a2650ae..3e8742f141c4bf6208b42c7c84254ecd5d23ded1 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/controller/FirstMenuController.java +++ b/src/main/java/no/ntnu/idatt1002/demo/controller/FirstMenuController.java @@ -1,6 +1,7 @@ package no.ntnu.idatt1002.demo.controller; -import java.io.IOException; +import java.io.*; + import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; @@ -16,17 +17,62 @@ import javafx.stage.Screen; import javafx.stage.Stage; import no.ntnu.idatt1002.demo.data.budget.FileHandlingSelectedBudget; import no.ntnu.idatt1002.demo.data.economics.Income; +import no.ntnu.idatt1002.demo.data.recipes.FileHandler; +import no.ntnu.idatt1002.demo.data.recipes.RecipeRegister; /** * Controller for the FirstMenu view. Handles user input on the buttons in the scene. */ public class FirstMenuController { + private String filePath; + + private RecipeRegister startRegister; + + //TODO: /** * 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 + "/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(); + } + File recipes = new File(buddyPath + "/recipes"); + if (!recipes.exists()) { + recipes.mkdirs(); + } + + /* File recipesFile = new File(buddyPath + "/recipes/Recipes.register"); + if (!recipesFile.exists()) { + System.out.printf("\"The file must be filled\""); + try { + recipesFile.createNewFile(); + System.out.printf("Going to fill the register"); + + recipesFile.createNewFile(); + + } catch (IOException e) { + throw new RuntimeException(e); + } + }*/ + + } /** @@ -64,7 +110,7 @@ public class FirstMenuController { try { //Only proceeds to next scene if there is a budget selected if (FileHandlingSelectedBudget - .readSelectedBudget("budgets/SelectedBudget") != null) { + .readSelectedBudget(filePath + "/SelectedBudget") != null) { switchNext(event); } } catch (IOException ioe) { @@ -134,7 +180,7 @@ public class FirstMenuController { try { //Only switches scenes if there is a budget that can be selected. if (FileHandlingSelectedBudget - .readSelectedBudget("budgets/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 b5065771230c864497b716619178fb4cd0f60cce..97c8bc9b8ecfccd6292ade427f4d547a4f139133 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/controller/IncomeExpenseController.java +++ b/src/main/java/no/ntnu/idatt1002/demo/controller/IncomeExpenseController.java @@ -134,6 +134,8 @@ public class IncomeExpenseController extends FinanceController { private ObservableList<Expense> expenses; + private String filePath; + @FXML private PieChart expensePieChart; @@ -146,17 +148,21 @@ public class IncomeExpenseController extends FinanceController { */ @FXML public void initialize() { + + String path = System.getProperty("user.home"); + filePath = path + "/BudgetBuddyFiles/budgets/"; + //Initialize columns setColumns(); try { //Initialize registers incomeRegister = loadIncomeDataFromFile( - "budgets/" + FileHandlingSelectedBudget - .readSelectedBudget("budgets/SelectedBudget") + "/Income"); + filePath + FileHandlingSelectedBudget + .readSelectedBudget( filePath + "SelectedBudget") + "/Income"); expenseRegister = loadExpenseDataFromFile( - "budgets/" + FileHandlingSelectedBudget - .readSelectedBudget("budgets/SelectedBudget") + "/Expense"); + filePath + FileHandlingSelectedBudget + .readSelectedBudget(filePath + "SelectedBudget") + "/Expense"); } catch (IOException ioe) { showErrorDialogBox("File reading error", "Could not read register", ""); } @@ -263,6 +269,7 @@ public class IncomeExpenseController extends FinanceController { @Override public void handleAddBtn(javafx.event.ActionEvent event) { int sizeBf = (expenseRegister.getItems().size() + incomeRegister.getItems().size()); + System.out.printf("handleAdd button"); if (event.getSource() == addIncome) { handleAddIncome(); @@ -548,11 +555,11 @@ public class IncomeExpenseController extends FinanceController { @Override public void saveDataToFile() throws IOException { FileHandling.writeItemRegisterToFile(incomeRegister, - "budgets/" + FileHandlingSelectedBudget - .readSelectedBudget("budgets/SelectedBudget") + "/Income"); + filePath + FileHandlingSelectedBudget + .readSelectedBudget(filePath + "SelectedBudget") + "/Income"); FileHandling.writeItemRegisterToFile(expenseRegister, - "budgets/" + FileHandlingSelectedBudget - .readSelectedBudget("budgets/SelectedBudget") + "/Expense"); + filePath + FileHandlingSelectedBudget + .readSelectedBudget(filePath + "SelectedBudget") + "/Expense"); } /** @@ -571,8 +578,8 @@ public class IncomeExpenseController extends FinanceController { String disposableIncomeAsString = String.valueOf( incomeRegister.getTotalSum() - expenseRegister.getTotalSum()); FileHandlingBudget.writeMaxAmountToFile( - "budgets/" + FileHandlingSelectedBudget - .readSelectedBudget("budgets/SelectedBudget") + "/Budget", disposableIncomeAsString); + filePath + FileHandlingSelectedBudget + .readSelectedBudget(filePath + "SelectedBudget") + "/Budget", disposableIncomeAsString); } @@ -616,9 +623,9 @@ public class IncomeExpenseController extends FinanceController { "Are you sure you want to go back?", "The budget you are creating will be deleted"); if (isConfirmed.isPresent() && isConfirmed.get() == ButtonType.OK) { loader.setLocation(getClass().getResource("/view/FirstMenu.fxml")); - FileHandlingSelectedBudget.deleteBudgetDirectory("budgets/" + FileHandlingSelectedBudget - .readSelectedBudget("budgets/SelectedBudget")); - FileHandlingSelectedBudget.clearSelectedBudget("budgets/SelectedBudget"); + FileHandlingSelectedBudget.deleteBudgetDirectory(filePath + FileHandlingSelectedBudget + .readSelectedBudget(filePath + "SelectedBudget")); + FileHandlingSelectedBudget.clearSelectedBudget(filePath + "SelectedBudget"); } else { return; } 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 7b13faf4d575a0ce80438af4bd6e43f9f57b25e1..8fd2f2b5b82392d72996c0252dd475109cca0ae5 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/controller/MainMenuController.java +++ b/src/main/java/no/ntnu/idatt1002/demo/controller/MainMenuController.java @@ -82,22 +82,29 @@ public class MainMenuController { Overview overview; + private String filePath; + /** * Initializes the registers and overviews in addition to all dynamic labels and progress bars. */ @FXML public void initialize() { + + String path = System.getProperty("user.home"); + filePath = path + "/BudgetBuddyFiles/budgets/"; + //Initialize all registers + overview + //TODO: instantiate sub-directories? try { incomeRegister = loadIncomeDataFromFile( - "budgets/" + FileHandlingSelectedBudget - .readSelectedBudget("budgets/SelectedBudget") + "/Income"); + filePath + FileHandlingSelectedBudget + .readSelectedBudget(filePath + "SelectedBudget") + "/Income"); expenseRegister = loadExpenseDataFromFile( - "budgets/" + FileHandlingSelectedBudget - .readSelectedBudget("budgets/SelectedBudget") + "/Expense"); + filePath + FileHandlingSelectedBudget + .readSelectedBudget(filePath + "SelectedBudget") + "/Expense"); generalBudget = loadBudgetDataFromFile( - "budgets/" + FileHandlingSelectedBudget - .readSelectedBudget("budgets/SelectedBudget") + "/Budget"); + filePath + FileHandlingSelectedBudget + .readSelectedBudget(filePath + "SelectedBudget") + "/Budget"); overview = new Overview(incomeRegister, expenseRegister, generalBudget); } catch (IOException | IllegalArgumentException ioe) { ioe.printStackTrace(); @@ -238,7 +245,7 @@ public class MainMenuController { public GeneralBudget loadBudgetDataFromFile(String fileDestination) throws IOException { //Instantiate new budget if (FileHandlingBudget.isEmpty(fileDestination)) { - System.out.println("hey"); + System.out.println("hey from MainMenuController"); generalBudget = new GeneralBudget(1000); } else { //Load previous budget try { diff --git a/src/main/java/no/ntnu/idatt1002/demo/controller/RecipeTileController.java b/src/main/java/no/ntnu/idatt1002/demo/controller/RecipeTileController.java index b72dfd2bf7c5a21cd87f28e1385cada002a4127f..d356866019498c60838551b8f02004bb17431819 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/controller/RecipeTileController.java +++ b/src/main/java/no/ntnu/idatt1002/demo/controller/RecipeTileController.java @@ -33,6 +33,7 @@ public class RecipeTileController implements Initializable { private Label missingTag; private RecipeRegister recipeRegister; + private String filePath; /** * The initialize method of the controller takes in a URL (location) and ResourceBundle(resources) @@ -45,7 +46,15 @@ public class RecipeTileController implements Initializable { */ @Override public void initialize(URL url, ResourceBundle resourceBundle) { - recipeRegister = FileHandler.readRecipeRegister("Recipes"); + + String path = System.getProperty("user.home"); + filePath = path + "/BudgetBuddyFiles/recipes/"; + + try { + recipeRegister = FileHandler.readRecipeRegister(filePath + "Recipes"); + } catch (IOException e) { + throw new RuntimeException(e); + } nameTag.setWrapText(true); } diff --git a/src/main/java/no/ntnu/idatt1002/demo/controller/SelectBudgetController.java b/src/main/java/no/ntnu/idatt1002/demo/controller/SelectBudgetController.java index ec257099cb5b9dc4494fdffcaca6c62ca117d167..73bc45c0fdf2efe590dcf4be1aa70302326c029f 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/controller/SelectBudgetController.java +++ b/src/main/java/no/ntnu/idatt1002/demo/controller/SelectBudgetController.java @@ -36,6 +36,7 @@ public class SelectBudgetController { private ListView<String> budgetListView; private BudgetRegister budgetRegister; + private String filePath; /** * Initializes the view when it is loaded. Prepares the view by adding event filters, @@ -43,6 +44,9 @@ public class SelectBudgetController { */ @FXML public void initialize() { + String path = System.getProperty("user.home"); + filePath = path + "/BudgetBuddyFiles/budgets"; + //Prevent users from choosing nothing okBtn.addEventFilter( ActionEvent.ACTION, event -> { @@ -54,10 +58,10 @@ public class SelectBudgetController { //Load budget register from file. try { - if (FileHandlingBudgetArchive.isBudgetRegisterEmpty("budgets/Archive")) { + if (FileHandlingBudgetArchive.isBudgetRegisterEmpty(filePath + "/Archive")) { budgetRegister = new BudgetRegister(); } else { - budgetRegister = FileHandlingBudgetArchive.readBudgetArchive("budgets/Archive"); + budgetRegister = FileHandlingBudgetArchive.readBudgetArchive(filePath + "/Archive"); } } catch (IOException ioe) { ioe.printStackTrace(); @@ -87,7 +91,7 @@ public class SelectBudgetController { public void selectBudget(Event event) { try { String name = budgetListView.getSelectionModel().getSelectedItem(); - FileHandlingSelectedBudget.updateSelectedBudget(name, "budgets/SelectedBudget"); + FileHandlingSelectedBudget.updateSelectedBudget(name, filePath + "/SelectedBudget"); } catch (IOException ioe) { showErrorDialogBox(ioe.getMessage(), ioe.getMessage(), ioe.getMessage()); } @@ -104,7 +108,7 @@ public class SelectBudgetController { @FXML public void exitWindow(ActionEvent event) { try { - FileHandlingSelectedBudget.clearSelectedBudget("budgets/SelectedBudget"); + FileHandlingSelectedBudget.clearSelectedBudget(filePath + "/SelectedBudget"); } catch (IOException ioe) { showErrorDialogBox(ioe.getMessage(), ioe.getMessage(), ioe.getMessage()); } diff --git a/src/main/java/no/ntnu/idatt1002/demo/controller/SuggestRecipesController.java b/src/main/java/no/ntnu/idatt1002/demo/controller/SuggestRecipesController.java index e0a992d7b3656a8c212f362d223d2fb03a58097f..25769b3ae3780c23cff269e843da8ad28175dde1 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/controller/SuggestRecipesController.java +++ b/src/main/java/no/ntnu/idatt1002/demo/controller/SuggestRecipesController.java @@ -1,6 +1,6 @@ package no.ntnu.idatt1002.demo.controller; -import java.io.IOException; +import java.io.*; import java.net.URL; import java.util.ArrayList; import java.util.Optional; @@ -65,6 +65,8 @@ public class SuggestRecipesController implements Initializable { private final ArrayList<VBox> currentRecipeTiles = new ArrayList<>(4); + private String filePath; + /** * The initialize method of the controller takes in a URL (location) and ResourceBundle(resources) @@ -79,9 +81,42 @@ public class SuggestRecipesController implements Initializable { */ @Override public void initialize(URL url, ResourceBundle resourceBundle) { - readIngredientsAtHand(); + String path = System.getProperty("user.home"); + filePath = path + "/BudgetBuddyFiles/recipes/"; + + File atHand = new File(filePath + "Fridge.register"); + if(!atHand.exists()){ + try { + atHand.createNewFile(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } - recipeRegister = FileHandler.readRecipeRegister("Recipes"); + File recipesFile = new File(filePath + "Recipes.register"); + if(!recipesFile.exists()){ + try { + recipesFile.createNewFile(); + + recipeRegister = FileHandler.readRecipeRegister("src/main/resources/recipes/Recipes"); + FileHandler.writeRegister(recipeRegister, filePath + "Recipes"); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + + try { + readIngredientsAtHand(); + } catch (IOException e) { + throw new RuntimeException(e); + } + + try { + recipeRegister = FileHandler.readRecipeRegister(filePath + "Recipes"); + } catch (IOException e) { + throw new RuntimeException(e); + } if (recipeRegister == null) { recipeRegister = new RecipeRegister(); } @@ -253,8 +288,8 @@ public class SuggestRecipesController implements Initializable { * fridge list is filled with the food types at hand sorted alphabetically by calling the method * 'updateFridge'. */ - private void readIngredientsAtHand() { - ingredientsAtHand = FileHandler.readIngredientsAtHand("Fridge"); + private void readIngredientsAtHand() throws IOException { + ingredientsAtHand = FileHandler.readIngredientsAtHand(filePath + "Fridge"); if (ingredientsAtHand == null) { ingredientsAtHand = new IngredientsAtHand(); fridgeList.setItems(FXCollections.observableArrayList(new ArrayList<>())); @@ -271,7 +306,7 @@ public class SuggestRecipesController implements Initializable { * @throws IOException If the method fails to write the IngredientsAtHand object to file. */ private void storeIngredientsAtHand() throws IOException { - FileHandler.writeIngredientsAtHand(ingredientsAtHand, "Fridge"); + FileHandler.writeIngredientsAtHand(ingredientsAtHand, filePath + "Fridge"); updateFridge(); } diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/budget/FileHandlingBudget.java b/src/main/java/no/ntnu/idatt1002/demo/data/budget/FileHandlingBudget.java index fc06e5af339d1a9b48d66f753dca52160aa65cf9..b54d9ce171a89b5d97ab84f3b5b753039fab1d3f 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/data/budget/FileHandlingBudget.java +++ b/src/main/java/no/ntnu/idatt1002/demo/data/budget/FileHandlingBudget.java @@ -16,7 +16,6 @@ import no.ntnu.idatt1002.demo.data.economics.ExpenseCategory; public class FileHandlingBudget { private static final String fileType = ".budget"; - private static final String filePath = "src/main/resources/"; private static final String maxAmount = "maxAmount="; private static final String budgetAmount = "budgetAmount="; private static final String budgetCategory = "budgetCategory="; @@ -26,28 +25,28 @@ public class FileHandlingBudget { * Method for writing (adding) a budget to a file. * * @param generalBudget the budget you want to write to a file. - * @param fileTitle the name of the file you want to check + * @param fileDestination the path and name of the file you want to check * @throws IOException if an input or output exception occurred. */ - public static void writeGeneralBudgetToFile(String fileTitle, GeneralBudget generalBudget) + public static void writeGeneralBudgetToFile(String fileDestination, GeneralBudget generalBudget) throws IOException { - try (BufferedWriter bw = new BufferedWriter(new FileWriter(filePath + fileTitle + fileType))) { + try (BufferedWriter bw = new BufferedWriter(new FileWriter(fileDestination + fileType))) { bw.write(generalBudget.toString()); } catch (IOException ex) { ex.printStackTrace(); - throw new IOException("Error writing to file: " + fileTitle); + throw new IOException("Error writing to file: " + fileDestination); } } /** * Method for checking if a .budget file is empty. * - * @param fileTitle the name of the file you want to check + * @param fileDestination the path and name of the file you want to check * @return true or false depending on if file is empty. * @throws IOException if an input or output exception occurred. */ - public static boolean isEmpty(String fileTitle) throws IOException { - try (BufferedReader br = new BufferedReader(new FileReader(filePath + fileTitle + fileType))) { + public static boolean isEmpty(String fileDestination) throws IOException { + try (BufferedReader br = new BufferedReader(new FileReader(fileDestination + fileType))) { return br.readLine() == null; } } @@ -56,13 +55,13 @@ public class FileHandlingBudget { /** * Method for checking if a .budget file is new (no categories) or old (has budget categories). * - * @param fileTitle The name of the file + * @param fileDestination The path and name of the file * @return True, if the budget is new. Else, returns false * @throws IOException if an input or output exception occurred. */ - public static boolean isNewBudget(String fileTitle) throws IOException { + public static boolean isNewBudget(String fileDestination) throws IOException { try (BufferedReader br = new BufferedReader( - new FileReader(filePath + fileTitle + fileType))) { + new FileReader(fileDestination + fileType))) { for (int i = 0; i < 2; ++i) { br.readLine(); @@ -84,8 +83,7 @@ public class FileHandlingBudget { */ public static void writeMaxAmountToFile(String fileDestination, String maxAmount) throws IOException { - try (BufferedWriter bw = new BufferedWriter(new FileWriter(filePath - + fileDestination + fileType))) { + try (BufferedWriter bw = new BufferedWriter(new FileWriter(fileDestination + fileType))) { bw.write("maxAmount=" + maxAmount); } catch (IOException ex) { throw new IOException("Error writing to file: " + fileDestination); @@ -95,18 +93,18 @@ public class FileHandlingBudget { /** * Method for reading (getting) a Budget from a file. * - * @param fileTitle the name of the file you want to read from. + * @param fileDestination the name of the file you want to read from. * @return the GeneralBudget from the file. * @throws IOException if an input or output exception occurred. */ - public static GeneralBudget readGeneralBudgetFromFile(String fileTitle) throws IOException { + public static GeneralBudget readGeneralBudgetFromFile(String fileDestination) throws IOException { GeneralBudget generalBudget = null; double maxAmount = 0; double budgetAmount = 0; ExpenseCategory expenseCategory = null; String budgetDescription = null; - try (BufferedReader br = new BufferedReader(new FileReader(filePath + fileTitle + fileType))) { + try (BufferedReader br = new BufferedReader(new FileReader(fileDestination + fileType))) { String line; String nextLine = br.readLine(); while ((line = nextLine) != null) { diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/budget/FileHandlingBudgetArchive.java b/src/main/java/no/ntnu/idatt1002/demo/data/budget/FileHandlingBudgetArchive.java index bcaf1ed651d9794094e59d4b6e35f7c7845b33c8..f3a85b1d67e8035b0e34e008661452f8c9788e5c 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/data/budget/FileHandlingBudgetArchive.java +++ b/src/main/java/no/ntnu/idatt1002/demo/data/budget/FileHandlingBudgetArchive.java @@ -1,10 +1,6 @@ package no.ntnu.idatt1002.demo.data.budget; -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; +import java.io.*; //Set titler på hvert vindu - kan gjøres i switch scene //Dynamic days left @@ -26,7 +22,7 @@ import java.io.IOException; * @since 19.04.2023 */ public class FileHandlingBudgetArchive { - private static final String filePath = "src/main/resources/"; + private static final String fileType = ".archive"; /** @@ -39,7 +35,9 @@ public class FileHandlingBudgetArchive { public static void writeBudgetRegisterToArchive(BudgetRegister budgetNames, String fileDestination) throws IOException { - try (BufferedWriter bw = new BufferedWriter(new FileWriter(filePath + fileDestination + fileType))) { + File file = new File(String.format("%s%s", fileDestination, fileType)); + + try (BufferedWriter bw = new BufferedWriter(new FileWriter(fileDestination + fileType))) { bw.write(budgetNames.toString()); } catch (IOException ioe) { throw new IOException("Could not write to file: Archive", ioe); @@ -55,8 +53,7 @@ public class FileHandlingBudgetArchive { * @throws IOException if an input or output exception occurred. */ public static boolean isBudgetRegisterEmpty(String fileDestination) throws IOException { - try (BufferedReader br = new BufferedReader(new FileReader(filePath - + fileDestination + fileType))) { + try (BufferedReader br = new BufferedReader(new FileReader(fileDestination + fileType))) { return br.readLine() == null; } } @@ -74,7 +71,7 @@ public class FileHandlingBudgetArchive { String line; try (BufferedReader br = new BufferedReader( - new FileReader(filePath + fileDestination + fileType))) { + new FileReader(fileDestination + fileType))) { String nextLine = br.readLine(); while ((line = nextLine) != null) { 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 2db069c24308a5947a7043100664246fea19a595..b0dd7c19e2ebe72465350675b2173c0d698b5682 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 @@ -15,8 +15,6 @@ import java.io.IOException; * @since 19.04.2023 */ public class FileHandlingSelectedBudget { - - private static final String filePath = "src/main/resources/"; private static final String selectedBudgetFileType = ".current"; private static final String registerFileType = ".register"; private static final String budgetFileType = ".budget"; @@ -29,9 +27,11 @@ public class FileHandlingSelectedBudget { * @throws IOException if an input or output exception occurred. */ public static String readSelectedBudget(String fileDestination) throws IOException { - System.out.println("Reading current file: " + filePath + fileDestination - + selectedBudgetFileType); - try (BufferedReader br = new BufferedReader(new FileReader(filePath + fileDestination + + System.out.println("Reading current file: " + fileDestination + + selectedBudgetFileType); + + try (BufferedReader br = new BufferedReader(new FileReader(fileDestination + selectedBudgetFileType))) { return br.readLine(); } catch (IOException ioException) { @@ -48,9 +48,19 @@ public class FileHandlingSelectedBudget { */ public static void updateSelectedBudget(String budgetName, String fileDestination) throws IOException { - System.out.print("Updating current file to: " + filePath + fileDestination + + System.out.print("Updating current file to: " + fileDestination + selectedBudgetFileType + "->" + budgetName); - try (BufferedWriter bw = new BufferedWriter(new FileWriter(filePath + fileDestination + +/* + File file = new File(String.format("%s%s", fileDestination, selectedBudgetFileType)); +*/ + +/* if (!file.exists()){ + file.createNewFile(); + }*/ + + try (BufferedWriter bw = new BufferedWriter(new FileWriter(fileDestination + selectedBudgetFileType))) { bw.write(budgetName); System.out.println("-----Budget name: " + budgetName); @@ -67,9 +77,9 @@ public class FileHandlingSelectedBudget { * @throws IOException if an input or output exception occurred. */ public static void clearSelectedBudget(String fileDestination) throws IOException { - System.out.println("Clearing current file from: " + filePath + fileDestination + System.out.println("Clearing current file from: " + fileDestination + selectedBudgetFileType); - try (BufferedWriter bw = new BufferedWriter(new FileWriter(filePath + fileDestination + try (BufferedWriter bw = new BufferedWriter(new FileWriter(fileDestination + selectedBudgetFileType))) { bw.write(""); } catch (IOException ex) { @@ -86,8 +96,7 @@ public class FileHandlingSelectedBudget { * @throws IOException if an input or output exception occurred. */ public static boolean isSelectedBudgetEmpty(String fileDestination) throws IOException { - try (BufferedReader br = new BufferedReader(new FileReader(filePath - + fileDestination + selectedBudgetFileType))) { + try (BufferedReader br = new BufferedReader(new FileReader(fileDestination + selectedBudgetFileType))) { return br.readLine() == null; } } @@ -99,9 +108,10 @@ public class FileHandlingSelectedBudget { * @return True, if a directory is successfully created. Else, returns false. */ public static boolean createBudgetDirectory(String budgetId) { - System.out.println("Creating directory: " + filePath + budgetId); - File f = new File(filePath + budgetId); - return f.mkdir(); + System.out.println("Creating directory: " + budgetId); + File f = new File(budgetId); + //f.mkdirs(); + return f.mkdirs(); } /** @@ -114,7 +124,7 @@ public class FileHandlingSelectedBudget { * @return True, if the directory is successfully deleted. Else, returns false. */ public static boolean deleteBudgetDirectory(String budgetDestination) { - File targetDirectory = new File(filePath + budgetDestination); + File targetDirectory = new File(budgetDestination); System.out.println("Deleting directory:" + targetDirectory.getPath()); String[] entries = targetDirectory.list(); @@ -123,7 +133,6 @@ public class FileHandlingSelectedBudget { File currentFile = new File(targetDirectory.getPath(), file); currentFile.delete(); } - return targetDirectory.delete(); } @@ -137,11 +146,12 @@ public class FileHandlingSelectedBudget { * @throws IOException if an input or output exception occurred. */ public static boolean createNewIncomeFile(String budgetDestination, String incomeFileTitle) + // TODO: Se over throws IOException { - System.out.println("Income filePath: " + filePath + budgetDestination + "/" + incomeFileTitle + System.out.println("Income filePath: " + budgetDestination + "/" + incomeFileTitle + registerFileType); File incomeFile = new File( - filePath + budgetDestination + "/" + incomeFileTitle + registerFileType); + budgetDestination + "/" + incomeFileTitle + registerFileType); return incomeFile.createNewFile(); } @@ -157,10 +167,10 @@ public class FileHandlingSelectedBudget { public static boolean createNewExpenseFile(String budgetDestination, String expenseFileTitle) throws IOException { - System.out.println("Expense filePath: " + filePath + budgetDestination + "/" + expenseFileTitle + System.out.println("Expense filePath: " + budgetDestination + "/" + expenseFileTitle + registerFileType); File expenseFile = new File( - filePath + budgetDestination + "/" + expenseFileTitle + registerFileType); + budgetDestination + "/" + expenseFileTitle + registerFileType); return expenseFile.createNewFile(); } @@ -175,10 +185,10 @@ public class FileHandlingSelectedBudget { */ public static boolean createNewBudgetFile(String budgetDestination, String budgetFileTitle) throws IOException { - System.out.println("Budget filePath: " + filePath + budgetDestination + "/" + budgetFileTitle + System.out.println("Budget filePath: " + budgetDestination + "/" + budgetFileTitle + budgetFileType); File budgetFile = new File( - filePath + budgetDestination + "/" + budgetFileTitle + budgetFileType); + budgetDestination + "/" + budgetFileTitle + budgetFileType); return budgetFile.createNewFile(); } } diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/economics/FileHandling.java b/src/main/java/no/ntnu/idatt1002/demo/data/economics/FileHandling.java index bfcf8c54d6fa7437f4c84eeb8f6114e5f27bfee0..23cc7c2f55502c0bfef617de94bb45041fa51351 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/data/economics/FileHandling.java +++ b/src/main/java/no/ntnu/idatt1002/demo/data/economics/FileHandling.java @@ -15,7 +15,11 @@ import java.time.LocalDate; */ public class FileHandling { - private static final String filePath = "src/main/resources/"; + private static final String jarFilePath = "src/main/resources/"; + + private static final String path = System.getProperty("user.home"); + private static final String filePath = path + "/BudgetBuddyFiles/"; + private static final String fileType = ".register"; private static final String date = "date="; private static final String description = "description="; @@ -28,13 +32,13 @@ public class FileHandling { * * @param <T> Subclass of item, either expense or income. * @param itemRegister the ItemRegister you want to write to a file. - * @param fileTitle the name of the file you want to check + * @param fileDestination the path and name of the file you want to check * @throws IOException if an input or output exception occurred. */ public static <T extends Item> void writeItemRegisterToFile(final ItemRegister<T> itemRegister, - String fileTitle) throws IOException { - try (BufferedWriter bw = new BufferedWriter(new FileWriter(filePath + fileTitle + fileType))) { + String fileDestination) throws IOException { + try (BufferedWriter bw = new BufferedWriter(new FileWriter(fileDestination + fileType))) { if (itemRegister.isEmpty()) { bw.write(""); } else { @@ -48,12 +52,12 @@ public class FileHandling { /** * Method for checking if a .register file is empty. * - * @param fileTitle the name of the file you want to check + * @param fileDestination the path and name of the file you want to check * @return true or false depending on if file is empty. * @throws IOException if an input or output exception occurred. */ - public static boolean isEmpty(String fileTitle) throws IOException { - try (BufferedReader br = new BufferedReader(new FileReader(filePath + fileTitle + fileType))) { + public static boolean isEmpty(String fileDestination) throws IOException { + try (BufferedReader br = new BufferedReader(new FileReader(fileDestination + fileType))) { return br.readLine() == null; } } @@ -61,18 +65,18 @@ public class FileHandling { /** * Method for reading (getting) an IncomeRegister from a file. * - * @param fileTitle the name of the file you want to read from. + * @param fileDestination the name of the file you want to read from. * @return the IncomeRegister from the file. * @throws IOException if an input or output exception occurred. */ - public static IncomeRegister readIncomeRegisterFromFile(String fileTitle) throws IOException { + public static IncomeRegister readIncomeRegisterFromFile(String fileDestination) throws IOException { IncomeRegister incomeRegister = new IncomeRegister(); LocalDate date = null; String description = ""; double amount = 0; boolean reoccuring = false; IncomeCategory incomeCategory = null; - try (BufferedReader br = new BufferedReader(new FileReader(filePath + fileTitle + fileType))) { + try (BufferedReader br = new BufferedReader(new FileReader(fileDestination + fileType))) { String line; String nextLine = br.readLine(); while ((line = nextLine) != null) { @@ -110,18 +114,18 @@ public class FileHandling { /** * Method for reading (getting) an ExpenseRegister from a file. * - * @param fileTitle the name of the file you want to read from. + * @param fileDestination the name of the file you want to read from. * @return the ExpenseRegister from the file. * @throws IOException if an input or output exception occurred */ - public static ExpenseRegister readExpenseRegisterFromFile(String fileTitle) throws IOException { + public static ExpenseRegister readExpenseRegisterFromFile(String fileDestination) throws IOException { ExpenseRegister expenseRegister = new ExpenseRegister(); LocalDate date = null; String description = ""; double amount = 0; boolean reoccuring = false; ExpenseCategory expenseCategory = null; - try (BufferedReader br = new BufferedReader(new FileReader(filePath + fileTitle + fileType))) { + try (BufferedReader br = new BufferedReader(new FileReader(fileDestination + fileType))) { String line; String nextLine = br.readLine(); while ((line = nextLine) != null) { diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/recipes/FileHandler.java b/src/main/java/no/ntnu/idatt1002/demo/data/recipes/FileHandler.java index 958aec7693c2903596b52f28c6990256adeec685..1c9cd2ce43a6a62cf3824d7cfdee5d33edd784a8 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/data/recipes/FileHandler.java +++ b/src/main/java/no/ntnu/idatt1002/demo/data/recipes/FileHandler.java @@ -13,8 +13,6 @@ import java.util.Scanner; public class FileHandler { private static final String fileType = ".register"; - private static final String filePath = "src/main/resources/recipes/"; - /** * The method takes a RecipeRegister object and a String as parameters. The recipe register is then written @@ -29,14 +27,14 @@ public class FileHandler { * file, in which case the stacktrace is printed to terminal. * * @param recipeRegister A recipe register object that is to be written to file. - * @param title The title by which to name the .register-file. + * @param fileDestination The path and title by which to name the .register-file. */ - public static void writeRegister(RecipeRegister recipeRegister, String title) { + public static void writeRegister(RecipeRegister recipeRegister, String fileDestination) { if (recipeRegister == null) { throw new IllegalArgumentException("Only a valid register object can be written to file."); } - try (FileWriter fileWriter = new FileWriter(filePath + title + fileType)) { + try (FileWriter fileWriter = new FileWriter(fileDestination + fileType)) { recipeRegister.getRecipes().forEach((recipe) -> { try { @@ -97,11 +95,15 @@ public class FileHandler { * instead of a recipe register. Each recipe is separated by a '#'-sign and passed on to the method 'readRecipe' * that reads and returns each Recipe object for the register. * - * @param title Title of the .register file at which the recipe register is saved. + * @param fileDestination Title of the .register file at which the recipe register is saved. * @return A recipe register object read from file. */ - public static RecipeRegister readRecipeRegister(String title) { - File file = new File(filePath + title + fileType); + public static RecipeRegister readRecipeRegister(String fileDestination) throws IOException { + System.out.println(fileDestination); + File file = new File(fileDestination + fileType); + + //TODO: Provide 'starter' for recipes + RecipeRegister register = new RecipeRegister(); @@ -128,12 +130,13 @@ public class FileHandler { * the Recipe object based on this. The beginning of each ingredient line is recognized by a hyphen ('-'), while * the instructions are all lines, including internal new-lines, that do not start with a hyphen. * - * @param readRecipe A String representation of one recipe as read from file. + * @param fileDestination A String representation of the path and recipe to read from file. * @return A recipe object based on the provided string representation. */ - public static Recipe readRecipe(String readRecipe) { - Scanner sc = new Scanner(readRecipe); + public static Recipe readRecipe(String fileDestination) { + Scanner sc = new Scanner(fileDestination); + System.out.println(fileDestination); Recipe recipe; String instructions = "None"; String recipeName = sc.nextLine().strip(); @@ -171,13 +174,14 @@ public class FileHandler { * * @param ingredientsAtHand An IngredientsAtHand object that holds a collection of constants of the * FoodItem enum class. - * @param title The title by which to name the file that the ingredients at hand are written to. + * @param fileDestination The path and title by which to name the file that the ingredients at hand are written to. * @throws IOException if an input or output error occurs. */ - public static void writeIngredientsAtHand(IngredientsAtHand ingredientsAtHand, String title) throws IOException { + public static void writeIngredientsAtHand(IngredientsAtHand ingredientsAtHand, String fileDestination) throws IOException { StringBuilder sb = new StringBuilder(); + System.out.println("Write IAH: " + fileDestination + fileType); - try (FileWriter fileWriter = new FileWriter(filePath + title + fileType)) { + try (FileWriter fileWriter = new FileWriter(fileDestination + fileType)) { if (ingredientsAtHand == null) { fileWriter.write(""); }else { @@ -195,13 +199,17 @@ public class FileHandler { * The method reads an IngredientsAtHand object from the .register file with the name provided as a parameter * and stored at /main/recourses/recipes/. If the file is not found, a FileNotFoundException is thrown and * null is returned instead of a IngredientsAtHand object. - * @param title Title of the file to read the IngredientsAtHand object from. + * @param fileDestination Path and title of the file to read the IngredientsAtHand object from. * @return An IngredientsAtHand object based on the provided .register file. */ - public static IngredientsAtHand readIngredientsAtHand(String title) { - File file = new File(filePath + title + fileType); + public static IngredientsAtHand readIngredientsAtHand(String fileDestination) throws IOException { + File file = new File(fileDestination + fileType); IngredientsAtHand ingredientsAtHand = new IngredientsAtHand(); + if (!file.exists()){ + file.createNewFile(); + } + try (Scanner sc = new Scanner(file)) { String line; diff --git a/src/main/resources/budgets/SelectedBudget.current b/src/main/resources/budgets/SelectedBudget.current index 06b279097892ff167d38ffe92f919d36d811db9d..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 100644 --- a/src/main/resources/budgets/SelectedBudget.current +++ b/src/main/resources/budgets/SelectedBudget.current @@ -1 +0,0 @@ -APRILreasonable \ No newline at end of file diff --git a/src/main/resources/recipes/Fridge.register b/src/main/resources/testFiles/recipes/Fridge.register similarity index 100% rename from src/main/resources/recipes/Fridge.register rename to src/main/resources/testFiles/recipes/Fridge.register diff --git a/src/main/resources/recipes/testReadRegisterFromFile.register b/src/main/resources/testFiles/recipes/testReadRegisterFromFile.register similarity index 100% rename from src/main/resources/recipes/testReadRegisterFromFile.register rename to src/main/resources/testFiles/recipes/testReadRegisterFromFile.register diff --git a/src/main/resources/view/IncomeAndExpenses.fxml b/src/main/resources/view/IncomeAndExpenses.fxml index 16909435076c3283bf84235f8faaaad6775eab63..efc5d84906fc71489f9a49e214f66ddb17d6e64c 100644 --- a/src/main/resources/view/IncomeAndExpenses.fxml +++ b/src/main/resources/view/IncomeAndExpenses.fxml @@ -57,53 +57,32 @@ <Insets left="15.0" /> </VBox.margin> </BorderPane> - <BorderPane prefHeight="64.0" prefWidth="1100.0"> - <left> - <HBox alignment="CENTER_LEFT" prefHeight="64.0" prefWidth="261.0" spacing="10.0" BorderPane.alignment="CENTER"> - <children> - <MenuButton mnemonicParsing="false" prefHeight="50.0" prefWidth="140.0" styleClass="button-style" stylesheets="@../style.css" text="Add"> - <items> - <MenuItem fx:id="addIncome" mnemonicParsing="false" onAction="#handleAddBtn" text="Income" /> - <MenuItem fx:id="addExpense" mnemonicParsing="false" onAction="#handleAddBtn" text="Expense" /> - </items> - <graphic> - <ImageView fitHeight="39.0" fitWidth="63.0" pickOnBounds="true" preserveRatio="true"> - <image> - <Image url="@../Images/add.png" /> - </image> - </ImageView> - </graphic> - <font> - <Font name="Lucida Console" size="14.0" /> - </font> - </MenuButton> - </children> - <BorderPane.margin> - <Insets left="30.0" /> - </BorderPane.margin> - </HBox> - </left> - <right> - <Pane BorderPane.alignment="CENTER" /> - </right> - <opaqueInsets> - <Insets /> - </opaqueInsets> - <VBox.margin> - <Insets left="50.0" right="15.0" /> - </VBox.margin> - <center> - <Pane BorderPane.alignment="CENTER"> - <children> - <ComboBox fx:id="filter" disable="true" layoutX="74.0" layoutY="19.0" opacity="0.0" prefWidth="150.0" promptText="Show"> - <opaqueInsets> - <Insets /> - </opaqueInsets> - </ComboBox> - </children> - </Pane> - </center> - </BorderPane> + <HBox alignment="CENTER_LEFT" prefHeight="49.0" prefWidth="1115.0"> + <children> + <MenuButton alignment="CENTER" mnemonicParsing="false" prefHeight="49.0" prefWidth="153.0" styleClass="button-style" stylesheets="@../style.css" text="Add"> + <items> + <MenuItem fx:id="addIncome" mnemonicParsing="false" onAction="#handleAddBtn" text="Income" /> + <MenuItem fx:id="addExpense" mnemonicParsing="false" onAction="#handleAddBtn" text="Expense" /> + </items> + <font> + <Font name="Lucida Console" size="14.0" /> + </font> + <opaqueInsets> + <Insets left="10.0" /> + </opaqueInsets> + <graphic> + <ImageView fitHeight="39.0" fitWidth="63.0" pickOnBounds="true" preserveRatio="true"> + <image> + <Image url="@../Images/add.png" /> + </image> + </ImageView> + </graphic> + <HBox.margin> + <Insets left="70.0" /> + </HBox.margin> + </MenuButton> + </children> + </HBox> <GridPane prefHeight="473.0" prefWidth="1029.0"> <columnConstraints> <ColumnConstraints hgrow="SOMETIMES" maxWidth="563.0" minWidth="10.0" prefWidth="503.0" /> @@ -142,12 +121,12 @@ </VBox> <Pane stylesheets="@../style.css" GridPane.columnIndex="1" GridPane.rowIndex="1"> <children> - <PieChart fx:id="incomePieChart" labelsVisible="false" layoutX="14.0" layoutY="-107.0" legendSide="RIGHT" maxHeight="298.0" maxWidth="522.0" prefHeight="290.0" prefWidth="515.0" styleClass="pie-back" stylesheets="@../style.css" title="Income" /> + <PieChart fx:id="incomePieChart" labelsVisible="false" layoutX="30.0" layoutY="-14.0" legendSide="RIGHT" maxHeight="298.0" maxWidth="522.0" prefHeight="211.0" prefWidth="462.0" styleClass="pie-back" stylesheets="@../style.css" title="Income" /> </children> </Pane> <Pane stylesheets="@../style.css" GridPane.columnIndex="1" GridPane.rowIndex="3"> <children> - <PieChart fx:id="expensePieChart" labelsVisible="false" layoutX="14.0" layoutY="-59.0" legendSide="RIGHT" maxHeight="298.0" maxWidth="530.0" prefHeight="290.0" prefWidth="515.0" stylesheets="@../style.css" title="Expenses" /> + <PieChart fx:id="expensePieChart" labelsVisible="false" layoutX="38.0" layoutY="-49.0" legendSide="RIGHT" maxHeight="298.0" maxWidth="530.0" prefHeight="253.0" prefWidth="462.0" stylesheets="@../style.css" title="Expenses" /> </children> </Pane> <Pane prefHeight="20.0" prefWidth="1046.0"> diff --git a/src/test/java/no/ntnu/idatt1002/demo/data/budget/FileHandlingBudgetArchiveTest.java b/src/test/java/no/ntnu/idatt1002/demo/data/budget/FileHandlingBudgetArchiveTest.java index 9a2a515c034a985fe7f23cf3df415447e0156342..1bb64178064454d2acafb988ac4d1e60a8e45957 100644 --- a/src/test/java/no/ntnu/idatt1002/demo/data/budget/FileHandlingBudgetArchiveTest.java +++ b/src/test/java/no/ntnu/idatt1002/demo/data/budget/FileHandlingBudgetArchiveTest.java @@ -27,7 +27,7 @@ public class FileHandlingBudgetArchiveTest { @BeforeEach void setUp() { budgetRegister = new BudgetRegister(); - filePath = "testFiles/budget/Archive"; + filePath = "src/main/resources/testFiles/budget/Archive"; name1 = "APRIL1"; name2 = "APRIL2"; @@ -73,8 +73,8 @@ public class FileHandlingBudgetArchiveTest { @BeforeEach void setUp() { budgetRegister = new BudgetRegister(); - filePath = "testFiles/budget/emptyFile"; - filePath1 = "testFiles/budget/notEmptyFile"; + filePath = "src/main/resources/testFiles/budget/emptyFile"; + filePath1 = "src/main/resources/testFiles/budget/notEmptyFile"; } @Test diff --git a/src/test/java/no/ntnu/idatt1002/demo/data/budget/FileHandlingBudgetTest.java b/src/test/java/no/ntnu/idatt1002/demo/data/budget/FileHandlingBudgetTest.java index 2e9df5573aa81de4eecb7cb9ffc78fa2cb859575..3050ef490c25ef09997c542ab42d004451a81ff3 100644 --- a/src/test/java/no/ntnu/idatt1002/demo/data/budget/FileHandlingBudgetTest.java +++ b/src/test/java/no/ntnu/idatt1002/demo/data/budget/FileHandlingBudgetTest.java @@ -19,7 +19,7 @@ class FileHandlingBudgetTest { @DisplayName("FileHandling budget with one BudgetItem does not throw exception") class fileHandlingBudgetWithOneBudgetItemDoesNotThrowException{ GeneralBudget generalBudget = new GeneralBudget(1200); - String fileTitle = "testFiles/budget/oneBudgetItemTest"; + String fileTitle = "src/main/resources/testFiles/budget/oneBudgetItemTest"; @BeforeEach void createGeneralBudget(){ @@ -43,7 +43,7 @@ class FileHandlingBudgetTest { @DisplayName("FileHandling budget with multiple BudgetItems does not throw exception") class FileHandlingBudgetWithMultipleBudgetItemsDoesNotThrowException{ GeneralBudget generalBudget = new GeneralBudget(1200); - String fileTitle = "testFiles/budget/multipleBudgetItem"; + String fileTitle = "src/main/resources/testFiles/budget/multipleBudgetItem"; @BeforeEach void createGeneralBudget(){ foodItem = new BudgetItem(500, "description", ExpenseCategory.FOOD); @@ -70,8 +70,8 @@ class FileHandlingBudgetTest { @DisplayName("Test isEmpty") class IsEmpty { - String emptyFileFilePath = "testFiles/budget/emptyFile"; - String nonEmptyFileFilePath = "testFiles/budget/multipleBudgetItem"; + String emptyFileFilePath = "src/main/resources/testFiles/budget/emptyFile"; + String nonEmptyFileFilePath = "src/main/resources/testFiles/budget/multipleBudgetItem"; @Test @DisplayName("Test isEmpty with empty file") @@ -90,9 +90,9 @@ class FileHandlingBudgetTest { @DisplayName("Test isNewBudget") class IsNewBudget { - String newBudgetFilePath = "testFiles/budget/newBudget"; + String newBudgetFilePath = "src/main/resources/testFiles/budget/newBudget"; - String oldBudgetFilePath = "testFiles/budget/oneBudgetItemTest"; + String oldBudgetFilePath = "src/main/resources/testFiles/budget/oneBudgetItemTest"; @Test @@ -111,7 +111,7 @@ class FileHandlingBudgetTest { @Test @DisplayName("Test writing max budget amount to file") void testWriteMaxMountToFile() throws IOException { - String fileDestination = "testFiles/budget/writeNewBudget"; + String fileDestination = "src/main/resources/testFiles/budget/writeNewBudget"; String maxAmount = "100"; FileHandlingBudget.writeMaxAmountToFile(fileDestination, maxAmount); diff --git a/src/test/java/no/ntnu/idatt1002/demo/data/budget/FileHandlingSelectedBudgetTest.java b/src/test/java/no/ntnu/idatt1002/demo/data/budget/FileHandlingSelectedBudgetTest.java index 626954fe81114e321a4eaf9191c46fb6cb852b89..b2706b745e2932f7975fda2decdef0762064a4c4 100644 --- a/src/test/java/no/ntnu/idatt1002/demo/data/budget/FileHandlingSelectedBudgetTest.java +++ b/src/test/java/no/ntnu/idatt1002/demo/data/budget/FileHandlingSelectedBudgetTest.java @@ -22,16 +22,19 @@ public class FileHandlingSelectedBudgetTest { @BeforeEach void setUp() { - filePath = "testFiles/budget/SelectedBudget"; - emptyFilePath = "testFiles/budget/emptyFile"; + filePath = "src/main/resources/testFiles/budget/"; + emptyFilePath = filePath + "emptyFile"; toString = "APRIL1"; budgetName1 = ""; budgetName2 = "APRIL1"; } + @Test @DisplayName("Test read and write to file") void testReadAndWriteToFile() throws IOException { - budgetName1 = FileHandlingSelectedBudget.readSelectedBudget(filePath); + System.out.println(filePath); + budgetName1 = FileHandlingSelectedBudget.readSelectedBudget(filePath + "SelectedBudget"); + assertEquals(toString, budgetName1); @@ -58,9 +61,8 @@ public class FileHandlingSelectedBudgetTest { @Test @DisplayName("Test clearSelectedBudget") void testClearSelectedBudget() throws IOException { - String filePath = "testFiles/budget/fileForClearing"; + String filePath = "src/main/resources/testFiles/budget/fileForClearing"; FileHandlingSelectedBudget.clearSelectedBudget(filePath); - assertNull(FileHandlingSelectedBudget.readSelectedBudget(filePath)); } @@ -72,7 +74,7 @@ public class FileHandlingSelectedBudgetTest { @BeforeEach void setUp() { - filePath = "testFiles/budget/emptyFile"; + filePath = "src/main/resources/testFiles/budget/emptyFile"; } @Test @@ -116,7 +118,7 @@ public class FileHandlingSelectedBudgetTest { @BeforeEach void setUp() { - filePath = "testFiles/budget/testDirectory"; + filePath = "src/main/resources/testFiles/budget/testDirectory"; } @Test @@ -167,7 +169,7 @@ public class FileHandlingSelectedBudgetTest { @BeforeEach void setUp() { - filePath = "testFiles/budget/testDirectory"; + filePath = "src/main/resources/testFiles/budget/testDirectory"; } @Test diff --git a/src/test/java/no/ntnu/idatt1002/demo/data/economics/FileHandlingTest.java b/src/test/java/no/ntnu/idatt1002/demo/data/economics/FileHandlingTest.java index 6b94466c5bfc9ff69cabd0b3747ceeb45ed8ce8d..13f41f5b2a91332828bb99869baf040ae2504b56 100644 --- a/src/test/java/no/ntnu/idatt1002/demo/data/economics/FileHandlingTest.java +++ b/src/test/java/no/ntnu/idatt1002/demo/data/economics/FileHandlingTest.java @@ -18,8 +18,8 @@ class FileHandlingTest { @Test @DisplayName("isEmpty returns true if a file is empty") void isEmptyReturnsTrueIfAFileIsEmpty() throws IOException { - FileHandling.writeItemRegisterToFile(incomeRegister, "/testFiles/economics/incomeRegisterTest"); - assertTrue(FileHandling.isEmpty("/testFiles/economics/incomeRegisterTest")); + FileHandling.writeItemRegisterToFile(incomeRegister, "src/main/resources/testFiles/economics/incomeRegisterTest"); + assertTrue(FileHandling.isEmpty("src/main/resources/testFiles/economics/incomeRegisterTest")); } @Test @@ -27,14 +27,14 @@ class FileHandlingTest { void isEmptyReturnsFalseIfFileIsNotEmpty() throws IOException { Income income1 = new Income("description", 59.9f, false, IncomeCategory.GIFT, LocalDate.of(2023, Month.MARCH, 3)); incomeRegister.addItem(income1); - FileHandling.writeItemRegisterToFile(incomeRegister, "/testFiles/economics/incomeRegisterTest"); - assertFalse(FileHandling.isEmpty("/testFiles/economics/incomeRegisterTest")); + FileHandling.writeItemRegisterToFile(incomeRegister, "src/main/resources/testFiles/economics/incomeRegisterTest"); + assertFalse(FileHandling.isEmpty("src/main/resources/testFiles/economics/incomeRegisterTest")); } } @Nested @DisplayName("FileHandling IncomeRegister to file") class fileHandlingIncomeRegisterToFile{ - String fileTitle = "/testFiles/economics/incomeRegisterTest"; + String fileTitle = "src/main/resources/testFiles/economics/incomeRegisterTest"; @Nested @DisplayName("FileHandling incomeRegister with income that has description") class fileHandlingIncomeRegisterWithIncomeThatHasDescription{ @@ -103,7 +103,7 @@ class FileHandlingTest { @Nested @DisplayName("FileHandling ExpenseRegister to file") class fileHandlingExpenseRegisterToFile{ - String fileTitle = "/testFiles/economics/expenseRegisterTest"; + String fileTitle = "src/main/resources/testFiles/economics/expenseRegisterTest"; @Nested @DisplayName("FileHandling ExpenseRegister with Expense that has description") class fileHandlingExpenseRegisterWithExpenseThatHasDescription{ diff --git a/src/test/java/no/ntnu/idatt1002/demo/data/recipes/FileHandlerTest.java b/src/test/java/no/ntnu/idatt1002/demo/data/recipes/FileHandlerTest.java index 248985c463aca4c94e938eb1cf6ebe620db94922..eaafce33a75bd157cbc49b021d740e6cb46092de 100644 --- a/src/test/java/no/ntnu/idatt1002/demo/data/recipes/FileHandlerTest.java +++ b/src/test/java/no/ntnu/idatt1002/demo/data/recipes/FileHandlerTest.java @@ -4,6 +4,8 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; + +import java.io.IOException; import java.util.ArrayList; import java.util.Objects; @@ -15,6 +17,7 @@ class FileHandlerTest { IngredientsAtHand ingredientsAtHand = new IngredientsAtHand(); Recipe recipe1; ArrayList<RecipeIngredient> ingredientsOfRecipe1 = new ArrayList<>(); + String testPath = "src/main/resources/testFiles/recipes/"; @BeforeEach void beforeEach() { @@ -74,19 +77,19 @@ class FileHandlerTest { @DisplayName("Test that writing empty register to file creates blank file.") void writeEmptyRegister() { RecipeRegister emptyRegister = new RecipeRegister(); - assertAll(() -> FileHandler.writeRegister(emptyRegister, "emptyRegister")); + assertAll(() -> FileHandler.writeRegister(emptyRegister, testPath + "emptyRegister")); } @Test @DisplayName("Test that writing null register to file throws exception.") void writeNullRegister() { - assertThrows(IllegalArgumentException.class, () -> FileHandler.writeRegister(null, "noRegister")); + assertThrows(IllegalArgumentException.class, () -> FileHandler.writeRegister(null, testPath + "noRegister")); } @Test @DisplayName("Write recipe register correctly to file as text.") void writeRecipeRegisterToFile() { - assertAll(() -> FileHandler.writeRegister(recipeRegister, "RecipeRegister")); + assertAll(() -> FileHandler.writeRegister(recipeRegister, testPath + "RecipeRegister")); } } @@ -98,10 +101,11 @@ class FileHandlerTest { @Test @DisplayName("Read recipe register correctly from file to object.") - void readRecipeRegisterFromFile() { - assertAll(() -> FileHandler.readRecipeRegister("testReadRegisterFromFile")); + void readRecipeRegisterFromFile() throws IOException { + System.out.printf(testPath); + assertAll(() -> FileHandler.readRecipeRegister(testPath + "/testReadRegisterFromFile")); - RecipeRegister registerReadFromTestFile = FileHandler.readRecipeRegister("testReadRegisterFromFile"); + RecipeRegister registerReadFromTestFile = FileHandler.readRecipeRegister(testPath + "/testReadRegisterFromFile"); assert registerReadFromTestFile != null; assertEquals(new RecipeIngredient(FoodItem.MINCED_MEAT, 500, MeasuringUnit.GR), @@ -127,25 +131,26 @@ class FileHandlerTest { @Test @DisplayName("Write empty ingredients at hand to file.") - void writeEmptyIngredientsAtHandToFile() { + void writeEmptyIngredientsAtHandToFile() throws IOException { IngredientsAtHand emptyAtHand = new IngredientsAtHand(); - assertAll(() -> FileHandler.writeIngredientsAtHand(emptyAtHand, "EmptyAtHandRegister")); - assertEquals(0, Objects.requireNonNull(FileHandler.readIngredientsAtHand("EmptyAtHandRegister")).getIngredientsAtHand().size()); + assertAll(() -> FileHandler.writeIngredientsAtHand(emptyAtHand, testPath + "EmptyAtHandRegister")); + assertEquals(0, Objects.requireNonNull(FileHandler.readIngredientsAtHand(testPath + "EmptyAtHandRegister")).getIngredientsAtHand().size()); } @Test @DisplayName("Write ingredients at hand to file.") void writeIngredientsAtHandToFile() { - assertAll(() -> FileHandler.writeIngredientsAtHand(ingredientsAtHand, "AtHandRegister")); + assertAll(() -> FileHandler.writeIngredientsAtHand(ingredientsAtHand, testPath + "AtHandRegister")); } @Test @DisplayName("Read ingredients at hand from file.") - void readIngredientsAtHandFromFile() { - assertAll(() -> FileHandler.writeIngredientsAtHand(ingredientsAtHand, "AtHandRegister")); + void readIngredientsAtHandFromFile() throws IOException { + System.out.println(testPath + "AtHandRegister"); + assertAll(() -> FileHandler.writeIngredientsAtHand(ingredientsAtHand, testPath + "AtHandRegister")); assertEquals(ingredientsAtHand.getIngredientsAtHand().size(), - Objects.requireNonNull(FileHandler.readIngredientsAtHand("AtHandRegister")).getIngredientsAtHand().size()); + Objects.requireNonNull(FileHandler.readIngredientsAtHand( testPath + "AtHandRegister")).getIngredientsAtHand().size()); } } } \ No newline at end of file