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 0dbfbbb6bb4d7cc808c71837df88c1a2735f875f..45f7fdeac74a19b620c7ef5cc98788dd9c085d11 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/controller/BudgetController.java +++ b/src/main/java/no/ntnu/idatt1002/demo/controller/BudgetController.java @@ -8,6 +8,8 @@ import javafx.fxml.FXMLLoader; import javafx.scene.Node; import javafx.scene.Parent; import javafx.scene.Scene; +import javafx.scene.chart.PieChart; +import javafx.scene.chart.PieChart.Data; import javafx.scene.control.*; import javafx.scene.control.Alert.AlertType; import javafx.scene.control.cell.PropertyValueFactory; @@ -22,6 +24,7 @@ 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 @@ -30,39 +33,29 @@ import java.util.Optional; * @author Anders Emil Bergan * @since 24.3.2023 */ -public class BudgetController { +public class BudgetController implements FinanceController { private GeneralBudget general; @FXML - private Button addBudget; + private Button addBtn; @FXML - private Button editBudget; - - @FXML - private Button expenseBtn; - - @FXML - private Button incomeBtn; + private Button editBtn; @FXML private Button returnBtn; - - @FXML - private Button nextBtn; - @FXML - private TableColumn<BudgetItem, Double> amountColumn; + private TableColumn<BudgetItem, Double> amountCol; @FXML private TableView<BudgetItem> budgetTableView = new TableView<>(); @FXML - private TableColumn<BudgetItem, ExpenseCategory> categoryColumn; + private TableColumn<BudgetItem, ExpenseCategory> categoryCol; @FXML - private TableColumn<BudgetItem, String> descriptionColumn; + private TableColumn<BudgetItem, String> descriptionCol; @FXML private Text sum; @@ -78,33 +71,45 @@ public class BudgetController { * 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 { + //TODO if budget is not empty - disable //Initialize table columns - categoryColumn.setCellValueFactory(new PropertyValueFactory<BudgetItem, ExpenseCategory>("budgetCategory")); - amountColumn.setCellValueFactory(new PropertyValueFactory<BudgetItem, Double>("budgetAmount")); - descriptionColumn.setCellValueFactory(new PropertyValueFactory<BudgetItem, String>("budgetDescription")); + categoryCol.setCellValueFactory(new PropertyValueFactory<BudgetItem, ExpenseCategory>("budgetCategory")); + 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); + //createBudgetPieChart(); //Initialize sum field under the tableview - sum.setText(String.valueOf(general.totalSum())); + //sum.setText(String.valueOf(general.totalSum())); } - @FXML - protected void handleAddButton(ActionEvent event) { - handleEditButton(event); + private ObservableList<PieChart.Data> createBudgetPieChart() { //TODO DOESNT WORK IF BUDGETITEM HAS NO BUDGET + 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()) + ); + } + + @Override + public void handleAddBtn(ActionEvent event) { + handleEditBtn(event); } /** * Adds or edits a budget item, depending on what mode the DialogMode enum is at. The method brings up a dialog box popup in which the user can fill and choose * values that the budget item will have. Open exiting the popup, the changes the saved to the tableview. * @param event A button click on either the add or delete button. */ - @FXML - public void handleEditButton(ActionEvent event) { + @Override + public void handleEditBtn(ActionEvent event) { BudgetItem item = null; String dialogTitle = ""; DialogMode dialogMode; @@ -124,11 +129,11 @@ public class BudgetController { AddBudgetController budgetController = loader.getController(); //Sets the title of the dialog box - if(event.getSource().equals(addBudget)){ + if(event.getSource().equals(addBtn)){ dialogMode = DialogMode.ADD; dialogTitle = "New Budget"; } - else if (event.getSource().equals(editBudget) && budgetTableView.getSelectionModel().getSelectedItem() != null) { + else if (event.getSource().equals(editBtn) && budgetTableView.getSelectionModel().getSelectedItem() != null) { dialogMode = DialogMode.EDIT; dialogTitle = "Edit expense"; //Gets the selected item from the table @@ -181,7 +186,7 @@ public class BudgetController { * Method for synching the register with the tableview. The observable list to which the tableview is set, is being refilled with all the entries * in the register, keeping it updated with new changes. */ - protected void refreshTableView(){ + public void refreshTableView(){ this.budgetList.setAll(general.getBudgetItems()); //Refreshing the sum of the amounts of the budget this.sum.setText(String.valueOf(general.totalSum())); @@ -191,7 +196,8 @@ public class BudgetController { * Returns an optional, which is a popup alert box, asking for confirmation for deleting an entry. * @return An alert box, asking for confirmation for deleting the selected entry of the tableview. */ - private Optional<ButtonType> showConfirmationDialog() { + @Override + public Optional<ButtonType> showConfirmationDialog() { Alert alert = new Alert(Alert.AlertType.CONFIRMATION); alert.setTitle("Confirm Delete"); alert.setHeaderText("Delete Confirmation"); @@ -200,6 +206,17 @@ public class BudgetController { return alert.showAndWait(); } + /** + * Saves the changes made to the tableview by writing the information to a file. + * + * @throws IOException If an error occurs while writing to the file. + */ + @Override + public void saveDataToFile() throws IOException { + FileHandlingBudget fileHandlingBudget = new FileHandlingBudget(); + fileHandlingBudget.writeGeneralBudgetToFile("Budget", general); + } + /** * Returns an optional, which is a popup alert box, informing that either the budget amount has * been exceeded or that the same category has been entered twice in the budget tableview. @@ -233,16 +250,6 @@ public class BudgetController { return general; } - /** - * Saves the changes made to the budget tableview by writing the information to a file. - * @param fileName The name of the file that is written to. - * @throws IOException If an error occurs while writing to the file. - */ - public void saveDataToFile(String fileName) throws IOException { - FileHandlingBudget fileHandlingBudget = new FileHandlingBudget(); - fileHandlingBudget.writeGeneralBudgetToFile(fileName, general); - } - /** * Switches scenes from the budget scene to another, by loading a new FXML file and setting the scene to this location. * The destination depends entirely on which button is pressed. @@ -250,19 +257,12 @@ public class BudgetController { * @throws IOException If an error occurs with loading any of the FXML files. */ @FXML - public void switchScene(ActionEvent event) throws IOException { + public void returnToMainMenu(ActionEvent event) throws IOException { //Always saving the data when switching scenes - saveDataToFile("Budget"); + saveDataToFile(); FXMLLoader loader = new FXMLLoader(); - if (event.getSource() == expenseBtn) { - loader.setLocation(SceneController.class.getResource("/view/Expenses.fxml")); - } else if (event.getSource() == returnBtn) { - loader.setLocation(SceneController.class.getResource("/view/FirstMenu.fxml")); - } else if (event.getSource() == incomeBtn) { - loader.setLocation(SceneController.class.getResource("/view/Income.fxml")); - } else if (event.getSource() == nextBtn) { - loader.setLocation(SceneController.class.getResource("/view/MainMenuNew.fxml")); - } + loader.setLocation(getClass().getResource("/view/MainMenuNew.fxml")); + Parent root = loader.load(); Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow(); Scene scene = new Scene(root);