diff --git a/src/main/java/no/ntnu/idatt1002/demo/controller/AddBudgetController.java b/src/main/java/no/ntnu/idatt1002/demo/controller/AddBudgetController.java index 6a9891f542882a0cbb85ee83b64deef963a64d64..98f68c9f435fdac2e692fa6e4875d3a897914df1 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/controller/AddBudgetController.java +++ b/src/main/java/no/ntnu/idatt1002/demo/controller/AddBudgetController.java @@ -7,8 +7,10 @@ import javafx.fxml.FXML; import javafx.scene.Node; import javafx.scene.control.Button; import javafx.scene.control.ComboBox; +import javafx.scene.control.Label; import javafx.scene.control.TextField; +import javafx.scene.text.Text; import javafx.stage.Stage; import no.ntnu.idatt1002.demo.data.Budget.BudgetItem; import no.ntnu.idatt1002.demo.data.Economics.ExpenseCategory; @@ -37,7 +39,10 @@ public class AddBudgetController { private Button okBtn; @FXML - private Button cancelButton; + private Button cancelBtn; + + @FXML + private Text errorMsg; /** * Initializes the category drop box by filling it with all the values from the ExpenseCategory enum. @@ -51,7 +56,10 @@ public class AddBudgetController { //Set the values inside the dropbox categoryVariable.setItems(expenseCategories); //Set default value - categoryVariable.setPromptText("Category"); + categoryVariable.setValue(ExpenseCategory.FOOD); + + addEventFilters(); + } public ExpenseCategory getCategory(){ @@ -108,12 +116,37 @@ public class AddBudgetController { stage.close(); } + private void addEventFilters() { + okBtn.addEventFilter( + ActionEvent.ACTION, event -> { + try { + validateInputs(); + } catch(IllegalArgumentException e) { + event.consume(); + errorMsg.setOpacity(1); + } + } + ); + } + + private boolean validateInputs() { + try { + BudgetItem item = new BudgetItem( + Double.parseDouble(amountVariable.getText()), + descriptionVariable.getText(), + categoryVariable.getValue()); + } catch (IllegalArgumentException e) { + throw new IllegalArgumentException("Invalid inputs. Cannot instantiate item", e); + } + return true; + } + /** * Closes the dialog box. * @param actionEvent A button click on the close button. */ @FXML - private void closeButton(ActionEvent actionEvent) { + private void pressCancelBtn(ActionEvent actionEvent) { final Node source = (Node) actionEvent.getSource(); final Stage stage = (Stage) source.getScene().getWindow(); stage.close(); 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 a638dcf356a695d16d3cc7c68a92055c9ed3fcb3..ee55a6f098547b76952830ef57e81a1b642d3f16 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/controller/BudgetController.java +++ b/src/main/java/no/ntnu/idatt1002/demo/controller/BudgetController.java @@ -78,7 +78,10 @@ public class BudgetController implements FinanceController { @FXML public void initialize() throws IOException { - //TODO if budget is not empty - disable + //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 make budget item throw exception with negative amount + //TODO specify error messgage for when amount is exceeded / duplicate exists //Initialize table columns categoryCol.setCellValueFactory(new PropertyValueFactory<BudgetItem, ExpenseCategory>("budgetCategory")); amountCol.setCellValueFactory(new PropertyValueFactory<BudgetItem, Double>("budgetAmount")); @@ -129,7 +132,7 @@ public class BudgetController implements FinanceController { String dialogTitle = ""; DialogMode dialogMode; - FXMLLoader loader = new FXMLLoader(SceneController.class.getResource("/view/AddBudget.fxml")); + FXMLLoader loader = new FXMLLoader(SceneController.class.getResource("/view/AddBudgetNew.fxml")); Dialog<BudgetItem> dialog = new Dialog<>(); dialog.initModality(Modality.APPLICATION_MODAL); @@ -255,6 +258,7 @@ public class BudgetController implements FinanceController { //Instantiate new budget if (fileHandlingBudget.isEmpty(fileName)) { general = new GeneralBudget(31, 1000); + //throws new IOException("Not valid budget") } else { //Load previous budget try { general = fileHandlingBudget.readGeneralBudgetFromFile(fileName);