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

Added javadoc to BudgetController

parent fd6bc368
No related branches found
No related tags found
8 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,!32Added input validation to add dialog boxes.,!30Redesigned scenes,!29Redesigned scenes,!28Redesigned scenes,!26Redesigned Main menu and expense/income windows
......@@ -23,6 +23,13 @@ import no.ntnu.idatt1002.demo.data.Economics.ExpenseCategory;
import java.io.IOException;
import java.util.Optional;
/**
* Controller for budget scene in the application. This controller manages all actions that relates to the budget tableview (add, edit and delete), the switching
* of scenes from the budget scene to another scene, and the saving of the table, whenever the user switches to another scene.
*
* @author Anders Emil Bergan
* @since 24.3.2023
*/
public class BudgetController {
private GeneralBudget general;
......@@ -67,35 +74,53 @@ public class BudgetController {
private ObservableList<BudgetItem> budgetList;
/**
* Initializes the budget register, the observable budget list and the tableview, along with all dynamic fields that relates to the tableview.
* The method is called each time the FXML of this scene is loaded.
* @throws IOException If there occurs any exception when loading the budget register from a file.
*/
@FXML
public void initialize() throws IOException {
//Initialize table columns
categoryColumn.setCellValueFactory(new PropertyValueFactory<BudgetItem, ExpenseCategory>("budgetCategory"));
amountColumn.setCellValueFactory(new PropertyValueFactory<BudgetItem, Double>("budgetAmount"));
descriptionColumn.setCellValueFactory(new PropertyValueFactory<BudgetItem, String>("budgetDescription"));
//Initialize registers and tableview
general = loadIncomeDataFromFile("Budget");
budgetList = FXCollections.observableArrayList(general.getBudgetItems());
budgetTableView.setItems(budgetList);
//Initialize sum field under the tableview
sum.setText(String.valueOf(general.totalSum()));
}
/**
* 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 switchAddBudget(javafx.event.ActionEvent event) throws IOException {
public void switchAddBudget(ActionEvent event) {
BudgetItem item = null;
String dialogTitle = "";
DialogMode dialogMode;
FXMLLoader loader = new FXMLLoader(SceneController.class.getResource("/view/AddBudget.fxml"));
Dialog<BudgetItem> dialog = new Dialog<>();
dialog.initModality(Modality.APPLICATION_MODAL);
//Try to load the FXML file onto another dialogPane
try{
dialog.getDialogPane().setContent(loader.load());
} catch(IOException e) {
e.printStackTrace();
}
//Loads the controller for the dialog box that appears whenever one adds or edits a budget item
AddBudgetController budgetController = loader.getController();
DialogMode dialogMode;
//Sets the title of the dialog box
if(event.getSource().equals(addBudget)){
dialogMode = DialogMode.ADD;
dialogTitle = "New Budget";
......@@ -104,7 +129,9 @@ public class BudgetController {
else if (event.getSource().equals(editBudget) && budgetTableView.getSelectionModel().getSelectedItem() != null) {
dialogMode = DialogMode.EDIT;
dialogTitle = "Edit expense";
//Gets the selected item from the table
item = budgetTableView.getSelectionModel().getSelectedItem();
//Binds the selected item to another item which is defined in the budgetcontroller
budgetController.setBudget(item);
} else {
return;
......@@ -113,6 +140,7 @@ public class BudgetController {
dialog.setTitle(dialogTitle);
dialog.showAndWait();
//Adds the new item to the register
item = budgetController.getNewBudgetItem();
if(item != null && dialogMode == DialogMode.ADD){
try {
......@@ -121,22 +149,34 @@ public class BudgetController {
showIllegalBudgetItemDialog();
}
}
//Updates the tableview using the register
refreshTableView();
}
/**
* Closes the popup window.
* @param event A button click on the close button
*/
@FXML
public void closeButton(javafx.event.ActionEvent actionEvent) {
final Node source = (Node) actionEvent.getSource();
public void closeButton(ActionEvent event) {
final Node source = (Node) event.getSource();
final Stage stage = (Stage) source.getScene().getWindow();
stage.close();
}
/**
* 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
*/
@FXML
public void deleteButton(ActionEvent event) {
//Gets the selected item from the tableview
BudgetItem item = budgetTableView.getSelectionModel().getSelectedItem();
//Exits the method if nothing is selected
if (item == null) {
return;
}
//Brings up a confirmation popup
Optional<ButtonType> isConfirmed = showConfirmationDialog();
if (isConfirmed.isPresent() && isConfirmed.get() == ButtonType.OK) {
general.deleteItemFromBudget(item.getBudgetCategory());
......@@ -144,7 +184,11 @@ public class BudgetController {
}
}
private Optional<ButtonType> showConfirmationDialog() {
/**
* Returns an optional, which is a popup alert box, asking for confirmation for deleting an entry.
* @return An alertbox, asking for confirmation for deleting the selected entry of the tableview.
*/
private Optional<ButtonType> showConfirmationDialog() { //TODO REWRITE
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("Confirm Delete");
alert.setHeaderText("Delete Confirmation");
......@@ -153,21 +197,30 @@ public class BudgetController {
return alert.showAndWait();
}
private Optional<ButtonType> showIllegalBudgetItemDialog() {
/**
* 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.
*/
private void showIllegalBudgetItemDialog() {
Alert alert = new Alert(AlertType.ERROR);
alert.setTitle("Budget amount exceeded");
alert.setHeaderText("Your budget exceeds the max limit");
alert.setContentText("The total budget sum must be below " + general.getMaxAmount());
return alert.showAndWait();
alert.showAndWait();
}
public GeneralBudget loadIncomeDataFromFile(String fileName) throws IOException {
/**
* 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 loadIncomeDataFromFile(String fileName) throws IOException { //TODO REFACTOR
FileHandlingBudget fileHandlingBudget = new FileHandlingBudget();
//Instantiate new budget
if (fileHandlingBudget.isEmpty(fileName)) {
general = new GeneralBudget(31, 1000);
} else {
} else { //Load previous budget
try {
general = fileHandlingBudget.readGeneralBudgetFromFile(fileName);
} catch (IOException e) {
......@@ -177,19 +230,36 @@ 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);
}
/**
* 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(){
this.budgetList.setAll(general.getBudgetItems());
//Refreshing the sum of the amounts of the budget
this.sum.setText(String.valueOf(general.totalSum()));
}
/**
* 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.
* @param event A button click on the buttons on the buttonbar or the next button
* @throws IOException If an error occurs with loading any of the FXML files.
*/
@FXML
public void switchScene(ActionEvent event) throws IOException {
//Always saving the data when switching scenes
saveDataToFile("Budget");
FXMLLoader loader = new FXMLLoader();
if (event.getSource() == expenseBtn) {
......
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