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

"Introduced input validation to addExpense. Made methods in AddBudgetController private"

parent d71903ea
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
......@@ -89,7 +89,7 @@ public class AddBudgetController {
* @param event If the OK button is pressed.
*/
@FXML
public void pressOkBtn(ActionEvent event) {
private void pressOkBtn(ActionEvent event) {
//Instantiates a new budget item
if(newBudgetItem == null){
ExpenseCategory category = getCategory();
......@@ -112,7 +112,7 @@ public class AddBudgetController {
* Closes the dialog box.
* @param actionEvent A button click on the close button.
*/
public void closeButton(ActionEvent actionEvent) {
private void closeButton(ActionEvent actionEvent) {
final Node source = (Node) actionEvent.getSource();
final Stage stage = (Stage) source.getScene().getWindow();
stage.close();
......
......@@ -14,7 +14,9 @@ import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.DatePicker;
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.Expense;
import no.ntnu.idatt1002.demo.data.Economics.ExpenseCategory;
import no.ntnu.idatt1002.demo.data.Economics.Expense;
......@@ -39,6 +41,9 @@ public class AddExpenseController {
@FXML
private TextField dateField;
@FXML
private Text errorMsg;
@FXML
private DatePicker datePicker;
......@@ -75,6 +80,9 @@ public class AddExpenseController {
//Set date to today
datePicker.setValue(LocalDate.now());
//Adding event filter to okBtn
addEventFilters();
}
public ExpenseCategory getCategory() {
......@@ -138,6 +146,8 @@ public class AddExpenseController {
chosenExpense.setDate(datePicker.getValue());
}
errorMsg.setOpacity(0);
final Node source = (Node) event.getSource();
((Stage) source.getScene().getWindow()).close();
}
......@@ -153,4 +163,27 @@ public class AddExpenseController {
stage.close();
}
private void addEventFilters() {
okBtn.addEventFilter(
ActionEvent.ACTION, event -> {
if (!validateInputs()) {
event.consume();
errorMsg.setOpacity(1);
}
});
}
private boolean validateInputs() {
try {
Expense expense = new Expense(
Double.parseDouble(amountField.getText()), recurringBox.getValue(),
categoryBox.getValue(), datePicker.getValue());
} catch (IllegalArgumentException e) {
System.out.println("ok");
return false;
}
return true;
}
}
\ No newline at end of file
......@@ -11,6 +11,8 @@
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<DialogPane expanded="true" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" fx:controller="no.ntnu.idatt1002.demo.controller.AddExpenseController">
<content>
......@@ -27,27 +29,55 @@
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="Date:" />
<Label text="Amount:" GridPane.rowIndex="1" />
<Label text="Description:" GridPane.rowIndex="2" />
<Label text="Category" GridPane.rowIndex="3" />
<Label text="Recurring" GridPane.rowIndex="4" />
<Label text="Date:">
<font>
<Font name="Lucida Console" size="12.0" />
</font></Label>
<Label text="Amount:" GridPane.rowIndex="1">
<font>
<Font name="Lucida Console" size="12.0" />
</font></Label>
<Label text="Description:" GridPane.rowIndex="2">
<font>
<Font name="Lucida Console" size="12.0" />
</font></Label>
<Label text="Category: " GridPane.rowIndex="3">
<font>
<Font name="Lucida Console" size="12.0" />
</font></Label>
<Label text="Recurring: " GridPane.rowIndex="4">
<font>
<Font name="Lucida Console" size="12.0" />
</font></Label>
<TextField fx:id="amountField" promptText="100" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<TextField fx:id="descriptionField" promptText="(optional)" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<ComboBox fx:id="categoryBox" prefWidth="150.0" promptText="Food" GridPane.columnIndex="1" GridPane.rowIndex="3" />
<ComboBox fx:id="recurringBox" prefWidth="150.0" promptText="No" GridPane.columnIndex="1" GridPane.rowIndex="4" />
<HBox alignment="BOTTOM_RIGHT" prefHeight="100.0" prefWidth="200.0" spacing="10.0" GridPane.columnIndex="1" GridPane.columnSpan="2" GridPane.rowIndex="5">
<HBox alignment="BOTTOM_RIGHT" prefHeight="100.0" prefWidth="200.0" spacing="10.0" GridPane.columnIndex="1" GridPane.columnSpan="2" GridPane.rowIndex="7">
<children>
<Button fx:id="cancelBtn" mnemonicParsing="false" onAction="#pressCancelBtn" prefHeight="25.0" prefWidth="60.0" text="Cancel" />
<Button fx:id="okBtn" mnemonicParsing="false" onAction="#pressOkBtn" prefHeight="25.0" prefWidth="60.0" text="OK" />
<Button fx:id="cancelBtn" mnemonicParsing="false" onAction="#pressCancelBtn" prefHeight="25.0" prefWidth="60.0" text="Cancel">
<font>
<Font name="Lucida Console" size="12.0" />
</font></Button>
<Button fx:id="okBtn" mnemonicParsing="false" onAction="#pressOkBtn" prefHeight="25.0" prefWidth="60.0" text="OK">
<font>
<Font name="Lucida Console" size="12.0" />
</font></Button>
</children>
<GridPane.margin>
<Insets top="20.0" />
</GridPane.margin>
</HBox>
<DatePicker fx:id="datePicker" GridPane.columnIndex="1" />
<Text fx:id="errorMsg" fill="#d90808" opacity="0.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Invalid inputs. Please make surce that amount is above 1 and that category and recurring have values" wrappingWidth="300.0" GridPane.columnSpan="3" GridPane.rowIndex="5" GridPane.rowSpan="2">
<font>
<Font name="Lucida Console" size="11.0" />
</font>
</Text>
</children>
</GridPane>
</content>
......
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