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

Created controller for AddExpense fxml file

parent ed375ce9
No related branches found
No related tags found
10 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.,!30Redesigned scenes,!29Redesigned scenes,!28Redesigned scenes,!26Redesigned Main menu and expense/income windows,!24Merging frontend-testing with master,!23Merging frontend-testing and master
package no.ntnu.idatt1002.demo.controller;
import java.text.NumberFormat;
import java.time.LocalDate;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import no.ntnu.idatt1002.demo.data.Economics.Expense;
import no.ntnu.idatt1002.demo.data.Economics.ExpenseCategory;
public class AddExpenseController {
Expense newExpense = null; //the expense that is chosen when editing or the expense that is created when adding
Expense oldExpense = null; //an expense that is meant to track the old state of an expense before editing, in case cancel bugtton is clicked
@FXML
private Button cancelBtn;
@FXML
private Button okBtn;
@FXML
private TextField dateField;
@FXML
private TextField descriptionField;
@FXML
private TextField amountField;
@FXML
private ComboBox<ExpenseCategory> categoryBox;
@FXML
private ComboBox<Boolean> recurringBox;
@FXML
public void initialize() {
ObservableList<ExpenseCategory> expenseCategories = FXCollections.observableArrayList(
ExpenseCategory.values());
categoryBox.setItems(expenseCategories);
categoryBox.setValue(ExpenseCategory.FOOD);
ObservableList<Boolean> recurring = FXCollections.observableArrayList(true, false);
recurringBox.setItems(recurring);
recurringBox.setValue(false);
System.out.print("This is in initialize");
System.out.println(descriptionField);
}
public ExpenseCategory getCategory() {
return categoryBox.getValue();
}
public boolean isRecurring() {
return recurringBox.getValue();//.equals("Yes");
}
public void setExpense(Expense expense) { //TODO NEED CANCEL BUTTON TO REMOVE THE CHANGES IF CANCEL IS PRESSED
dateField.textProperty().bindBidirectional(new SimpleStringProperty(expense.getDate().toString()));
amountField.textProperty().bindBidirectional(expense.amountProperty(), NumberFormat.getNumberInstance()); //TODO AMOUNT IS STORED WITH COMMA, WHICH IS NOT ALLOWED
descriptionField.textProperty().bindBidirectional(expense.descriptionProperty());
//categoryBox.valueProperty().bindBidirectional(expense.getCategory());
recurringBox.valueProperty().bindBidirectional(expense.recurringProperty());
}
@FXML
public void pressOkBtn(ActionEvent event) {
LocalDate date = LocalDate.parse(dateField.getText());
double amount = Double.parseDouble(amountField.getText());
String description = descriptionField.getText();
ExpenseCategory category = getCategory();
boolean recurring = isRecurring();
newExpense = new Expense(description, amount, recurring, category, date);
System.out.println(date + " " + amount + " " + description + " " + category + " " + recurring);
final Node source = (Node) event.getSource();
((Stage) source.getScene().getWindow()).close();
}
public class AddExpenseController {
@FXML
public void pressCancelBtn(ActionEvent event) {
final Node source = (Node) event.getSource();
final Stage stage = (Stage) source.getScene().getWindow();
Expense expense;
stage.close();
}
public void setExpense(Expense expense) {
this.expense = expense;
public Expense getNewExpense() {
return this.newExpense;
}
}
\ No newline at end of file
}
package no.ntnu.idatt1002.demo.controller;public class ExpenseController {
}
package no.ntnu.idatt1002.demo.view;public class DialogEnum {
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ButtonType?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.DialogPane?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.RowConstraints?>
<DialogPane expanded="true" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" fx:controller="no.ntnu.idatt1002.demo.controller.AddExpenseController">
<DialogPane xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" fx:controller="no.ntnu.idatt1002.demo.controller.SceneController">
<content>
<GridPane>
<GridPane hgap="10.0" vgap="10.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="95.0" minWidth="10.0" prefWidth="11.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="189.0" minWidth="10.0" prefWidth="189.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
......@@ -25,29 +25,34 @@
<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>
<padding>
<Insets bottom="10.0" left="10.0" top="20.0" />
</padding>
<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" />
<TextField fx:id="dateField" promptText="1/1/23" GridPane.columnIndex="1" />
<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">
<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" />
</children>
<Label text="Category" GridPane.rowIndex="3">
<GridPane.margin>
<Insets top="20.0" />
<Insets />
</GridPane.margin>
</HBox>
</Label>
<TextField fx:id="dateField" prefHeight="25.0" prefWidth="16.0" promptText="Date" GridPane.columnIndex="1" GridPane.columnSpan="2" />
<TextField fx:id="amountField" promptText="Amount" GridPane.columnIndex="1" GridPane.columnSpan="2" GridPane.rowIndex="1" />
<TextField fx:id="descriptionField" promptText="Description (optional)" GridPane.columnIndex="1" GridPane.columnSpan="2" GridPane.rowIndex="2" />
<ComboBox fx:id="recurringBox" disable="true" prefWidth="150.0" GridPane.columnIndex="1" GridPane.rowIndex="4">
<GridPane.margin>
<Insets />
</GridPane.margin>
</ComboBox>
<Label text="Recurring" GridPane.rowIndex="4" />
<ComboBox fx:id="categoryBox" disable="true" prefHeight="25.0" prefWidth="214.0" GridPane.columnIndex="1" GridPane.rowIndex="3" />
</children>
</GridPane>
</content>
<buttonTypes>
<ButtonType fx:constant="CANCEL" />
<ButtonType fx:constant="OK" />
</buttonTypes>
</DialogPane>
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