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

"Validate inputs for income"

parent ee9af4b0
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
......@@ -112,6 +112,7 @@ public class AddBudgetController {
* Closes the dialog box.
* @param actionEvent A button click on the close button.
*/
@FXML
private void closeButton(ActionEvent actionEvent) {
final Node source = (Node) actionEvent.getSource();
final Stage stage = (Stage) source.getScene().getWindow();
......
......@@ -14,6 +14,7 @@ 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.Economics.Expense;
import no.ntnu.idatt1002.demo.data.Economics.Income;
......@@ -36,6 +37,8 @@ public class AddIncomeController {
@FXML
private Button okBtn;
@FXML
private Text errorMsg;
@FXML
private DatePicker datePicker;
......@@ -72,6 +75,8 @@ public class AddIncomeController {
//Set date to today
datePicker.setValue(LocalDate.now());
addEventFilters();
}
public IncomeCategory getCategory() {
......@@ -121,7 +126,7 @@ public class AddIncomeController {
@FXML
public void pressOkBtn(ActionEvent event) {
//Instantiates a new income
if (chosenIncome == null) {
if (newIncome == null) {
LocalDate date = datePicker.getValue();
double amount = Double.parseDouble(amountField.getText());
String description = descriptionField.getText();
......@@ -142,6 +147,26 @@ public class AddIncomeController {
((Stage) source.getScene().getWindow()).close();
}
private void addEventFilters() {
okBtn.addEventFilter(
ActionEvent.ACTION, event -> {
if (!validateInputs()) {
event.consume();
errorMsg.setOpacity(1);
}
});
}
private boolean validateInputs() {
try {
Income income = new Income(
Double.parseDouble(amountField.getText()), recurringBox.getValue(),
categoryBox.getValue(), datePicker.getValue());
} catch (IllegalArgumentException e) {
System.out.println("ok");
return false;
}
return true;
}
/**
* Closes the dialog box and cancels any pending changes.
......
......@@ -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.AddIncomeController">
<content>
......@@ -27,27 +29,62 @@
<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="Choose" GridPane.columnIndex="1" GridPane.rowIndex="3" />
<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