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 1325d2968e40ab23d687559dc589f1b6a60d8a79..5bf55849770965eaf0230fc4808b55c5ff16ed45 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/controller/BudgetController.java +++ b/src/main/java/no/ntnu/idatt1002/demo/controller/BudgetController.java @@ -1,6 +1,9 @@ package no.ntnu.idatt1002.demo.controller; import java.time.LocalDate; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.event.ActionEvent; @@ -20,12 +23,11 @@ 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; - import java.io.IOException; import java.util.Optional; -import no.ntnu.idatt1002.demo.data.Economics.IncomeCategory; /** * Controller for budget scene in the application. This controller manages all actions that relates to the budget tableview (add, edit and delete), the switching @@ -70,14 +72,16 @@ public class BudgetController implements FinanceController { @FXML private ObservableList<BudgetItem> budgetList; + @FXML + private PieChart budgetPieChart; + /** * Initializes the budget register, the observable budget list and the tableview, along with the values of the dropbox used for filtering the tableview. - * @throws IOException If there occurs any exception when loading the budget register from a file. */ @FXML - public void initialize() throws IOException { + public void initialize() { //TODO if budget is not empty (HAS VALUES) -> make uneditable -> EVENT FILTER TO CONTEXT MENU //TODO disable return to main menu when creating budget because this is the same view as when you create budeget //TODO make budget item throw exception with negative amount @@ -88,29 +92,39 @@ public class BudgetController implements FinanceController { amountCol.setCellValueFactory(new PropertyValueFactory<BudgetItem, Double>("budgetAmount")); descriptionCol.setCellValueFactory(new PropertyValueFactory<BudgetItem, String>("budgetDescription")); - //Initialize registers and tableview - general = loadBudgetDataFromFile("Budget"); - budgetList = FXCollections.observableArrayList(general.getBudgetItems()); - budgetTableView.setItems(budgetList); + try { //Initialize registers, tableview and pie charts + general = loadBudgetDataFromFile("Budget"); + budgetList = FXCollections.observableArrayList(general.getBudgetItems()); + budgetTableView.setItems(budgetList); + refreshPieChart(); + } catch(IOException ioe) { + sum.setText("Invalid budget file format"); + } catch(IllegalArgumentException iae) { + sum.setText("Cannot load pie charts"); + } formatDatePicker(); - //createBudgetPieChart(); //Initialize sum field under the tableview //sum.setText(String.valueOf(general.totalSum())); } - private ObservableList<PieChart.Data> createBudgetPieChart() throws IllegalArgumentException { //TODO DOESNT WORK IF BUDGETITEM HAS NO BUDGET - try { - return FXCollections.observableArrayList( - new Data("Food", general.getBudgetItem(ExpenseCategory.FOOD).getBudgetAmount()), - new Data("Books", general.getBudgetItem(ExpenseCategory.BOOKS).getBudgetAmount()), - new Data("Clothes", - general.getBudgetItem(ExpenseCategory.CLOTHES).getBudgetAmount()), - new Data("Other", general.getBudgetItem(ExpenseCategory.OTHER).getBudgetAmount()) - ); - } catch(IllegalArgumentException iae) { - return FXCollections.observableArrayList(); + private List<ExpenseCategory> getChosenBudgetCategories() { //todo could be moved to generalbudget + return Arrays.stream(ExpenseCategory.values()).toList(). + stream().filter(expenseCategory -> general.hasBudgetCategory(expenseCategory)).toList(); + } + private ObservableList<PieChart.Data> createBudgetPieChart() throws IllegalArgumentException { // + ObservableList<PieChart.Data> budgetData = FXCollections.observableArrayList(); + List<ExpenseCategory> chosenCategories = getChosenBudgetCategories(); + for (ExpenseCategory category : chosenCategories) { + budgetData.add(new Data(category.toString().substring(0, 1).toUpperCase(). + concat(category.toString().substring(1)), + general.getBudgetItem(category).getBudgetAmount())); } + return budgetData; + } + + private void refreshPieChart() { + this.budgetPieChart.setData(createBudgetPieChart()); } /** @@ -176,7 +190,7 @@ public class BudgetController implements FinanceController { item = budgetController.getNewBudgetItem(); if(item != null && dialogMode == DialogMode.ADD){ try { - general.addToBudgetBudgetItem(item); + general.addToBudgetBudgetItem(item); } catch(IllegalArgumentException e) { showIllegalBudgetItemDialog(); } @@ -280,9 +294,9 @@ public class BudgetController implements FinanceController { * @param event A button click on the return to main menu button * @throws IOException If an error occurs with loading any of the FXML files. */ - @FXML + @FXML public void returnToMainMenu(ActionEvent event) throws IOException { - //Always saving the data when switching scenes + //Always saving the data when switching scenes saveDataToFile(); FXMLLoader loader = new FXMLLoader(); loader.setLocation(getClass().getResource("/view/MainMenuNew.fxml")); diff --git a/src/main/resources/Economics/Income.register b/src/main/resources/Economics/Income.register index b058ccdeba1d571bb6c65e5043018f3e2f538305..51f3e3d3818ae28ec3300f8405b47cf4b908714e 100644 --- a/src/main/resources/Economics/Income.register +++ b/src/main/resources/Economics/Income.register @@ -1,6 +1,6 @@ date=2023-03-24 description=studie -amount=1000.0 +amount=900.0 isRecurring=Recurring category=STUDENT_LOAN