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

Override inherited methods + update switchScene to have several possible destinations

parent f5078b13
No related branches found
No related tags found
1 merge request!43Merging frontend-testing into master
......@@ -47,7 +47,7 @@ import no.ntnu.idatt1002.demo.data.Economics.IncomeRegister;
* @author Harry Linrui Xu
* @since 30.03.2023
*/
public class IncomeExpenseController implements FinanceController {
public class IncomeExpenseController extends FinanceController {
private final static String sumText = "Sum: ";
@FXML
private TableColumn<Expense, Double> expAmountCol;
......@@ -97,9 +97,6 @@ public class IncomeExpenseController implements FinanceController {
@FXML
private MenuItem addIncome;
@FXML
private ProgressBar budgetProgress;
@FXML
private DatePicker date;
......@@ -112,6 +109,9 @@ public class IncomeExpenseController implements FinanceController {
@FXML
private Button returnBtn;
@FXML
private Button continueBtn;
@FXML
private Label title;
......@@ -152,33 +152,28 @@ public class IncomeExpenseController implements FinanceController {
//Initialize columns
setColumns();
//Initialize registers and tableview
incomeRegister = loadIncomeDataFromFile("Income");
income = FXCollections.observableArrayList(incomeRegister.getItems());
incomeTableView.setItems(income);
//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);
//Setting pie chart values to correspond with the registers
incomePieChart.setLegendSide(Side.RIGHT);
expensePieChart.setLegendSide(Side.RIGHT);
expensePieChart.setLabelLineLength(10);
expenseRegister = loadExpenseDataFromFile("Expense");
expenses = FXCollections.observableArrayList(expenseRegister.getItems());
expenseTableView.setItems(expenses);
refreshPieCharts();
//Setting pie chart values to correspond with the registers
incomePieChart.setLegendSide(Side.RIGHT);
incomePieChart.setLabelLineLength(10);
refreshPieCharts();
refreshProgress();
expensePieChart.setLegendSide(Side.RIGHT);
expensePieChart.setLabelLineLength(10);
refreshPieChart();
formatDatePicker();
//Initialize sum field under the tableview
inSum.setText(sumText + String.valueOf(incomeRegister.getTotalSum()));
expSum.setText(sumText + String.valueOf(expenseRegister.getTotalSum()));
}
private void setColumns() {
......@@ -195,7 +190,7 @@ public class IncomeExpenseController implements FinanceController {
expRecurringCol.setCellValueFactory(new PropertyValueFactory<>("recurring"));
}
private ObservableList<PieChart.Data> createExpensePieChart() {
public ObservableList<PieChart.Data> createExpensePieChart() {
return FXCollections.observableArrayList(
new Data("Food", expenseRegister.getExpenseByCategory(ExpenseCategory.FOOD).getTotalSum()),
new Data("Books", expenseRegister.getExpenseByCategory(ExpenseCategory.BOOKS).getTotalSum()),
......@@ -215,7 +210,8 @@ public class IncomeExpenseController implements FinanceController {
/**
* Method for disabling the date picker, yet having its opacity at max.
*/
private void formatDatePicker() {
@Override
public void formatDatePicker() {
date.setValue(LocalDate.now());
date.setDisable(true);
date.setStyle("-fx-opacity: 1");
......@@ -225,6 +221,7 @@ public class IncomeExpenseController implements FinanceController {
* Method for handling the adding of new entries in the tableview.
* @param event A button click on the add button.
*/
@Override
public void handleAddBtn(javafx.event.ActionEvent event) {
int sizeBf = (expenseRegister.getItems().size() + incomeRegister.getItems().size());
......@@ -239,7 +236,6 @@ public class IncomeExpenseController implements FinanceController {
if (sizeAf != sizeBf) {
refreshTableView();
refreshProgress();
}
}
......@@ -274,7 +270,6 @@ public class IncomeExpenseController implements FinanceController {
//Updates the tableview and pie chart using the register
refreshTableView();
refreshProgress();
}
/**
......@@ -302,15 +297,12 @@ public class IncomeExpenseController implements FinanceController {
this.expSum.setText("Sum: " + String.valueOf(expenseRegister.getTotalSum()));
}
private void refreshPieCharts() {
@Override
public void refreshPieChart() {
this.incomePieChart.setData(createIncomePieChart());
this.expensePieChart.setData(createExpensePieChart());
}
private void refreshProgress() {
budgetProgress.setProgress(expenseRegister.getTotalSum()/incomeRegister.getTotalSum());
}
@FXML
private void handleAddIncome() {
//Instantiate FXML loader and loads the popup for adding income
......@@ -355,7 +347,6 @@ public class IncomeExpenseController implements FinanceController {
Expense newExpense;
String dialogTitle = "Add expense";
// Load the FXML file for your dialog box
Dialog<Expense> dialog = new Dialog<>();
dialog.initModality(Modality.APPLICATION_MODAL);
......@@ -471,34 +462,6 @@ public class IncomeExpenseController implements FinanceController {
}
this.expensePieChart.setData(createExpensePieChart());
}
/**
* 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(String title, String header, String content) {
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();
}
/**
* Displays an alert box of type error, informing of a custom error.
*/
private void showErrorDialogBox(String title, String header, String content) {
Alert alert = new Alert(AlertType.ERROR);
alert.setTitle(title);
alert.setHeaderText(header);
alert.setContentText(content);
alert.showAndWait();
}
/**
* Saves the changes made to the tableview by writing the information to a file.
......@@ -510,67 +473,6 @@ public class IncomeExpenseController implements FinanceController {
FileHandling.writeItemRegisterToFile(expenseRegister, "Expense");
}
/**
* 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.
*/
public IncomeRegister loadIncomeDataFromFile(String fileName) {
//Instantiate new incomeRegister
try {
if (FileHandling.isEmpty(fileName)) {
incomeRegister = new IncomeRegister();
} else { //Load previous income register
incomeRegister = FileHandling.readIncomeRegisterFromFile(fileName);
}
} catch (IOException ioe) {
showErrorDialogBox("File reading error", "Error in reading from file", "Could not"
+ "read the IncomeRegister from file");
}
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.
*/
public ExpenseRegister loadExpenseDataFromFile(String fileName) {
//ItemRegister<T extends Item>
try {
if (FileHandling.isEmpty(fileName)) {
expenseRegister = new ExpenseRegister();
} else { //Load previous income register
expenseRegister = FileHandling.readExpenseRegisterFromFile(fileName);
}
} catch (IOException ioe) {
showErrorDialogBox("File reading error", "Error in reading from file", "Could not"
+ "read the ExpenseRegister from file");
}
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.
*/
public GeneralBudget loadBudgetDataFromFile(String fileName) {
//Instantiate new budget
try {
if (FileHandlingBudget.isEmpty(fileName)) {
generalBudget = new GeneralBudget(1000);
} else { //Load previous income register
generalBudget = FileHandlingBudget.readGeneralBudgetFromFile(fileName);
}
} catch (IOException ioe) {
showErrorDialogBox("File reading error", "Error in reading from file", "Could not"
+ "read the GeneralBudget from file");
}
return generalBudget;
}
/**
* Switches scenes back to main menu, by loading a new FXML file and setting the scene to this location.
* @param event A button click on the return to main menu button
......@@ -580,17 +482,21 @@ public class IncomeExpenseController implements FinanceController {
try {
saveDataToFile();
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/view/MainMenuNew.fxml"));
if (event.getSource() == returnBtn) {
loader.setLocation(getClass().getResource("/view/FirstMenu.fxml"));
} else if (event.getSource() == continueBtn) {
loader.setLocation(getClass().getResource("/view/newBudgetBudgert.fxml"));
} else if (event.getSource() == returnBtn) {
//wipe the whole direcotyr with in the currentFile.txt
}
Parent root = loader.load();
Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
} catch(IOException ioe) {
showErrorDialogBox("Loading error", "Error in loading", "Could not save"
showErrorDialogBox("Loading error", "Error in loading", "Could not save"
+ "to 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