From 3c3c384b5b6e91f683fd5192b6da91f98e46006e Mon Sep 17 00:00:00 2001 From: Harry Linrui XU <xulr0820@hotmail.com> Date: Fri, 24 Mar 2023 18:37:34 +0100 Subject: [PATCH] "Added methods that load and save data in BudgetController" --- .../demo/controller/BudgetController.java | 34 ++++++++++++++++--- .../demo/controller/IncomeController.java | 13 ++++--- 2 files changed, 35 insertions(+), 12 deletions(-) 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 6bb20e42..97da74c3 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/controller/BudgetController.java +++ b/src/main/java/no/ntnu/idatt1002/demo/controller/BudgetController.java @@ -14,6 +14,7 @@ import javafx.scene.control.cell.PropertyValueFactory; import javafx.stage.Modality; import javafx.stage.Stage; import no.ntnu.idatt1002.demo.data.Budget.BudgetItem; +import no.ntnu.idatt1002.demo.data.Budget.FileHandlingBudget; import no.ntnu.idatt1002.demo.data.Budget.GeneralBudget; import no.ntnu.idatt1002.demo.data.Economics.Expense; import no.ntnu.idatt1002.demo.data.Economics.ExpenseCategory; @@ -23,11 +24,13 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Optional; +import no.ntnu.idatt1002.demo.data.Economics.FileHandling; +import no.ntnu.idatt1002.demo.data.Economics.IncomeRegister; public class BudgetController { private DialogMode dialogMode; - private GeneralBudget general = new GeneralBudget(31, 2000); + private GeneralBudget general; @FXML private Button addBudget; @@ -77,12 +80,13 @@ public class BudgetController { public void initialize() throws IOException { - budgetList = FXCollections.observableArrayList(general.getBudgetItems()); - budgetTableView.setItems(budgetList); - categoryColumn.setCellValueFactory(new PropertyValueFactory<BudgetItem, ExpenseCategory>("budgetCategory")); amountColumn.setCellValueFactory(new PropertyValueFactory<BudgetItem, Double>("budgetAmount")); descriptionColumn.setCellValueFactory(new PropertyValueFactory<BudgetItem, String>("budgetDescription")); + + general = loadIncomeDataFromFile("Budget"); + budgetList = FXCollections.observableArrayList(general.getBudgetItems()); + budgetTableView.setItems(budgetList); } @FXML public void switchAddBudget(javafx.event.ActionEvent event) throws IOException { @@ -167,6 +171,26 @@ public class BudgetController { } + public GeneralBudget loadIncomeDataFromFile(String fileName) throws IOException { + FileHandlingBudget fileHandlingBudget = new FileHandlingBudget(); + if (fileHandlingBudget.isEmpty(fileName)) { + general = new GeneralBudget(31, 1000); + } else { + try { + general = fileHandlingBudget.readGeneralBudgetFromFile(fileName); + } catch (IOException e) { + e.printStackTrace(); + } + } + return general; + } + + public void saveDataToFile(String fileName) throws IOException { + FileHandlingBudget fileHandlingBudget = new FileHandlingBudget(); + fileHandlingBudget.writeGeneralBudgetToFile(fileName, general); + } + + protected void refreshObservableList(){ budgetTableView.setItems(budgetList); this.budgetList.setAll(general.getBudgetItems()); @@ -174,7 +198,7 @@ public class BudgetController { @FXML public void switchScene(ActionEvent event) throws IOException { - //saveDataToFile("Income"); + //saveDataToFile("Budget"); FXMLLoader loader = new FXMLLoader(); if (event.getSource() == expenseBtn) { loader.setLocation(SceneController.class.getResource("/view/Expenses.fxml")); diff --git a/src/main/java/no/ntnu/idatt1002/demo/controller/IncomeController.java b/src/main/java/no/ntnu/idatt1002/demo/controller/IncomeController.java index 1fcb3f6c..457da80f 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/controller/IncomeController.java +++ b/src/main/java/no/ntnu/idatt1002/demo/controller/IncomeController.java @@ -82,23 +82,22 @@ public class IncomeController { ObservableList<String> filter; @FXML - public void initialize() - throws IOException { //TODO SAME REGISTER FOR BOTH, BUT LOAD DIFFERENT DATA DEPENDING ON WHICH IT IS + public void initialize() throws IOException { //TODO SAME REGISTER FOR BOTH, BUT LOAD DIFFERENT DATA DEPENDING ON WHICH IT IS dateColumn.setCellValueFactory(new PropertyValueFactory<Income, String>("date")); amountColumn.setCellValueFactory(new PropertyValueFactory<Income, Double>("amount")); - categoryColumn.setCellValueFactory( - new PropertyValueFactory<Income, IncomeCategory>("category")); + categoryColumn.setCellValueFactory(new PropertyValueFactory<Income, IncomeCategory>("category")); descriptionColumn.setCellValueFactory(new PropertyValueFactory<Income, String>("description")); recurringColumn.setCellValueFactory(new PropertyValueFactory<Income, Boolean>("recurring")); - filter = FXCollections.observableArrayList("All", "Gift", "Salary", "Student loan", - "Fixed income"); + filter = FXCollections.observableArrayList("All", "Gift", "Salary", "Student loan", "Fixed income"); incomeRegister = loadIncomeDataFromFile("Income"); income = FXCollections.observableArrayList(incomeRegister.getItems()); incomeTableView.setItems(income); show.setItems(filter); show.setValue("All"); + + //if budget.register isEmpty -> disable expense } @FXML @@ -179,10 +178,10 @@ public class IncomeController { } public IncomeRegister loadIncomeDataFromFile(String fileName) throws IOException { - //ItemRegister<T extends Item> FileHandling fileHandling = new FileHandling(); if (fileHandling.isEmpty(fileName)) { incomeRegister = new IncomeRegister(); + System.out.println("hey"); } else { try { incomeRegister = fileHandling.readIncomeRegisterFromFile(fileName); -- GitLab