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

"Removed everything that has to do with income, and moved showAndWait into if...

"Removed everything that has to do with income, and moved showAndWait into if else, to prevent edit popup to appear even if nothing is selected in tableview"
parent 307754d3
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
......@@ -35,18 +35,12 @@ enum DialogMode {
ADD, EDIT, DELETE
}
enum ItemMode {
INCOME, EXPENSE
}
public class ExpensesController {
/**
* The mode of the dialog. NEW if new contact, EDIT if edit existing contact.
*/
private DialogMode dialogMode;
private ItemMode itemMode;
@FXML
private Button addBtn;
@FXML
......@@ -64,6 +58,9 @@ public class ExpensesController {
@FXML
private Button overviewBtn;
@FXML
private Button budgetBtn;
@FXML
private Button returnBtn;
@FXML
......@@ -92,31 +89,22 @@ public class ExpensesController {
ObservableList<String> filter;
//ItemRegister itemRegister;
IncomeRegister incomeRegister;
ObservableList<Income> income;
@FXML
public void initialize() throws IOException { //TODO SAME REGISTER FOR BOTH, BUT LOAD DIFFERENT DATA DEPENDING ON WHICH IT IS
public void initialize()
throws IOException { //TODO SAME REGISTER FOR BOTH, BUT LOAD DIFFERENT DATA DEPENDING ON WHICH IT IS
dateColumn.setCellValueFactory(new PropertyValueFactory<Expense, String>("date"));
amountColumn.setCellValueFactory(new PropertyValueFactory<Expense, Double>("amount"));
categoryColumn.setCellValueFactory(new PropertyValueFactory<Expense, ExpenseCategory>("category"));
categoryColumn.setCellValueFactory(
new PropertyValueFactory<Expense, ExpenseCategory>("category"));
descriptionColumn.setCellValueFactory(new PropertyValueFactory<Expense, String>("description"));
recurringColumn.setCellValueFactory(new PropertyValueFactory<Expense, Boolean>("recurring"));
if (expenseTableView != null) {
itemMode = ItemMode.EXPENSE;
filter = FXCollections.observableArrayList("All", "Food", "Clothes", "Books", "Other", "Fixed expense");
expenseRegister = loadExpenseDataFromFile("Expense");
expenses = FXCollections.observableArrayList(expenseRegister.getItems());
expenseTableView.setItems(expenses);
} else {
itemMode = ItemMode.INCOME;
filter = FXCollections.observableArrayList("All", "Salary", "Student loan", "Gift", "Fixed expense");
incomeRegister = loadIncomeDataFromFile("Income");
income = FXCollections.observableArrayList(incomeRegister.getItems());
incomeTableView.setItems(income);
}
filter = FXCollections.observableArrayList("All", "Food", "Clothes", "Books", "Other",
"Fixed expense");
expenseRegister = loadExpenseDataFromFile("Expense");
expenses = FXCollections.observableArrayList(expenseRegister.getItems());
expenseTableView.setItems(expenses);
show.setItems(filter);
show.setValue("All");
}
......@@ -125,16 +113,12 @@ public class ExpensesController {
protected void handleAddButton(ActionEvent event) {
handleEditButton(event);
}
@FXML
protected void handleEditButton(ActionEvent event) {
FXMLLoader loader = new FXMLLoader();
if (itemMode == ItemMode.EXPENSE) {
loader.setLocation(getClass().getResource("/view/AddExpense.fxml"));
} else {
loader.setLocation(getClass().getResource("/view/AddIncome.fxml"));
}
loader.setLocation(getClass().getResource("/view/AddExpense.fxml"));
Expense newExpense = null;
Income newIncome = null;
String dialogTitle = "";
// Load the FXML file for your dialog box
Dialog<Expense> dialog = new Dialog<>();
......@@ -149,91 +133,51 @@ public class ExpensesController {
// Get the controller for the loaded FXML file
AddExpenseController dialogController = loader.getController();
AddIncomeController dialogController1 = loader.getController();
if (itemMode == ItemMode.EXPENSE) {
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;
}
} else {
if (event.getSource().equals(addBtn)) {
dialogMode = DialogMode.ADD;
dialogTitle = "Income";
} else if (event.getSource().equals(editBtn) && incomeTableView.getSelectionModel().getSelectedItem() != null) {
dialogMode = DialogMode.EDIT;
dialogTitle = "Edit income";
newIncome = incomeTableView.getSelectionModel().getSelectedItem();
dialogController1.setIncome(newIncome); //TODO NEED TO FXI THIS TO INCOME
} else {
return;
}
if (event.getSource().equals(addBtn)) {
dialogMode = DialogMode.ADD;
dialogTitle = "Add expense";
dialog.setTitle(dialogTitle);
dialog.showAndWait();
} else if (event.getSource().equals(editBtn)
&& expenseTableView.getSelectionModel().getSelectedItem() != null) {
dialogMode = DialogMode.EDIT;
dialogTitle = "Edit expense";
newExpense = expenseTableView.getSelectionModel().getSelectedItem();
dialogController.setExpense(newExpense);
dialog.setTitle(dialogTitle);
dialog.showAndWait();
}
dialog.setTitle(dialogTitle);
// Show the Dialog and wait for the user to close it
dialog.showAndWait();
//Get the newly created expense from the dialog pane
newExpense = dialogController.getNewExpense();
if (itemMode == ItemMode.EXPENSE) {
newExpense = dialogController.getNewExpense();
if (newExpense != null && dialogMode == DialogMode.ADD) {
expenseRegister.addItem(newExpense);
refreshObservableList();
}
} else {
newIncome = dialogController1.getNewIncome(); //TODO NEED TO FIX to newIncome
if (newIncome != null && dialogMode == DialogMode.ADD) { //TODO NEED TO FIX TO NEW INCOME
incomeRegister.addItem(newIncome); //TODO NEED TO FIX TO NEW INCOME
refreshObservableList();
}
if (newExpense != null && dialogMode == DialogMode.ADD) {
expenseRegister.addItem(newExpense);
refreshObservableList();
}
//Only add the expense to the tableview, if the expense is not null
}
//Only add the expense to the tableview, if the expense is not null
@FXML
public void handleDeleteBtn(ActionEvent event) {
if (itemMode == ItemMode.EXPENSE) {
Expense chosenExpense = expenseTableView.getSelectionModel().getSelectedItem();
if (chosenExpense == null) {
return;
}
Optional<ButtonType> isConfirmed = showConfirmationDialog();
if (isConfirmed.isPresent() && isConfirmed.get() == ButtonType.OK) {
expenseRegister.removeItem(chosenExpense);
refreshObservableList();
}
} else {
Income chosenIncome = incomeTableView.getSelectionModel().getSelectedItem();
if (chosenIncome == null) {
return;
}
Optional<ButtonType> isConfirmed = showConfirmationDialog();
if (isConfirmed.isPresent() && isConfirmed.get() == ButtonType.OK) {
incomeRegister.removeItem(chosenIncome);
refreshObservableList();
}
Expense chosenExpense = expenseTableView.getSelectionModel().getSelectedItem();
if (chosenExpense == null) {
return;
}
Optional<ButtonType> isConfirmed = showConfirmationDialog();
if (isConfirmed.isPresent() && isConfirmed.get() == ButtonType.OK) {
expenseRegister.removeItem(chosenExpense);
refreshObservableList();
}
}
protected void refreshObservableList() {
if (itemMode == ItemMode.EXPENSE) {
this.expenses.setAll(expenseRegister.getItems());
} else {
this.income.setAll(incomeRegister.getItems());
}
this.expenses.setAll(expenseRegister.getItems());
}
private Optional<ButtonType> showConfirmationDialog() {
Alert alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle("Confirm Delete");
......@@ -248,49 +192,24 @@ public class ExpensesController {
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 IncomeRegister loadIncomeDataFromFile(String fileName) throws IOException {
FileHandling fileHandling = new FileHandling();
if (fileHandling.isEmpty(fileName)) {
incomeRegister = new IncomeRegister();
} else {
try {
incomeRegister = fileHandling.readIncomeRegisterFromFile(fileName);
expenseRegister = fileHandling.readExpenseRegisterFromFile(fileName);
} catch (IOException e) {
e.printStackTrace();
}
}
return incomeRegister;
return expenseRegister;
}
public void saveDataToFile(String fileName) throws IOException {
FileHandling fileHandling = new FileHandling();
if (itemMode == ItemMode.EXPENSE) {
fileHandling.writeItemRegisterToFile(expenseRegister, fileName); }
else {
fileHandling.writeItemRegisterToFile(incomeRegister, fileName); }
fileHandling.writeItemRegisterToFile(expenseRegister, fileName);
}
@FXML
public void switchScene(ActionEvent event) throws IOException {
if (itemMode == ItemMode.EXPENSE) {
saveDataToFile("Expense");
}
else {
saveDataToFile("Income");
}
saveDataToFile("Expense");
FXMLLoader loader = new FXMLLoader();
if (event.getSource() == incomeBtn) {
loader.setLocation(SceneController.class.getResource("/view/Income.fxml"));
......@@ -298,13 +217,14 @@ public class ExpensesController {
loader.setLocation(SceneController.class.getResource("/view/Overview.fxml"));
} else if (event.getSource() == returnBtn) {
loader.setLocation(SceneController.class.getResource("/view/FirstMenu.fxml"));
}
Parent root = loader.load();
Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
} else if (event.getSource() == budgetBtn) {
loader.setLocation(SceneController.class.getResource("/view/underProgress.fxml"));
Parent root = loader.load();
Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
}
}
\ No newline at end of file
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