Skip to content
Snippets Groups Projects
Commit d49e796f authored by Harry Linrui XU's avatar Harry Linrui XU
Browse files

Implemented pie charts display in BudgetController

parent 6a78fe6a
No related branches found
No related tags found
3 merge requests!43Merging frontend-testing into master,!38"Made progressbar dynamic in accordance to spending. Added balance field....,!37Made the sub progress bars respond to changes in expense
Pipeline #214835 passed
package no.ntnu.idatt1002.demo.controller; package no.ntnu.idatt1002.demo.controller;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import javafx.collections.FXCollections; import javafx.collections.FXCollections;
import javafx.collections.ObservableList; import javafx.collections.ObservableList;
import javafx.event.ActionEvent; import javafx.event.ActionEvent;
...@@ -20,12 +23,11 @@ import javafx.stage.Stage; ...@@ -20,12 +23,11 @@ import javafx.stage.Stage;
import no.ntnu.idatt1002.demo.data.Budget.BudgetItem; import no.ntnu.idatt1002.demo.data.Budget.BudgetItem;
import no.ntnu.idatt1002.demo.data.Budget.FileHandlingBudget; import no.ntnu.idatt1002.demo.data.Budget.FileHandlingBudget;
import no.ntnu.idatt1002.demo.data.Budget.GeneralBudget; 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 no.ntnu.idatt1002.demo.data.Economics.ExpenseCategory;
import java.io.IOException; import java.io.IOException;
import java.util.Optional; 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 * 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 { ...@@ -70,14 +72,16 @@ public class BudgetController implements FinanceController {
@FXML @FXML
private ObservableList<BudgetItem> budgetList; 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. * 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 @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 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 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 //TODO make budget item throw exception with negative amount
...@@ -88,29 +92,39 @@ public class BudgetController implements FinanceController { ...@@ -88,29 +92,39 @@ public class BudgetController implements FinanceController {
amountCol.setCellValueFactory(new PropertyValueFactory<BudgetItem, Double>("budgetAmount")); amountCol.setCellValueFactory(new PropertyValueFactory<BudgetItem, Double>("budgetAmount"));
descriptionCol.setCellValueFactory(new PropertyValueFactory<BudgetItem, String>("budgetDescription")); descriptionCol.setCellValueFactory(new PropertyValueFactory<BudgetItem, String>("budgetDescription"));
//Initialize registers and tableview try { //Initialize registers, tableview and pie charts
general = loadBudgetDataFromFile("Budget"); general = loadBudgetDataFromFile("Budget");
budgetList = FXCollections.observableArrayList(general.getBudgetItems()); budgetList = FXCollections.observableArrayList(general.getBudgetItems());
budgetTableView.setItems(budgetList); budgetTableView.setItems(budgetList);
refreshPieChart();
} catch(IOException ioe) {
sum.setText("Invalid budget file format");
} catch(IllegalArgumentException iae) {
sum.setText("Cannot load pie charts");
}
formatDatePicker(); formatDatePicker();
//createBudgetPieChart();
//Initialize sum field under the tableview //Initialize sum field under the tableview
//sum.setText(String.valueOf(general.totalSum())); //sum.setText(String.valueOf(general.totalSum()));
} }
private ObservableList<PieChart.Data> createBudgetPieChart() throws IllegalArgumentException { //TODO DOESNT WORK IF BUDGETITEM HAS NO BUDGET private List<ExpenseCategory> getChosenBudgetCategories() { //todo could be moved to generalbudget
try { return Arrays.stream(ExpenseCategory.values()).toList().
return FXCollections.observableArrayList( stream().filter(expenseCategory -> general.hasBudgetCategory(expenseCategory)).toList();
new Data("Food", general.getBudgetItem(ExpenseCategory.FOOD).getBudgetAmount()), }
new Data("Books", general.getBudgetItem(ExpenseCategory.BOOKS).getBudgetAmount()), private ObservableList<PieChart.Data> createBudgetPieChart() throws IllegalArgumentException { //
new Data("Clothes", ObservableList<PieChart.Data> budgetData = FXCollections.observableArrayList();
general.getBudgetItem(ExpenseCategory.CLOTHES).getBudgetAmount()), List<ExpenseCategory> chosenCategories = getChosenBudgetCategories();
new Data("Other", general.getBudgetItem(ExpenseCategory.OTHER).getBudgetAmount()) for (ExpenseCategory category : chosenCategories) {
); budgetData.add(new Data(category.toString().substring(0, 1).toUpperCase().
} catch(IllegalArgumentException iae) { concat(category.toString().substring(1)),
return FXCollections.observableArrayList(); general.getBudgetItem(category).getBudgetAmount()));
} }
return budgetData;
}
private void refreshPieChart() {
this.budgetPieChart.setData(createBudgetPieChart());
} }
/** /**
...@@ -176,7 +190,7 @@ public class BudgetController implements FinanceController { ...@@ -176,7 +190,7 @@ public class BudgetController implements FinanceController {
item = budgetController.getNewBudgetItem(); item = budgetController.getNewBudgetItem();
if(item != null && dialogMode == DialogMode.ADD){ if(item != null && dialogMode == DialogMode.ADD){
try { try {
general.addToBudgetBudgetItem(item); general.addToBudgetBudgetItem(item);
} catch(IllegalArgumentException e) { } catch(IllegalArgumentException e) {
showIllegalBudgetItemDialog(); showIllegalBudgetItemDialog();
} }
...@@ -280,9 +294,9 @@ public class BudgetController implements FinanceController { ...@@ -280,9 +294,9 @@ public class BudgetController implements FinanceController {
* @param event A button click on the return to main menu button * @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. * @throws IOException If an error occurs with loading any of the FXML files.
*/ */
@FXML @FXML
public void returnToMainMenu(ActionEvent event) throws IOException { public void returnToMainMenu(ActionEvent event) throws IOException {
//Always saving the data when switching scenes //Always saving the data when switching scenes
saveDataToFile(); saveDataToFile();
FXMLLoader loader = new FXMLLoader(); FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/view/MainMenuNew.fxml")); loader.setLocation(getClass().getResource("/view/MainMenuNew.fxml"));
......
date=2023-03-24 date=2023-03-24
description=studie description=studie
amount=1000.0 amount=900.0
isRecurring=Recurring isRecurring=Recurring
category=STUDENT_LOAN category=STUDENT_LOAN
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment