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

Added input validation to budget

parent f4872333
No related branches found
No related tags found
5 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.,!31Added input validation to tableviews
......@@ -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();
......
......@@ -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);
......
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