Skip to content
Snippets Groups Projects
Select Git revision
2 results Searching

serializers.py

Blame
  • Forked from Åsmund Haugse / tdt4242-base
    Source project has a limited visibility.
    ExpensesController.java 6.80 KiB
    package no.ntnu.idatt1002.demo.controller;
    
    import java.io.File;
    import java.io.IOException;
    
    import java.time.LocalDate;
    import java.time.Month;
    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.TextField;
    import javafx.scene.control.cell.PropertyValueFactory;
    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;
    
    enum DialogMode {
      ADD, EDIT, DELETE
    }
    public class ExpensesController {
    
      /**
       * The mode of the dialog. NEW if new contact, EDIT if edit existing contact.
       */
      private DialogMode mode;
    
      @FXML
      private Button addBtn;
      @FXML
      private Button editBtn;
    
      @FXML
      private Button deleteBtn;
    
      @FXML
      private ComboBox<String> show;
    
      @FXML
      private Button incomeBtn;
    
      @FXML
      private Button overviewBtn;
    
      @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;
    
      ExpenseRegister expenseRegister;
      ObservableList<Expense> expenses;
    
      @FXML
      public void initialize() throws IOException {
        ObservableList<String> filter = FXCollections.observableArrayList("All", "Other", "Food");
        show.setItems(filter);
        show.setValue("All");
    
        Expense newExpense = new Expense(99, true, ExpenseCategory.FOOD, LocalDate.of(2023, Month.JULY, 5));
        expenseRegister.addItem(newExpense);
        expenses = FXCollections.observableArrayList(expenseRegister.getItems());
    
        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"));
    
        expenseTableView.setItems(expenses);
      }
    
      @FXML
      protected void handleAddButton(ActionEvent event) {
        handleEditButton(event);
      }
      @FXML
      protected void handleEditButton(ActionEvent event) {
        Expense newExpense = null;
        String dialogTitle = "";
        // Load the FXML file for your dialog box
        FXMLLoader loader = new FXMLLoader(getClass().getResource("/view/AddExpense.fxml"));
        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();
    
        if (event.getSource().equals(addBtn)) {
          mode = DialogMode.ADD;
          dialogTitle = "Add expense";
    
        } else if (event.getSource().equals(editBtn) && expenseTableView.getSelectionModel().getSelectedItem() != null) {
          mode = DialogMode.EDIT;
          dialogTitle = "Edit expense";
          newExpense = expenseTableView.getSelectionModel().getSelectedItem();
          dialogController.setExpense(newExpense);
        } else {
          return;
        }
    
        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();
    
        //Only add the expense to the tableview, if the expense is not null
        if (newExpense != null && mode == DialogMode.ADD) {
          expenseRegister.addItem(newExpense);
          refreshObservableList();
        } else {
          System.out.println("expense is null or dialog mode is add");
        }
        expenseTableView.refresh();
      }
    
      protected void refreshObservableList() {
        this.expenses.setAll(expenseRegister.getItems());
    
      }
    
      @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);
          refreshObservableList();
        }
      }
    
      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 loadDataFromFile(String fileName) throws IOException {
        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() == overviewBtn) {
          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();
      }
    }