Newer
Older
Harry Linrui XU
committed
package no.ntnu.idatt1002.demo.controller;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.util.Optional;
Harry Linrui XU
committed
import javafx.beans.binding.Bindings;
Harry Linrui XU
committed
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
Harry Linrui XU
committed
import javafx.fxml.FXMLLoader;
import javafx.geometry.Side;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.chart.PieChart;
import javafx.scene.chart.PieChart.Data;
Harry Linrui XU
committed
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.ComboBox;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.text.Text;
Harry Linrui XU
committed
import javafx.stage.Stage;
Harry Linrui XU
committed
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import no.ntnu.idatt1002.demo.data.Budget.FileHandlingBudget;
import no.ntnu.idatt1002.demo.data.Budget.GeneralBudget;
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.IncomeCategory;
import no.ntnu.idatt1002.demo.data.Economics.IncomeRegister;
public class IncomeExpenseController implements FinanceController {
@FXML
private TableColumn<Expense, Double> expAmountCol;
@FXML
private TableColumn<Expense, ExpenseCategory> expCategoryCol;
@FXML
private TableColumn<Expense, String> expDateCol;
@FXML
private TableColumn<Expense, String> expDescriptionCol;
@FXML
private TableColumn<Expense, Boolean> expRecurringCol;
@FXML
private TableColumn<Income, Double> inAmountCol;
@FXML
private TableColumn<Income, IncomeCategory> inCategoryCol;
@FXML
private TableColumn<Income, String> inDateCol;
@FXML
private TableColumn<Income, String> inDescriptionCol;
@FXML
private TableColumn<Income, Boolean> inRecurringCol;
@FXML
private TableView<Expense> expenseTableView;
@FXML
private TableView<Income> incomeTableView;
@FXML
private Text inSum;
@FXML
private Text expSum;
@FXML
private Button addBtn;
@FXML
private Button deleteBtn;
@FXML
private Button editBtn;
@FXML
private ProgressBar budgetProgress;
@FXML
private DatePicker date;
@FXML
private Label daysLeftLbl;
@FXML
private ComboBox<?> filter;
@FXML
private Button returnBtn;
@FXML
private Label title;
private FinanceMode financeMode;
private IncomeRegister incomeRegister;
private ExpenseRegister expenseRegister;
private GeneralBudget generalBudget;
private ObservableList<Income> income;
private ObservableList<Expense> expenses;
Harry Linrui XU
committed
@FXML
private PieChart expensePieChart;
@FXML
private PieChart incomePieChart;
Harry Linrui XU
committed
FileHandling fileHandling;
@FXML
public void initialize() throws IOException {
Harry Linrui XU
committed
fileHandling = new FileHandling();
Harry Linrui XU
committed
//Initialize columns
Harry Linrui XU
committed
setColumns();
//Initialize registers and tableview
incomeRegister = loadIncomeDataFromFile("Income");
income = FXCollections.observableArrayList(incomeRegister.getItems());
incomeTableView.setItems(income);
expenseRegister = loadExpenseDataFromFile("Expense");
expenses = FXCollections.observableArrayList(expenseRegister.getItems());
expenseTableView.setItems(expenses);
incomePieChart.getData().addAll(createIncomePyChart());
Harry Linrui XU
committed
incomePieChart.setTitle("Income");
incomePieChart.setLegendSide(Side.RIGHT);
Harry Linrui XU
committed
Harry Linrui XU
committed
expensePieChart.setData(createExpensePyChart());
Harry Linrui XU
committed
expensePieChart.setTitle("Expenses");
Harry Linrui XU
committed
expensePieChart.setLegendSide(Side.RIGHT);
Harry Linrui XU
committed
expensePieChart.setLabelLineLength(10);
//Initialize sum field under the tableview
// inSum.setText(String.valueOf(incomeRegister.getTotalSum()));
//expSum.setText(String.valueOf(expenseRegister.getTotalSum()));
}
private void setColumns() {
Harry Linrui XU
committed
inDateCol.setCellValueFactory(new PropertyValueFactory<Income, String>("date"));
inAmountCol.setCellValueFactory(new PropertyValueFactory<Income, Double>("amount"));
inCategoryCol.setCellValueFactory(new PropertyValueFactory<Income, IncomeCategory>("category"));
inDescriptionCol.setCellValueFactory(new PropertyValueFactory<Income, String>("description"));
inRecurringCol.setCellValueFactory(new PropertyValueFactory<Income, Boolean>("recurring"));
expDateCol.setCellValueFactory(new PropertyValueFactory<Expense, String>("date"));
expAmountCol.setCellValueFactory(new PropertyValueFactory<Expense, Double>("amount"));
expCategoryCol.setCellValueFactory(new PropertyValueFactory<Expense, ExpenseCategory>("category"));
expDescriptionCol.setCellValueFactory(new PropertyValueFactory<Expense, String>("description"));
expRecurringCol.setCellValueFactory(new PropertyValueFactory<Expense, Boolean>("recurring"));
Harry Linrui XU
committed
}
Harry Linrui XU
committed
Harry Linrui XU
committed
private ObservableList<PieChart.Data> createExpensePyChart() {
Harry Linrui XU
committed
return FXCollections.observableArrayList(
new Data("Food", expenseRegister.getExpenseByCategory(ExpenseCategory.FOOD).getTotalSum()),
new Data("Books", expenseRegister.getExpenseByCategory(ExpenseCategory.BOOKS).getTotalSum()),
new Data("Clothes", expenseRegister.getExpenseByCategory(ExpenseCategory.CLOTHES).getTotalSum()),
new Data("Other", expenseRegister.getExpenseByCategory(ExpenseCategory.OTHER).getTotalSum())
);
Harry Linrui XU
committed
}
Harry Linrui XU
committed
Harry Linrui XU
committed
private ObservableList<PieChart.Data> createIncomePyChart() {
Harry Linrui XU
committed
return FXCollections.observableArrayList(
new Data("Food", incomeRegister.getIncomeByCategory(IncomeCategory.STUDENT_LOAN).getTotalSum()),
new Data("Books", incomeRegister.getIncomeByCategory(IncomeCategory.SALARY).getTotalSum())
);
Harry Linrui XU
committed
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
}
/**
* Method for handling the adding of new entries in the tableview.
* @param event A button click on the add button.
*/
@Override
public void handleAddBtn(ActionEvent event) {
handleEditBtn(event);
}
/**
* Method for handling the editing of a chosen entry in the tableview.
*
* @param event A button click on the edit button.
*/
@Override
public void handleEditBtn(ActionEvent event) {
}
/**
* Deletes an entry from the tableview, if an entry has been selected. The method brings up a
* popup window, asking for confirmation for deleting the entry.
*
* @param event A button click on the delete button
*/
@Override
public void handleDeleteBtn(ActionEvent event) {
}
/**
* Method for synching the register with the tableview. The observable list to which the tableview
* is set, is being refilled with all the entries in the register, keeping it updated with new
* changes.
*/
@Override
public void refreshTableView() {
Harry Linrui XU
committed
this.income.setAll(incomeRegister.getItems());
//this.sum.setText(String.valueOf(incomeRegister.getTotalSum()));
Harry Linrui XU
committed
Harry Linrui XU
committed
this.expenses.setAll(expenseRegister.getItems());
//this.sum.setText(String.valueOf(incomeRegister.getTotalSum()));
Harry Linrui XU
committed
}
/**
* Returns an optional, which is a popup alert box, asking for confirmation for deleting an
* entry.
*
* @return An alert box, asking for confirmation for deleting the selected entry of the tableview.
*/
@Override
public Optional<ButtonType> showConfirmationDialog() {
return Optional.empty();
}
/**
* Saves the changes made to the tableview by writing the information to a file.
* @throws IOException If an error occurs while writing to the file.
*/
@Override
Harry Linrui XU
committed
public void saveDataToFile() throws IOException {
fileHandling.writeItemRegisterToFile(incomeRegister, "Income");
fileHandling.writeItemRegisterToFile(expenseRegister, "Expense");
Harry Linrui XU
committed
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
}
/**
* Method that either reads data from a file with which it fills an income register, if older changes exist, or instantiates an income register if the file is empty.
* @param fileName The name of the file that is being read from.
* @return An object of type IncomeRegister.
* @throws IOException If an error occurs while reading from the file.
*/
public IncomeRegister loadIncomeDataFromFile(String fileName) throws IOException {
//Instantiate new incomeRegister
if (fileHandling.isEmpty(fileName)) {
incomeRegister = new IncomeRegister();
} else { //Load previous income register
try {
incomeRegister = fileHandling.readIncomeRegisterFromFile(fileName);
} catch (IOException e) {
e.printStackTrace();
}
}
return incomeRegister;
}
/**
* Method that either reads data from a file with which it fills an expense register, if older changes exist, or instantiates an expense register if the file is empty.
* @param fileName The name of the file that is being read from.
* @return An object of type IncomeRegister.
* @throws IOException If an error occurs while reading from the file.
*/
public ExpenseRegister loadExpenseDataFromFile(String fileName) throws IOException {
//ItemRegister<T extends Item>
if (fileHandling.isEmpty(fileName)) {
expenseRegister = new ExpenseRegister();
} else {
try {
expenseRegister = fileHandling.readExpenseRegisterFromFile(fileName);
} catch (IOException e) {
e.printStackTrace();
}
}
return expenseRegister;
}
/**
* Method that either reads data from a file with which it fills a budget register, if this is an old budget, or instantiates a budget register if this is a new budget.
* @param fileName The name of the file that is being read from.
* @return An object of type GeneralBudget.
* @throws IOException If an error occurs while reading from the file.
*/
public GeneralBudget loadBudgetDataFromFile(String fileName) throws IOException {
FileHandlingBudget fileHandlingBudget = new FileHandlingBudget();
//Instantiate new budget
if (fileHandlingBudget.isEmpty(fileName)) {
generalBudget = new GeneralBudget(31, 1000);
} else { //Load previous budget
try {
generalBudget = fileHandlingBudget.readGeneralBudgetFromFile(fileName);
} catch (IOException e) {
e.printStackTrace();
}
}
return generalBudget;
}
/**
* Switches the scene to the Main Menu scene.
*/
Harry Linrui XU
committed
@FXML
private void returnToMainMenu(javafx.event.ActionEvent event) throws IOException {
Harry Linrui XU
committed
saveDataToFile();
Harry Linrui XU
committed
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/view/MainMenuNew.fxml"));
Parent root = loader.load();
Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();