From 1c419752e37ad4834a0f693cc2a57d0999d18451 Mon Sep 17 00:00:00 2001 From: Harry Linrui XU <xulr0820@hotmail.com> Date: Sun, 16 Apr 2023 16:24:11 +0200 Subject: [PATCH] Catch blocks all display alert box --- .../controller/IncomeExpenseController.java | 127 ++++++++++-------- src/main/resources/Budget/Budget.budget | 8 ++ .../demo/data/Budget/GeneralBudgetTest.java | 9 ++ 3 files changed, 89 insertions(+), 55 deletions(-) diff --git a/src/main/java/no/ntnu/idatt1002/demo/controller/IncomeExpenseController.java b/src/main/java/no/ntnu/idatt1002/demo/controller/IncomeExpenseController.java index c67d16b7..7163bec9 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/controller/IncomeExpenseController.java +++ b/src/main/java/no/ntnu/idatt1002/demo/controller/IncomeExpenseController.java @@ -1,6 +1,5 @@ package no.ntnu.idatt1002.demo.controller; -import java.awt.event.ActionEvent; import java.io.IOException; import java.time.LocalDate; import java.util.Optional; @@ -27,7 +26,6 @@ import javafx.scene.control.ProgressBar; import javafx.scene.control.TableColumn; import javafx.scene.control.TableView; import javafx.scene.control.cell.PropertyValueFactory; -import javafx.scene.text.Text; import javafx.stage.Modality; import javafx.stage.Stage; import no.ntnu.idatt1002.demo.data.Budget.FileHandlingBudget; @@ -158,7 +156,7 @@ public class IncomeExpenseController implements FinanceController { //Initialize columns setColumns(); - try { + //Initialize registers and tableview incomeRegister = loadIncomeDataFromFile("Income"); income = FXCollections.observableArrayList(incomeRegister.getItems()); @@ -175,9 +173,6 @@ public class IncomeExpenseController implements FinanceController { expensePieChart.setLabelLineLength(10); refreshPieCharts(); - } catch(IOException ioe) { - errorMsg.setOpacity(1); - } refreshProgress(); @@ -190,17 +185,17 @@ public class IncomeExpenseController implements FinanceController { } private void setColumns() { - inDateCol.setCellValueFactory(new PropertyValueFactory<Income, String>("date")); - inAmountCol.setCellValueFactory(new PropertyValueFactory<Income, Double>("amount")); - inCategoryCol.setCellValueFactory(new PropertyValueFactory<Income, IncomeCategory>("category")); - inDescriptionCol.setCellValueFactory(new PropertyValueFactory<Income, String>("description")); - inRecurringCol.setCellValueFactory(new PropertyValueFactory<Income, Boolean>("recurring")); - - expDateCol.setCellValueFactory(new PropertyValueFactory<Expense, String>("date")); - expAmountCol.setCellValueFactory(new PropertyValueFactory<Expense, Double>("amount")); - expCategoryCol.setCellValueFactory(new PropertyValueFactory<Expense, ExpenseCategory>("category")); - expDescriptionCol.setCellValueFactory(new PropertyValueFactory<Expense, String>("description")); - expRecurringCol.setCellValueFactory(new PropertyValueFactory<Expense, Boolean>("recurring")); + inDateCol.setCellValueFactory(new PropertyValueFactory<>("date")); + inAmountCol.setCellValueFactory(new PropertyValueFactory<>("amount")); + inCategoryCol.setCellValueFactory(new PropertyValueFactory<>("category")); + inDescriptionCol.setCellValueFactory(new PropertyValueFactory<>("description")); + inRecurringCol.setCellValueFactory(new PropertyValueFactory<>("recurring")); + + expDateCol.setCellValueFactory(new PropertyValueFactory<>("date")); + expAmountCol.setCellValueFactory(new PropertyValueFactory<>("amount")); + expCategoryCol.setCellValueFactory(new PropertyValueFactory<>("category")); + expDescriptionCol.setCellValueFactory(new PropertyValueFactory<>("description")); + expRecurringCol.setCellValueFactory(new PropertyValueFactory<>("recurring")); } private ObservableList<PieChart.Data> createExpensePieChart() { @@ -328,7 +323,7 @@ public class IncomeExpenseController implements FinanceController { FXMLLoader loader = new FXMLLoader(); loader.setLocation(getClass().getResource("/view/AddIncome.fxml")); - Income newIncome = null; + Income newIncome; String dialogTitle = "Add income"; // Load the FXML file for your dialog box Dialog<Income> dialog = new Dialog<>(); @@ -338,8 +333,8 @@ public class IncomeExpenseController implements FinanceController { // Set the Dialog's content to the loaded FXML file dialog.getDialogPane().setContent(loader.load()); } catch (IOException e) { - errorMsg.setText("Error in loading in dialog box"); - errorMsg.setOpacity(1); + showErrorDialogBox("Loading", "Error in loading dialog box", "An error occurred" + + "while loading AddIncome window"); } // Get the controller for the loaded FXML file @@ -364,7 +359,7 @@ public class IncomeExpenseController implements FinanceController { FXMLLoader loader = new FXMLLoader(); loader.setLocation(getClass().getResource("/view/AddExpense.fxml")); - Expense newExpense = null; + Expense newExpense; String dialogTitle = "Add expense"; // Load the FXML file for your dialog box Dialog<Expense> dialog = new Dialog<>(); @@ -374,8 +369,8 @@ public class IncomeExpenseController implements FinanceController { // Set the Dialog's content to the loaded FXML file dialog.getDialogPane().setContent(loader.load()); } catch (IOException e) { - errorMsg.setText("Error in loading in dialog box"); - errorMsg.setOpacity(1); + showErrorDialogBox("Loading", "Error in loading dialog box", "An error occurred" + + "while loading AddExpense window"); } // Get the controller for the loaded FXML file @@ -412,7 +407,8 @@ public class IncomeExpenseController implements FinanceController { // Set the Dialog's content to the loaded FXML file dialog.getDialogPane().setContent(loader.load()); } catch (IOException e) { - e.printStackTrace(); + showErrorDialogBox("Loading", "Error in loading dialog box", "An error occurred" + + "while loading EditIncome window"); } // Get the controller for the loaded FXML file @@ -443,7 +439,8 @@ public class IncomeExpenseController implements FinanceController { // Set the Dialog's content to the loaded FXML file dialog.getDialogPane().setContent(loader.load()); } catch (IOException e) { - e.printStackTrace(); + showErrorDialogBox("Loading", "Error in loading dialog box", "An error occurred" + + "while loading EditExpense window"); } // Get the controller for the loaded FXML file @@ -459,7 +456,10 @@ public class IncomeExpenseController implements FinanceController { @FXML private void handleDeleteIncome(Income chosenIncome) { - Optional<ButtonType> isConfirmed = showConfirmationDialog(); + String title = "Confirm Delete" ; + String header = "Delete Confirmation"; + String content = "Are you sure you would like to delete the selected income?"; + Optional<ButtonType> isConfirmed = showConfirmationDialog(title, header, content); if (isConfirmed.isPresent() && isConfirmed.get() == ButtonType.OK) { incomeRegister.removeItem(chosenIncome); } @@ -468,7 +468,10 @@ public class IncomeExpenseController implements FinanceController { @FXML private void handleDeleteExpense(Expense chosenExpense) { - Optional<ButtonType> isConfirmed = showConfirmationDialog(); + String title = "Confirm Delete" ; + String header = "Delete Confirmation"; + String content = "Are you sure you would like to delete the selected expense?"; + Optional<ButtonType> isConfirmed = showConfirmationDialog(title, header, content); if (isConfirmed.isPresent() && isConfirmed.get() == ButtonType.OK) { expenseRegister.removeItem(chosenExpense); } @@ -481,7 +484,7 @@ public class IncomeExpenseController implements FinanceController { * @return An alert box, asking for confirmation for deleting the selected entry of the tableview. */ @Override - public Optional<ButtonType> showConfirmationDialog() { + public Optional<ButtonType> showConfirmationDialog(String title, String header, String content) { Alert alert = new Alert(AlertType.CONFIRMATION); alert.setTitle("Confirm Delete"); alert.setHeaderText("Delete Confirmation"); @@ -489,6 +492,20 @@ public class IncomeExpenseController implements FinanceController { return alert.showAndWait(); } + + + /** + * Displays an alert box of type error, informing of a custom error. + */ + private 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(); + } + + /** * 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. @@ -504,18 +521,18 @@ public class IncomeExpenseController implements FinanceController { * 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. - * @throws IOException If an error occurs while reading from the file. */ - public IncomeRegister loadIncomeDataFromFile(String fileName) throws IOException { + public IncomeRegister loadIncomeDataFromFile(String fileName) { //Instantiate new incomeRegister - if (fileHandling.isEmpty(fileName)) { - incomeRegister = new IncomeRegister(); - } else { //Load previous income register - try { - incomeRegister = fileHandling.readIncomeRegisterFromFile(fileName); - } catch (IOException e) { - e.printStackTrace(); + 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", "An error occurred" + + "while reading the IncomeRegister from file"); } return incomeRegister; } @@ -524,18 +541,18 @@ public class IncomeExpenseController implements FinanceController { * 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. - * @throws IOException If an error occurs while reading from the file. */ - public ExpenseRegister loadExpenseDataFromFile(String fileName) throws IOException { + public ExpenseRegister loadExpenseDataFromFile(String fileName) { //ItemRegister<T extends Item> - if (fileHandling.isEmpty(fileName)) { - expenseRegister = new ExpenseRegister(); - } else { - try { + try { + if (fileHandling.isEmpty(fileName)) { + expenseRegister = new ExpenseRegister(); + } else { //Load previous income register expenseRegister = fileHandling.readExpenseRegisterFromFile(fileName); - } catch (IOException e) { - e.printStackTrace(); } + } catch (IOException ioe) { + showErrorDialogBox("File reading error", "Error in reading from file", "An error occurred" + + "while reading the ExpenseRegister from file"); } return expenseRegister; } @@ -544,19 +561,19 @@ public class IncomeExpenseController implements FinanceController { * 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 { + public GeneralBudget loadBudgetDataFromFile(String fileName) { FileHandlingBudget fileHandlingBudget = new FileHandlingBudget(); //Instantiate new budget - if (fileHandlingBudget.isEmpty(fileName)) { - generalBudget = new GeneralBudget(31, 1000); - } else { //Load previous budget - try { + try { + if (fileHandling.isEmpty(fileName)) { + generalBudget = new GeneralBudget(31, 1000); + } else { //Load previous income register generalBudget = fileHandlingBudget.readGeneralBudgetFromFile(fileName); - } catch (IOException e) { - e.printStackTrace(); } + } catch (IOException ioe) { + showErrorDialogBox("File reading error", "Error in reading from file", "An error occurred" + + "while reading the GeneralBudget from file"); } return generalBudget; } @@ -578,8 +595,8 @@ public class IncomeExpenseController implements FinanceController { stage.setScene(scene); stage.show(); } catch(IOException ioe) { - errorMsg.setText("Error in saving to file"); - errorMsg.setOpacity(1); + showErrorDialogBox("Loading error", "Error in loading", "An error occurred" + + "while saving saving to file and loading next window"); } } diff --git a/src/main/resources/Budget/Budget.budget b/src/main/resources/Budget/Budget.budget index 08268ba2..a1737fc1 100644 --- a/src/main/resources/Budget/Budget.budget +++ b/src/main/resources/Budget/Budget.budget @@ -9,3 +9,11 @@ budgetAmount=100.0 budgetCategory=CLOTHES budgetDescription= +budgetAmount=0.0 +budgetCategory=BOOKS +budgetDescription= + +budgetAmount=0.0 +budgetCategory=OTHER +budgetDescription= + diff --git a/src/test/java/no/ntnu/idatt1002/demo/data/Budget/GeneralBudgetTest.java b/src/test/java/no/ntnu/idatt1002/demo/data/Budget/GeneralBudgetTest.java index d4ee0643..bd6064b4 100644 --- a/src/test/java/no/ntnu/idatt1002/demo/data/Budget/GeneralBudgetTest.java +++ b/src/test/java/no/ntnu/idatt1002/demo/data/Budget/GeneralBudgetTest.java @@ -104,4 +104,13 @@ class GeneralBudgetTest { assertTrue(list.isEmpty()); } + + /* @Test + @DisplayName("Gets the number of days left in the month. 17 has to be changed to the actual number of days left in the month.") + void get_days_left_of_the_month(){ + List<BudgetItem> list = new ArrayList<>(); + GeneralBudget budget1 = new GeneralBudget(12, list, 1200); + + assertEquals(17, budget1.getDaysLeftOfBudgetPeriod()); + }*/ } \ No newline at end of file -- GitLab