Skip to content
Snippets Groups Projects
Commit 757d85dd authored by Harry Linrui Xu's avatar Harry Linrui Xu
Browse files

Merge branch 'frontend-testing' into 'master'

Merging frontend-testing with master

See merge request !24
parents 6140c592 d2d0c44e
No related branches found
No related tags found
1 merge request!24Merging frontend-testing with master
Pipeline #212786 passed
Showing
with 976 additions and 57 deletions
##############################
## Java
##############################
.mtj.tmp/
*.class
*.jar
*.war
*.ear
*.nar
##############################
## Maven
##############################
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
pom.xml.bak
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
.mvn/wrapper/maven-wrapper.jar
##############################
## IntelliJ
##############################
out/
.idea/*
*.iml
*.ipr
*.iws
##############################
## Database
##############################
*.db
*.DS_Store
......@@ -14,7 +14,7 @@
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.version>5.9.2</junit.version>
<junit.version>5.8.1</junit.version>
<javafx.version>19.0.2.1</javafx.version>
</properties>
......
......@@ -12,14 +12,18 @@ import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.DatePicker;
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;
import no.ntnu.idatt1002.demo.data.Economics.Income;
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
Expense chosenExpense = 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;
......@@ -29,6 +33,9 @@ public class AddExpenseController {
@FXML
private TextField dateField;
@FXML
private DatePicker datePicker;
@FXML
private TextField descriptionField;
......@@ -52,8 +59,7 @@ public class AddExpenseController {
recurringBox.setItems(recurring);
recurringBox.setValue(false);
System.out.print("This is in initialize");
System.out.println(descriptionField);
datePicker.setValue(LocalDate.now());
}
public ExpenseCategory getCategory() {
......@@ -65,22 +71,37 @@ public class AddExpenseController {
}
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());
chosenExpense = new Expense(expense.getDescription(), expense.getAmount(), expense.isRecurring(), expense.getCategory(), expense.getDate());
chosenExpense.descriptionProperty().bindBidirectional(expense.descriptionProperty());
chosenExpense.amountProperty().bindBidirectional(expense.amountProperty());
chosenExpense.recurringProperty().bindBidirectional(expense.recurringProperty());
chosenExpense.expenseCategoryObjectProperty().bindBidirectional(expense.expenseCategoryObjectProperty());
chosenExpense.dateProperty().bindBidirectional(expense.dateProperty());
descriptionField.textProperty().set(expense.getDescription());
amountField.textProperty().setValue(String.valueOf(expense.getAmount()));
recurringBox.setValue(expense.isRecurring());
datePicker.setValue(expense.getDate());
categoryBox.setValue(expense.getCategory());
}
@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);
if (newExpense == null) {
LocalDate date = datePicker.getValue();
double amount = Double.parseDouble(amountField.getText());
String description = descriptionField.getText();
ExpenseCategory category = getCategory();
boolean recurring = isRecurring();
newExpense = new Expense(description, amount, recurring, category, date);
}
if (chosenExpense != null) {
chosenExpense.setDescription((descriptionField.getText()));
chosenExpense.setAmount(Double.parseDouble(amountField.getText()));
chosenExpense.setRecurring(recurringBox.getValue());
chosenExpense.setCategory(categoryBox.getValue());
chosenExpense.setDate(datePicker.getValue());
}
final Node source = (Node) event.getSource();
((Stage) source.getScene().getWindow()).close();
......
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.DatePicker;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import no.ntnu.idatt1002.demo.data.Economics.Expense;
import no.ntnu.idatt1002.demo.data.Economics.Income;
import no.ntnu.idatt1002.demo.data.Economics.IncomeCategory;
public class AddIncomeController {
Income newIncome = null;
Income chosenIncome = null;
@FXML
private Button cancelBtn;
@FXML
private Button okBtn;
@FXML
private DatePicker datePicker;
@FXML
private TextField descriptionField;
@FXML
private TextField amountField;
@FXML
private ComboBox<IncomeCategory> categoryBox;
@FXML
private ComboBox<Boolean> recurringBox;
@FXML
public void initialize() {
ObservableList<IncomeCategory> incomeCategories = FXCollections.observableArrayList(
IncomeCategory.values());
categoryBox.setItems(incomeCategories);
categoryBox.setValue(IncomeCategory.GIFT);
ObservableList<Boolean> recurring = FXCollections.observableArrayList(true, false);
recurringBox.setItems(recurring);
recurringBox.setValue(false);
datePicker.setValue(LocalDate.now());
}
public IncomeCategory getCategory() {
return categoryBox.getValue();
}
public boolean isRecurring() {
return recurringBox.getValue();//.equals("Yes");
}
public void setIncome(Income income) { //TODO NEED CANCEL BUTTON TO REMOVE THE CHANGES IF CANCEL IS PRESSED
chosenIncome = new Income(income.getDescription(), income.getAmount(), income.isRecurring(), income.getCategory(), income.getDate());
chosenIncome.descriptionProperty().bindBidirectional(income.descriptionProperty());
chosenIncome.amountProperty().bindBidirectional(income.amountProperty());
chosenIncome.recurringProperty().bindBidirectional(income.recurringProperty());
chosenIncome.incomeCategoryObjectProperty().bindBidirectional(income.incomeCategoryObjectProperty());
chosenIncome.dateProperty().bindBidirectional(income.dateProperty());
descriptionField.textProperty().set(income.getDescription());
amountField.textProperty().setValue(String.valueOf(income.getAmount()));
recurringBox.setValue(income.isRecurring());
descriptionField.textProperty().set(income.getDescription());
amountField.textProperty().setValue(String.valueOf(income.getAmount()));
recurringBox.setValue(income.isRecurring());
datePicker.setValue(income.getDate());
categoryBox.setValue(income.getCategory());
}
@FXML
public void pressOkBtn(ActionEvent event) {
if (chosenIncome == null) {
LocalDate date = datePicker.getValue();
double amount = Double.parseDouble(amountField.getText());
String description = descriptionField.getText();
IncomeCategory category = getCategory();
boolean recurring = isRecurring();
newIncome = new Income(description, amount, recurring, category, date);
}
if (chosenIncome != null) {
chosenIncome.setDescription((descriptionField.getText()));
chosenIncome.setAmount(Double.parseDouble(amountField.getText()));
chosenIncome.setRecurring(recurringBox.getValue());
chosenIncome.setCategory(categoryBox.getValue());
chosenIncome.setDate(datePicker.getValue());
}
final Node source = (Node) event.getSource();
((Stage) source.getScene().getWindow()).close();
}
@FXML
public void pressCancelBtn(ActionEvent event) {
final Node source = (Node) event.getSource();
final Stage stage = (Stage) source.getScene().getWindow();
stage.close();
}
public Income getNewIncome() {
return this.newIncome;
}
}
\ No newline at end of file
package no.ntnu.idatt1002.demo.controller;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.text.Text;
import javafx.stage.Modality;
import javafx.stage.Stage;
import no.ntnu.idatt1002.demo.data.Budget.BudgetItem;
import no.ntnu.idatt1002.demo.data.Budget.FileHandlingBudget;
import no.ntnu.idatt1002.demo.data.Budget.GeneralBudget;
import no.ntnu.idatt1002.demo.data.Economics.ExpenseCategory;
import java.io.IOException;
import java.util.Optional;
public class BudgetController {
private GeneralBudget general;
@FXML
private Button addBudget;
@FXML
private Button editBudget;
@FXML
private Button expenseBtn;
@FXML
private Button incomeBtn;
@FXML
private Button returnBtn;
@FXML
private Button nextBtn;
@FXML
private TableColumn<BudgetItem, Double> amountColumn;
@FXML
private TableView<BudgetItem> budgetTableView = new TableView<>();
@FXML
private TableColumn<BudgetItem, ExpenseCategory> categoryColumn;
@FXML
private TableColumn<BudgetItem, String> descriptionColumn;
@FXML
private Text sum;
@FXML
private TableColumn<BudgetItem, Double> percentageColumn;
@FXML
private ObservableList<BudgetItem> budgetList;
public void initialize() throws IOException {
categoryColumn.setCellValueFactory(new PropertyValueFactory<BudgetItem, ExpenseCategory>("budgetCategory"));
amountColumn.setCellValueFactory(new PropertyValueFactory<BudgetItem, Double>("budgetAmount"));
descriptionColumn.setCellValueFactory(new PropertyValueFactory<BudgetItem, String>("budgetDescription"));
general = loadIncomeDataFromFile("Budget");
budgetList = FXCollections.observableArrayList(general.getBudgetItems());
budgetTableView.setItems(budgetList);
sum.setText(String.valueOf(general.totalSum()));
}
@FXML
public void switchAddBudget(javafx.event.ActionEvent event) throws IOException {
BudgetItem item = null;
String dialogTitle = "";
FXMLLoader loader = new FXMLLoader(SceneController.class.getResource("/view/AddBudget.fxml"));
Dialog<BudgetItem> dialog = new Dialog<>();
dialog.initModality(Modality.APPLICATION_MODAL);
try{
dialog.getDialogPane().setContent(loader.load());
} catch(IOException e) {
e.printStackTrace();
}
AddBudgetController budgetController = loader.getController();
DialogMode dialogMode;
if(event.getSource().equals(addBudget)){
dialogMode = DialogMode.ADD;
dialogTitle = "New Budget";
}
else if (event.getSource().equals(editBudget) && budgetTableView.getSelectionModel().getSelectedItem() != null) {
dialogMode = DialogMode.EDIT;
dialogTitle = "Edit expense";
item = budgetTableView.getSelectionModel().getSelectedItem();
budgetController.setBudget(item);
} else {
return;
}
dialog.setTitle(dialogTitle);
dialog.showAndWait();
item = budgetController.getNewBudgetItem();
if(item != null && dialogMode == DialogMode.ADD){
try {
general.addToBudgetBudgetItem(item);
} catch(IllegalArgumentException e) {
showIllegalBudgetItemDialog();
}
}
refreshTableView();
}
@FXML
public void closeButton(javafx.event.ActionEvent actionEvent) {
final Node source = (Node) actionEvent.getSource();
final Stage stage = (Stage) source.getScene().getWindow();
stage.close();
}
@FXML
public void deleteButton(ActionEvent event) {
BudgetItem item = budgetTableView.getSelectionModel().getSelectedItem();
if (item == null) {
return;
}
Optional<ButtonType> isConfirmed = showConfirmationDialog();
if (isConfirmed.isPresent() && isConfirmed.get() == ButtonType.OK) {
general.deleteItemFromBudget(item.getBudgetCategory());
refreshTableView();
}
}
private Optional<ButtonType> showConfirmationDialog() {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("Confirm Delete");
alert.setHeaderText("Delete Confirmation");
alert.setContentText("Are you sure you would like to delete the selected entry?");
return alert.showAndWait();
}
private Optional<ButtonType> showIllegalBudgetItemDialog() {
Alert alert = new Alert(AlertType.ERROR);
alert.setTitle("Budget amount exceeded");
alert.setHeaderText("Your budget exceeds the max limit");
alert.setContentText("The total budget sum must be below " + general.getMaxAmount());
return alert.showAndWait();
}
public GeneralBudget loadIncomeDataFromFile(String fileName) throws IOException {
FileHandlingBudget fileHandlingBudget = new FileHandlingBudget();
if (fileHandlingBudget.isEmpty(fileName)) {
general = new GeneralBudget(31, 1000);
} else {
try {
general = fileHandlingBudget.readGeneralBudgetFromFile(fileName);
} catch (IOException e) {
e.printStackTrace();
}
}
return general;
}
public void saveDataToFile(String fileName) throws IOException {
FileHandlingBudget fileHandlingBudget = new FileHandlingBudget();
fileHandlingBudget.writeGeneralBudgetToFile(fileName, general);
}
protected void refreshTableView(){
this.budgetList.setAll(general.getBudgetItems());
this.sum.setText(String.valueOf(general.totalSum()));
}
@FXML
public void switchScene(ActionEvent event) throws IOException {
saveDataToFile("Budget");
FXMLLoader loader = new FXMLLoader();
if (event.getSource() == expenseBtn) {
loader.setLocation(SceneController.class.getResource("/view/Expenses.fxml"));
} else if (event.getSource() == returnBtn) {
loader.setLocation(SceneController.class.getResource("/view/FirstMenu.fxml"));
} else if (event.getSource() == incomeBtn) {
loader.setLocation(SceneController.class.getResource("/view/Income.fxml"));
} else if (event.getSource() == nextBtn) {
loader.setLocation(SceneController.class.getResource("/view/MainMenu.fxml"));
}
Parent root = loader.load();
Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
}
package no.ntnu.idatt1002.demo.controller;
import java.io.IOException;
import java.util.Optional;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Dialog;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.text.Text;
import javafx.stage.Modality;
import javafx.stage.Stage;
import no.ntnu.idatt1002.demo.data.Economics.Expense;
import no.ntnu.idatt1002.demo.data.Economics.ExpenseCategory;
import no.ntnu.idatt1002.demo.data.Economics.ExpenseRegister;
import no.ntnu.idatt1002.demo.data.Economics.FileHandling;
import no.ntnu.idatt1002.demo.data.Economics.Income;
import no.ntnu.idatt1002.demo.data.Economics.IncomeRegister;
import no.ntnu.idatt1002.demo.data.Economics.Item;
import no.ntnu.idatt1002.demo.data.Economics.ItemRegister;
enum DialogMode {
ADD, EDIT, DELETE
}
public class ExpensesController {
@FXML
private Button addBtn;
@FXML
private Button editBtn;
@FXML
private Button deleteBtn;
@FXML
private ComboBox<String> show;
@FXML
private Button incomeBtn;
@FXML
private Text sum;
@FXML
private Button budgetBtn;
@FXML
private Button returnBtn;
@FXML
private TableColumn<Expense, Double> amountColumn;
@FXML
private TableColumn<Expense, ExpenseCategory> categoryColumn;
@FXML
private TableColumn<Expense, String> dateColumn;
@FXML
private TableColumn<Expense, String> descriptionColumn;
@FXML
private TableColumn<Expense, Boolean> recurringColumn;
@FXML
private TableView<Expense> expenseTableView;
@FXML
ExpenseRegister expenseRegister;
ObservableList<Expense> expenses;
ObservableList<String> filter;
@FXML
public void initialize() throws IOException {
dateColumn.setCellValueFactory(new PropertyValueFactory<Expense, String>("date"));
amountColumn.setCellValueFactory(new PropertyValueFactory<Expense, Double>("amount"));
categoryColumn.setCellValueFactory(new PropertyValueFactory<Expense, ExpenseCategory>("category"));
descriptionColumn.setCellValueFactory(new PropertyValueFactory<Expense, String>("description"));
recurringColumn.setCellValueFactory(new PropertyValueFactory<Expense, Boolean>("recurring"));
filter = FXCollections.observableArrayList("All", "Food", "Clothes", "Books", "Other",
"Fixed expense");
show.setItems(filter);
show.setValue("All");
expenseRegister = loadExpenseDataFromFile("Expense");
expenses = FXCollections.observableArrayList(expenseRegister.getItems());
expenseTableView.setItems(expenses);
sum.setText(String.valueOf(expenseRegister.getTotalSum()));
}
@FXML
protected void handleAddButton(ActionEvent event) {
handleEditButton(event);
}
@FXML
protected void handleEditButton(ActionEvent event) {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/view/AddExpense.fxml"));
Expense newExpense = null;
String dialogTitle = "";
// Load the FXML file for your dialog box
Dialog<Expense> dialog = new Dialog<>();
dialog.initModality(Modality.APPLICATION_MODAL);
try {
// Set the Dialog's content to the loaded FXML file
dialog.getDialogPane().setContent(loader.load());
} catch (IOException e) {
e.printStackTrace();
}
// Get the controller for the loaded FXML file
AddExpenseController dialogController = loader.getController();
/**
* The mode of the dialog. NEW if new contact, EDIT if edit existing contact.
*/
DialogMode dialogMode;
if (event.getSource().equals(addBtn)) {
dialogMode = DialogMode.ADD;
dialogTitle = "Add expense";
} else if (event.getSource().equals(editBtn)
&& expenseTableView.getSelectionModel().getSelectedItem() != null) {
dialogMode = DialogMode.EDIT;
dialogTitle = "Edit expense";
newExpense = expenseTableView.getSelectionModel().getSelectedItem();
dialogController.setExpense(newExpense);
} else {
return;
}
dialog.setTitle(dialogTitle);
dialog.showAndWait();
// Show the Dialog and wait for the user to close it
//Get the newly created expense from the dialog pane
newExpense = dialogController.getNewExpense();
if (newExpense != null && dialogMode == DialogMode.ADD) {
expenseRegister.addItem(newExpense);
}
refreshTableView();
}
//Only add the expense to the tableview, if the expense is not null
@FXML
public void handleDeleteBtn(ActionEvent event) {
Expense chosenExpense = expenseTableView.getSelectionModel().getSelectedItem();
if (chosenExpense == null) {
return;
}
Optional<ButtonType> isConfirmed = showConfirmationDialog();
if (isConfirmed.isPresent() && isConfirmed.get() == ButtonType.OK) {
expenseRegister.removeItem(chosenExpense);
refreshTableView();
}
}
protected void refreshTableView() {
this.expenses.setAll(expenseRegister.getItems());
this.sum.setText(String.valueOf(expenseRegister.getTotalSum()));
}
private Optional<ButtonType> showConfirmationDialog() {
Alert alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle("Confirm Delete");
alert.setHeaderText("Delete Confirmation");
alert.setContentText("Are you sure you would like to delete the selected entry?");
return alert.showAndWait();
}
public ExpenseRegister loadExpenseDataFromFile(String fileName) throws IOException {
//ItemRegister<T extends Item>
FileHandling fileHandling = new FileHandling();
if (fileHandling.isEmpty(fileName)) {
expenseRegister = new ExpenseRegister();
} else {
try {
expenseRegister = fileHandling.readExpenseRegisterFromFile(fileName);
} catch (IOException e) {
e.printStackTrace();
}
}
return expenseRegister;
}
public void saveDataToFile(String fileName) throws IOException {
FileHandling fileHandling = new FileHandling();
fileHandling.writeItemRegisterToFile(expenseRegister, fileName);
}
@FXML
public void switchScene(ActionEvent event) throws IOException {
saveDataToFile("Expense");
FXMLLoader loader = new FXMLLoader();
if (event.getSource() == incomeBtn) {
loader.setLocation(SceneController.class.getResource("/view/Income.fxml"));
} else if (event.getSource() == returnBtn) {
loader.setLocation(SceneController.class.getResource("/view/FirstMenu.fxml"));
} else if (event.getSource() == budgetBtn) {
loader.setLocation(SceneController.class.getResource("/view/BudgetNew.fxml"));
}
Parent root = loader.load();
Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
}
package no.ntnu.idatt1002.demo.controller;
import java.io.IOException;
import java.util.Optional;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Dialog;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.text.Text;
import javafx.stage.Modality;
import javafx.stage.Stage;
import no.ntnu.idatt1002.demo.data.Economics.Income;
import no.ntnu.idatt1002.demo.data.Economics.IncomeCategory;
import no.ntnu.idatt1002.demo.data.Economics.IncomeRegister;
import no.ntnu.idatt1002.demo.data.Economics.FileHandling;
public class IncomeController {
/**
* The mode of the dialog. NEW if new contact, EDIT if edit existing contact.
*/
private DialogMode dialogMode;
@FXML
private Button addBtn;
@FXML
private Button editBtn;
@FXML
private Button deleteBtn;
@FXML
private ComboBox<String> show;
@FXML
private Button expenseBtn;
@FXML
private Button overviewBtn;
@FXML
private Button budgetBtn;
@FXML
private Button returnBtn;
@FXML
private Text sum;
@FXML
private TableColumn<Income, Double> amountColumn;
@FXML
private TableColumn<Income, IncomeCategory> categoryColumn;
@FXML
private TableColumn<Income, String> dateColumn;
@FXML
private TableColumn<Income, String> descriptionColumn;
@FXML
private TableColumn<Income, Boolean> recurringColumn;
@FXML
private TableView<Income> incomeTableView;
IncomeRegister incomeRegister;
ObservableList<Income> income;
ObservableList<String> filter;
@FXML
public void initialize() throws IOException {
dateColumn.setCellValueFactory(new PropertyValueFactory<Income, String>("date"));
amountColumn.setCellValueFactory(new PropertyValueFactory<Income, Double>("amount"));
categoryColumn.setCellValueFactory(new PropertyValueFactory<Income, IncomeCategory>("category"));
descriptionColumn.setCellValueFactory(new PropertyValueFactory<Income, String>("description"));
recurringColumn.setCellValueFactory(new PropertyValueFactory<Income, Boolean>("recurring"));
filter = FXCollections.observableArrayList("All", "Gift", "Salary", "Student loan", "Fixed income");
show.setItems(filter);
show.setValue("All");
incomeRegister = loadIncomeDataFromFile("Income");
income = FXCollections.observableArrayList(incomeRegister.getItems());
incomeTableView.setItems(income);
sum.setText(String.valueOf(incomeRegister.getTotalSum()));
//if budget.register isEmpty -> disable expense
}
@FXML
protected void handleAddButton(ActionEvent event) {
handleEditButton(event);
}
@FXML
protected void handleEditButton(ActionEvent event) {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/view/AddIncome.fxml"));
Income newIncome = null;
String dialogTitle = "";
// Load the FXML file for your dialog box
Dialog<Income> dialog = new Dialog<>();
dialog.initModality(Modality.APPLICATION_MODAL);
try {
// Set the Dialog's content to the loaded FXML file
dialog.getDialogPane().setContent(loader.load());
} catch (IOException e) {
e.printStackTrace();
}
// Get the controller for the loaded FXML file
AddIncomeController dialogController = loader.getController();
if (event.getSource().equals(addBtn)) {
dialogMode = DialogMode.ADD;
dialogTitle = "Add income";
} else if (event.getSource().equals(editBtn)
&& incomeTableView.getSelectionModel().getSelectedItem() != null) {
dialogMode = DialogMode.EDIT;
dialogTitle = "Edit income";
newIncome = incomeTableView.getSelectionModel().getSelectedItem();
dialogController.setIncome(newIncome);
} else {
return;
}
dialog.setTitle(dialogTitle);
// Show the Dialog and wait for the user to close it
dialog.showAndWait();
//Get the newly created income from the dialog pane
newIncome = dialogController.getNewIncome();
if (newIncome != null && dialogMode == DialogMode.ADD) {
incomeRegister.addItem(newIncome);
}
refreshTableView();
}
//Only add the income to the tableview, if the income is not null
@FXML
public void handleDeleteBtn(ActionEvent event) {
Income chosenIncome = incomeTableView.getSelectionModel().getSelectedItem();
if (chosenIncome == null) {
return;
}
Optional<ButtonType> isConfirmed = showConfirmationDialog();
if (isConfirmed.isPresent() && isConfirmed.get() == ButtonType.OK) {
incomeRegister.removeItem(chosenIncome);
refreshTableView();
}
}
protected void refreshTableView() {
this.income.setAll(incomeRegister.getItems());
this.sum.setText(String.valueOf(incomeRegister.getTotalSum()));
}
private Optional<ButtonType> showConfirmationDialog() {
Alert alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle("Confirm Delete");
alert.setHeaderText("Delete Confirmation");
alert.setContentText("Are you sure you would like to delete the selected entry?");
return alert.showAndWait();
}
public IncomeRegister loadIncomeDataFromFile(String fileName) throws IOException {
FileHandling fileHandling = new FileHandling();
if (fileHandling.isEmpty(fileName)) {
incomeRegister = new IncomeRegister();
System.out.println("hey");
} else {
try {
incomeRegister = fileHandling.readIncomeRegisterFromFile(fileName);
} catch (IOException e) {
e.printStackTrace();
}
}
return incomeRegister;
}
public void saveDataToFile(String fileName) throws IOException {
FileHandling fileHandling = new FileHandling();
fileHandling.writeItemRegisterToFile(incomeRegister, fileName);
}
@FXML
public void switchScene(ActionEvent event) throws IOException {
saveDataToFile("Income");
FXMLLoader loader = new FXMLLoader();
if (event.getSource() == expenseBtn) {
loader.setLocation(SceneController.class.getResource("/view/Expenses.fxml"));
} else if (event.getSource() == overviewBtn) {
loader.setLocation(SceneController.class.getResource("/view/Overview.fxml"));
} else if (event.getSource() == returnBtn) {
loader.setLocation(SceneController.class.getResource("/view/FirstMenu.fxml"));
} else if (event.getSource() == budgetBtn) {
loader.setLocation(SceneController.class.getResource("/view/BudgetNew.fxml"));
}
Parent root = loader.load();
Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
}
package no.ntnu.idatt1002.demo.controller;
import java.io.IOException;
import java.time.LocalDate;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
......@@ -11,18 +12,23 @@ import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.image.ImageView;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import no.ntnu.idatt1002.demo.data.Economics.ExpenseRegister;
import no.ntnu.idatt1002.demo.data.Economics.FileHandling;
import no.ntnu.idatt1002.demo.data.Economics.IncomeRegister;
public class MainMenuController {
@FXML
private Button addExpenseButton;
private Button addExpenseBtn;
@FXML
private Button foodButton;
@FXML
private Button overviewButton;
private Button overviewBtn;
@FXML
private ProgressBar progressbar;
......@@ -36,35 +42,55 @@ public class MainMenuController {
@FXML
private Label today;
@FXML
private Text budgetMonth;
//ExpenseRepository expenseRepository;
@FXML
public void initialize() {
progressbar.setProgress(0.5);
//progressbar.setProgress((ExpenseRepository.getSum())/5000);
System.out.println(progressbar.getProgress());
progressMarker.setTranslateX(-275 + progressbar.getProgress());
today.setTranslateX(-275 + progressbar.getProgress());
}
private Label balanceLbl;
@FXML
public void switchExpenses(ActionEvent event) throws IOException {
FXMLLoader loader = new FXMLLoader(SceneController.class.getResource("/view/Expenses.fxml"));
Parent root = loader.load();
Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
public void initialize() throws IOException {
IncomeController incomeController = new IncomeController();
IncomeRegister incomeRegister = incomeController.loadIncomeDataFromFile("Income");
ExpensesController expensesController = new ExpensesController();
ExpenseRegister expenseRegister = expensesController.loadExpenseDataFromFile("Expense");
double incomeSum = incomeRegister.getTotalSum();
double expenseSum = expenseRegister.getTotalSum();
progressbar.setProgress(expenseSum/incomeSum);
progressMarker.setTranslateX(-275 + progressbar.getProgress());
budgetMonth.setText("BUDGET " + (LocalDate.EPOCH.getMonth()));
double balance = incomeSum - expenseSum;
balanceLbl.setText("Balance: " + (balance));
today.setText("Used " + expenseSum + " out of " + incomeSum + " this month");
if (balance < 0) {
balanceLbl.setTextFill(Color.RED);
}
date.setValue(LocalDate.now());
//date.restrict
}
@FXML
public void switchOverview(ActionEvent event) throws IOException {
FXMLLoader loader = new FXMLLoader(SceneController.class.getResource("/view/Overview.fxml"));
public void switchScene(ActionEvent event) throws IOException {
//saveDataToFile("Income");
FXMLLoader loader = new FXMLLoader();
if (event.getSource() == addExpenseBtn) {
loader.setLocation(SceneController.class.getResource("/view/Expenses.fxml"));
} else if (event.getSource() == overviewBtn) {
loader.setLocation(SceneController.class.getResource("/view/Budget.fxml"));
}
Parent root = loader.load();
Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
}
......@@ -36,6 +36,23 @@ public class FileHandlingBudget {
}
}
/**
* Method for checking if a .register file is empty.
*
* @param fileTitle the name of the file you want to check
* @return true or false depending on if file is empty.
* @throws IOException if an input or output exception occurred.
*/
public boolean isEmpty(String fileTitle) throws IOException {
try (BufferedReader br = new BufferedReader(new FileReader(filePath + fileTitle + fileType))) {
if (br.readLine() == null) {
return true;
} else {
return false;
}
}
}
/**
* Method for reading (getting) a Budget from a file.
*
......@@ -74,10 +91,9 @@ public class FileHandlingBudget {
} else if (line.startsWith(FileHandlingBudget.budgetDescription)) {
budgetDescription = line.replace(FileHandlingBudget.budgetDescription,"");
}
if ((line.isEmpty() || (nextLine == null)) && (expenseCategory!=null)) {
if (line.isEmpty() || (nextLine == null)) {
if(generalBudget == null){
generalBudget = new GeneralBudget(budgetPeriod,maxAmount);
generalBudget.addToBudget(budgetAmount,budgetDescription,expenseCategory);
} else{
generalBudget.addToBudget(budgetAmount,budgetDescription,expenseCategory);
}
......
......@@ -45,6 +45,9 @@ public class Expense extends Item{
this.category = new SimpleObjectProperty<>(category);
}
public ObjectProperty<ExpenseCategory> expenseCategoryObjectProperty() {
return this.category;
}
/**
* The method returns the category to which the Expense belongs as a value of the ExpenseCategory enum class.
* @return The category the Expense belongs to as a value of the ExpenseCategory enum class.
......
......@@ -92,8 +92,7 @@ public class FileHandling{
case "STUDENT_LOAN" -> IncomeCategory.STUDENT_LOAN;
default -> IncomeCategory.SALARY;
};
}
if(line.isEmpty() || (nextLine == null)) {
} if(line.isEmpty() || (nextLine == null)) {
if(description.equals("")){
incomeRegister.addItem(new Income(amount, reoccuring, incomeCategory, date));
} else{
......
......@@ -53,6 +53,9 @@ public class Income extends Item{
}
public ObjectProperty<IncomeCategory> incomeCategoryObjectProperty() {
return this.category;
}
/**
* The method returns the category to which the Income belongs as a value of the IncomeCategory enum.
* @return The category to which the Income belongs to as a value of the IncomeCategory enum.
......
......@@ -112,14 +112,14 @@ public abstract class Item {
this.recurring.set(recurring);
}
public StringProperty dateProperty() {
return this.dateProperty();
}
/**
public StringProperty dateProperty() {
* The method returns the ObjectProperty of date.
* @return
*/
public ObjectProperty<LocalDate> dateProperty() {
return this.date;
}
*/
/**
* The method returns the date of the item object (income/expense).
* @return The date of the transaction.
......
budgetPeriod=31
maxAmount=1000.0
budgetAmount=500.0
budgetCategory=FOOD
budgetDescription=dd
date=2023-03-01
description=twelve
amount=12.0
isRecurring=Not recurring
category=CLOTHES
date=2023-03-11
description=1111
amount=121.0
isRecurring=Not recurring
category=OTHER
date=2023-03-26
amount=10.0
isRecurring=Not recurring
category=FOOD
date=2023-03-24
description=studie
amount=1000.0
isRecurring=Recurring
category=STUDENT_LOAN
date=2023-03-25
amount=100.0
isRecurring=Not recurring
category=GIFT
date=2023-03-25
description=airbnb
amount=1000.0
isRecurring=Not recurring
category=GIFT
date=03.03.23
description=description
amount=59.900001525878906
isReoccuring=Not reoccurring
category=CLOTHES
date=03.03.23
description=description
amount=59.900001525878906
isReoccuring=Not reoccurring
category=GIFT
src/main/resources/Images/add_image 2.png

3.61 KiB

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