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 0c67d8e16ed8d7959a0106d2d28dc13e50983119..043cebb3d73ac9b60091ddf2fe97967798a5c809 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/controller/FinanceController.java +++ b/src/main/java/no/ntnu/idatt1002/demo/controller/FinanceController.java @@ -2,8 +2,19 @@ package no.ntnu.idatt1002.demo.controller; import java.awt.event.ActionEvent; import java.io.IOException; +import java.time.LocalDate; import java.util.Optional; +import javafx.collections.ObservableList; +import javafx.scene.chart.PieChart; +import javafx.scene.chart.PieChart.Data; +import javafx.scene.control.Alert; +import javafx.scene.control.Alert.AlertType; import javafx.scene.control.ButtonType; +import no.ntnu.idatt1002.demo.data.Budget.FileHandlingBudget; +import no.ntnu.idatt1002.demo.data.Budget.GeneralBudget; +import no.ntnu.idatt1002.demo.data.Economics.ExpenseRegister; +import no.ntnu.idatt1002.demo.data.Economics.FileHandling; +import no.ntnu.idatt1002.demo.data.Economics.IncomeRegister; /** * Interface for controllers for scenes with finance tableviews. @@ -11,48 +22,141 @@ import javafx.scene.control.ButtonType; * @author Harry Linrui Xu * @since 29.3.2023 */ -public interface FinanceController { +public abstract class FinanceController { + private ExpenseRegister expenseRegister; + + private IncomeRegister incomeRegister; + + private GeneralBudget general; /** * Method for handling the adding of new entries in the tableview. * @param event A button click on the add button. */ - void handleAddBtn(javafx.event.ActionEvent event); + abstract void handleAddBtn(javafx.event.ActionEvent event); /** * Method for handling the editing of a chosen entry in the tableview. * @param event A button click on the edit button. */ - void handleEditBtn(javafx.event.ActionEvent event); + abstract void handleEditBtn(javafx.event.ActionEvent event); /** * Deletes an entry from the tableview, if an entry has been selected. The method brings up a popup window, asking for confirmation for deleting the entry. * @param event A button click on the delete button */ - void handleDeleteBtn(javafx.event.ActionEvent event); + abstract void handleDeleteBtn(javafx.event.ActionEvent event); /** * 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. */ - void refreshTableView(); + abstract void refreshTableView(); /** * 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. */ - Optional<ButtonType> showConfirmationDialog(String title, String header, String content); + public Optional<ButtonType> showConfirmationDialog(String title, String header, String content) { + Alert alert = new Alert(Alert.AlertType.CONFIRMATION); + alert.setTitle(title); + alert.setHeaderText(header); + alert.setContentText(content); + + return alert.showAndWait(); + } + + abstract void refreshPieChart(); + + /** + * Method for disabling the date picker, yet having its opacity at max. + */ + abstract void formatDatePicker(); + + /** + * Displays an alert box of type error, informing of a custom error. + */ + public void showErrorDialogBox(String title, String header, String content) { + Alert alert = new Alert(AlertType.ERROR); + alert.setTitle(title); + alert.setHeaderText(header); + alert.setContentText(content); + alert.showAndWait(); + } + + /** + * 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. + * @return An object of type GeneralBudget. + * @throws IOException If an error occurs while reading from the file. + */ + public GeneralBudget loadBudgetDataFromFile(String fileName) throws IOException { + //Instantiate new budget + if (FileHandlingBudget.isEmpty(fileName)) { + general = new GeneralBudget(1000); + //throws new IOException("Not valid budget") + } else { //Load previous budget + try { + general = FileHandlingBudget.readGeneralBudgetFromFile(fileName); + } catch (IOException e) { + showErrorDialogBox("File error", "Error in reading from fil", "An error occurred" + + "when reading GeneralBudget from the file"); + } + } + return general; + } + + /** + * 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. + * @return An object of type IncomeRegister. + */ + public IncomeRegister loadIncomeDataFromFile(String fileName) { + //Instantiate new incomeRegister + try { + if (FileHandling.isEmpty(fileName)) { + incomeRegister = new IncomeRegister(); + } else { //Load previous income register + incomeRegister = FileHandling.readIncomeRegisterFromFile(fileName); + } + } catch (IOException ioe) { + showErrorDialogBox("File reading error", "Error in reading from file", "Could not" + + "read the IncomeRegister from file"); + } + return incomeRegister; + } + + /** + * 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. + * @return An object of type IncomeRegister. + */ + public ExpenseRegister loadExpenseDataFromFile(String fileName) { + //Instantiate expense register + try { + if (FileHandling.isEmpty(fileName)) { + expenseRegister = new ExpenseRegister(); + } else { //Load previous income register + expenseRegister = FileHandling.readExpenseRegisterFromFile(fileName); + } + } catch (IOException ioe) { + showErrorDialogBox("File reading error", "Error in reading from file", "Could not" + + "read the ExpenseRegister from file"); + } + return expenseRegister; + } + /** * 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. */ - void saveDataToFile() throws IOException; + abstract void saveDataToFile() throws IOException; /** * Switches scenes, by loading a new FXML file and setting the scene to this location. * @param event A button click on the return to main menu button */ - void switchScene(javafx.event.ActionEvent event); + abstract void switchScene(javafx.event.ActionEvent event); }