From 23a80ae2c9e09c6debc034de38371352bc84046b Mon Sep 17 00:00:00 2001
From: Harry Linrui XU <xulr0820@hotmail.com>
Date: Fri, 24 Mar 2023 10:38:07 +0100
Subject: [PATCH] Created controller for income

---
 .../demo/controller/IncomeController.java     | 220 ++++++++++++++++++
 src/main/resources/view/Income.fxml           |  33 +--
 2 files changed, 239 insertions(+), 14 deletions(-)
 create mode 100644 src/main/java/no/ntnu/idatt1002/demo/controller/IncomeController.java

diff --git a/src/main/java/no/ntnu/idatt1002/demo/controller/IncomeController.java b/src/main/java/no/ntnu/idatt1002/demo/controller/IncomeController.java
new file mode 100644
index 00000000..3bfd4aaa
--- /dev/null
+++ b/src/main/java/no/ntnu/idatt1002/demo/controller/IncomeController.java
@@ -0,0 +1,220 @@
+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.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;
+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;
+
+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 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 { //TODO SAME REGISTER FOR BOTH, BUT LOAD DIFFERENT DATA DEPENDING ON WHICH IT IS
+    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");
+    incomeRegister = loadIncomeDataFromFile("Income");
+    income = FXCollections.observableArrayList(incomeRegister.getItems());
+    incomeTableView.setItems(income);
+
+    show.setItems(filter);
+    show.setValue("All");
+  }
+
+  @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);
+    }
+
+    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);
+      refreshObservableList();
+    }
+
+  }
+  //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);
+      refreshObservableList();
+    }
+  }
+
+  protected void refreshObservableList() {
+    this.income.setAll(incomeRegister.getItems());
+  }
+
+  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 {
+    //ItemRegister<T extends Item>
+    FileHandling fileHandling = new FileHandling();
+    if (fileHandling.isEmpty(fileName)) {
+      incomeRegister = new IncomeRegister();
+    } 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/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
diff --git a/src/main/resources/view/Income.fxml b/src/main/resources/view/Income.fxml
index d7ae545f..090983df 100644
--- a/src/main/resources/view/Income.fxml
+++ b/src/main/resources/view/Income.fxml
@@ -33,7 +33,7 @@
          <top>
             <HBox BorderPane.alignment="CENTER">
                <children>
-                  <Button fx:id="returnBtn" mnemonicParsing="false" onAction="#switchScene" text="Return ">
+                  <Button mnemonicParsing="false" onAction="#switchStartMenu" text="Return ">
                      <opaqueInsets>
                         <Insets left="100.0" />
                      </opaqueInsets>
@@ -73,7 +73,7 @@
                <children>
                   <HBox alignment="BOTTOM_LEFT" prefWidth="410.0" spacing="5.0">
                      <children>
-                        <Button fx:id="addBtn" alignment="TOP_CENTER" mnemonicParsing="false" onAction="#handleAddButton" text="Add" textAlignment="CENTER">
+                        <Button fx:id="add" alignment="TOP_CENTER" mnemonicParsing="false" onAction="#addIncome" text="Add" textAlignment="CENTER">
                            <graphic>
                               <ImageView fitHeight="19.0" fitWidth="16.0" pickOnBounds="true" preserveRatio="true">
                                  <image>
@@ -82,7 +82,7 @@
                               </ImageView>
                            </graphic>
                         </Button>
-                        <Button fx:id="editBtn" alignment="TOP_CENTER" mnemonicParsing="false" onAction="#handleEditButton" text="Edit" textAlignment="CENTER">
+                        <Button alignment="TOP_CENTER" mnemonicParsing="false" text="Edit" textAlignment="CENTER">
                            <graphic>
                               <ImageView fitHeight="19.0" fitWidth="16.0" pickOnBounds="true" preserveRatio="true">
                                  <image>
@@ -91,7 +91,7 @@
                               </ImageView>
                            </graphic>
                         </Button>
-                        <Button fx:id="deleteBtn" alignment="TOP_CENTER" mnemonicParsing="false" onAction="#handleDeleteBtn" text="Delete" textAlignment="CENTER">
+                        <Button alignment="TOP_CENTER" mnemonicParsing="false" text="Delete" textAlignment="CENTER">
                            <graphic>
                               <ImageView fitHeight="19.0" fitWidth="16.0" pickOnBounds="true" preserveRatio="true">
                                  <image>
@@ -122,10 +122,15 @@
                   </VBox>
                   <HBox prefHeight="100.0" prefWidth="200.0" GridPane.columnSpan="2" GridPane.rowIndex="2">
                      <children>
-                        <Button disable="true" mnemonicParsing="false" text="Income" />
-                        <Button fx:id="budgetBtn" mnemonicParsing="false" onAction="#switchScene" text="Budget" />
-                        <Button mnemonicParsing="false" text="Expenses" />
-                        <Button mnemonicParsing="false" onAction="#switchScene" text="Next">
+                        <Button mnemonicParsing="false" onAction="#switchOverview" text="Overview">
+                           <HBox.margin>
+                              <Insets right="5.0" />
+                           </HBox.margin>
+                        </Button>
+                        <Button disable="true" mnemonicParsing="false" onAction="#switchIncome" text="Income" />
+                        <Button mnemonicParsing="false" onAction="#switchExpenses" text="Expenses" />
+                        <Button disable="true" mnemonicParsing="false" text="Savings" />
+                        <Button mnemonicParsing="false" onAction="#switchExpenses" text="Next">
                            <HBox.margin>
                               <Insets left="170.0" />
                            </HBox.margin>
@@ -135,13 +140,13 @@
                         <Insets top="10.0" />
                      </padding>
                   </HBox>
-                  <TableView fx:id="incomeTableView" prefHeight="260.0" prefWidth="485.0" GridPane.columnSpan="2" GridPane.rowIndex="1">
+                  <TableView fx:id="expenseTableView" prefHeight="260.0" prefWidth="485.0" GridPane.columnSpan="2" GridPane.rowIndex="1">
                      <columns>
-                        <TableColumn fx:id="dateColumn" prefWidth="75.0" text="Date" />
-                        <TableColumn fx:id="amountColumn" prefWidth="75.0" text="Amount" />
-                        <TableColumn fx:id="categoryColumn" prefWidth="75.0" text="Category" />
-                        <TableColumn fx:id="descriptionColumn" prefWidth="75.0" text="Description" />
-                        <TableColumn fx:id="recurringColumn" prefWidth="75.0" text="Recurring" />
+                        <TableColumn fx:id="date" prefWidth="75.0" text="Date" />
+                        <TableColumn fx:id="amount" prefWidth="75.0" text="Amount" />
+                        <TableColumn fx:id="category" prefWidth="75.0" text="Category" />
+                        <TableColumn fx:id="description" prefWidth="75.0" text="Description" />
+                        <TableColumn fx:id="recurring" prefWidth="75.0" text="Recurring" />
                      </columns>
                      <columnResizePolicy>
                         <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
-- 
GitLab