From 28f63fa0de15934c54c913338d22b3b9cc295896 Mon Sep 17 00:00:00 2001 From: Harry Linrui XU <xulr0820@hotmail.com> Date: Tue, 18 Apr 2023 13:02:42 +0200 Subject: [PATCH] Working on creating several budgets. --- .../demo/controller/BudgetController.java | 3 +- .../controller/CreateBudgetController.java | 133 ++++++++++++++++++ .../controller/IncomeExpenseController.java | 11 +- .../demo/controller/SceneController.java | 105 +++++++++++--- .../demo/data/Budget/FileHandlingBudget.java | 74 +++++++++- src/main/resources/Budget/Budget.budget | 6 +- src/main/resources/Economics/Expense.register | 10 +- src/main/resources/Economics/Income.register | 8 +- 8 files changed, 306 insertions(+), 44 deletions(-) create mode 100644 src/main/java/no/ntnu/idatt1002/demo/controller/CreateBudgetController.java diff --git a/src/main/java/no/ntnu/idatt1002/demo/controller/BudgetController.java b/src/main/java/no/ntnu/idatt1002/demo/controller/BudgetController.java index 87acb2d5..d6e931cf 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/controller/BudgetController.java +++ b/src/main/java/no/ntnu/idatt1002/demo/controller/BudgetController.java @@ -100,7 +100,8 @@ public class BudgetController extends FinanceController { descriptionCol.setCellValueFactory(new PropertyValueFactory<BudgetItem, String>("budgetDescription")); try { - general = loadBudgetDataFromFile("Budget"); + System.out.println("Fuck you"); + general = loadBudgetDataFromFile(FileHandlingBudget.readCurrentFile("CurrentFile") + "/Budget"); budgetList = FXCollections.observableArrayList(general.getBudgetItems()); budgetTableView.setItems(budgetList); if (FileHandlingBudget.isNewBudget("Budget")) { diff --git a/src/main/java/no/ntnu/idatt1002/demo/controller/CreateBudgetController.java b/src/main/java/no/ntnu/idatt1002/demo/controller/CreateBudgetController.java new file mode 100644 index 00000000..aa2091e3 --- /dev/null +++ b/src/main/java/no/ntnu/idatt1002/demo/controller/CreateBudgetController.java @@ -0,0 +1,133 @@ +package no.ntnu.idatt1002.demo.controller; + +import java.io.IOException; +import java.time.LocalDate; +import java.util.Optional; +import javafx.event.ActionEvent; +import javafx.fxml.FXML; +import javafx.scene.Node; +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.Label; +import javafx.scene.control.TextField; +import javafx.stage.Stage; +import no.ntnu.idatt1002.demo.data.Budget.FileHandlingBudget; +import no.ntnu.idatt1002.demo.data.Economics.Expense; + +public class CreateBudgetController { + + public Button okBtn; + public Button cancelBtn; + private String currentMonth; + + private Expense expense; + + private String budgetName; + + @FXML + private TextField nameField; + + @FXML + private Label errorMsg; + + + @FXML + public void initialize() { + currentMonth = String.valueOf(LocalDate.now().getMonth()); + okBtn.addEventFilter( + ActionEvent.ACTION, event -> { + if(nameField.getText().equals("")) { + errorMsg.setOpacity(1); + event.consume(); + } + }); + okBtn.addEventFilter( + ActionEvent.ACTION, event -> { + //if(hasDuplicate() { //TODO MAYBE THIS IN THE START BUDGET FILE + //errorMsg.setText("Budget for this month already exist") + } + ); + } + @FXML + public void pressOkBtn(ActionEvent event) { + String title = "Confirm name"; + String header = "Are you sure you to use this name?"; + String content = "The name cannot be changed later"; + + budgetName = nameField.getText(); + System.out.println(budgetName); + + if(!createNewFiles(budgetName)) { + updateCurrentFile("", ""); + errorMsg.setText("A budget of the same \nname already exists"); + errorMsg.setOpacity(1); + return; + } + + Optional<ButtonType> isConfirmed = showConfirmationDialog(title, header, content); + if (isConfirmed.isPresent() && isConfirmed.get() == ButtonType.OK) { + updateCurrentFile(currentMonth, budgetName); + } else { + updateCurrentFile("", ""); + } + + final Node source = (Node) event.getSource(); + ((Stage) source.getScene().getWindow()).close(); + } + + @FXML + public void pressCancelBtn(ActionEvent event) { + updateCurrentFile("", ""); + final Node source = (Node) event.getSource(); + ((Stage) source.getScene().getWindow()).close(); + } + + /** + * 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. + */ + @FXML + private Optional<ButtonType> showConfirmationDialog(String title, String header, String content) { + Alert alert = new Alert(Alert.AlertType.CONFIRMATION); + alert.setTitle(title); + alert.setHeaderText(header); + alert.setContentText(content); + + return alert.showAndWait(); + } + + private void showErrorMsgBox(String title, String header, String content) { + Alert alert = new Alert(AlertType.ERROR); + alert.setTitle(title); + alert.setHeaderText(header); + alert.setContentText(content); + + alert.showAndWait(); + } + + public boolean createNewFiles(String budgetName) { + boolean empty; + try { + empty = FileHandlingBudget.createBudgetDirectory(currentMonth + budgetName); + FileHandlingBudget.createNewIncomeFile(currentMonth + budgetName, "Income"); + FileHandlingBudget.createNewExpenseFile(currentMonth + budgetName, "Expense"); + FileHandlingBudget.createNewBudgetFile(currentMonth + budgetName, "Budget"); + } catch (IOException ioe) { + empty = false; + System.out.println(ioe.getMessage()); + ioe.printStackTrace(); + showErrorMsgBox(ioe.getMessage(), ioe.getMessage(), ioe.getMessage()); + } + return empty; + } + + public void updateCurrentFile(String currentMonth, String budgetName) { + try { + FileHandlingBudget.updateCurrentFile(currentMonth + budgetName); + } catch (IOException ioe) { + showErrorMsgBox(ioe.getMessage(), ioe.getMessage(), ioe.getMessage()); + } + } +} diff --git a/src/main/java/no/ntnu/idatt1002/demo/controller/IncomeExpenseController.java b/src/main/java/no/ntnu/idatt1002/demo/controller/IncomeExpenseController.java index d2380156..eac049a7 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/controller/IncomeExpenseController.java +++ b/src/main/java/no/ntnu/idatt1002/demo/controller/IncomeExpenseController.java @@ -473,6 +473,10 @@ public class IncomeExpenseController extends FinanceController { FileHandling.writeItemRegisterToFile(expenseRegister, "Expense"); } + public void saveDisposableIncomeToFile() throws IOException { + String disposableIncomeAsString = String.valueOf(incomeRegister.getTotalSum() - expenseRegister.getTotalSum()); + FileHandlingBudget.writeMaxAmountToFile(FileHandlingBudget.readCurrentFile("CurrentFile"), disposableIncomeAsString); + } /** * 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 @@ -480,14 +484,15 @@ public class IncomeExpenseController extends FinanceController { @FXML public void switchScene(javafx.event.ActionEvent event) { try { - saveDataToFile(); FXMLLoader loader = new FXMLLoader(); if (event.getSource() == returnBtn) { + saveDataToFile(); loader.setLocation(getClass().getResource("/view/FirstMenu.fxml")); + System.out.println(FileHandlingBudget.deleteBudgetDirectory(FileHandlingBudget.readCurrentFile("CurrentFile"))); + FileHandlingBudget.updateCurrentFile(""); } 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 + saveDisposableIncomeToFile(); } Parent root = loader.load(); Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow(); diff --git a/src/main/java/no/ntnu/idatt1002/demo/controller/SceneController.java b/src/main/java/no/ntnu/idatt1002/demo/controller/SceneController.java index 66bdb460..9a8ccf35 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/controller/SceneController.java +++ b/src/main/java/no/ntnu/idatt1002/demo/controller/SceneController.java @@ -1,46 +1,113 @@ package no.ntnu.idatt1002.demo.controller; +import static com.sun.javafx.scene.control.skin.Utils.getResource; + import java.io.IOException; +import java.time.LocalDate; +import java.util.Optional; 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.ButtonType; +import javafx.scene.control.Dialog; import javafx.stage.Modality; import javafx.stage.Stage; +import no.ntnu.idatt1002.demo.data.Budget.FileHandlingBudget; +import no.ntnu.idatt1002.demo.data.Economics.Expense; public class SceneController { - private Stage stage; - private Scene scene; - - public void switchNewBudget(ActionEvent event) throws IOException { - FXMLLoader loader = new FXMLLoader(SceneController.class.getResource("/view/NewBudget.fxml")); - Parent root = loader.load(); - stage = (Stage)((Node)event.getSource()).getScene().getWindow(); - scene = new Scene(root); - stage.setScene(scene); - stage.show(); + @FXML + public void initialize() { } + + /** + * Brings up popup window to name budget + * @throws IOException + */ + @FXML public void switchIncome(ActionEvent event) throws IOException { - FXMLLoader loader = new FXMLLoader(SceneController.class.getResource("/view/newBudgetBudgert.fxml")); - Parent root = loader.load(); - stage = (Stage)((Node)event.getSource()).getScene().getWindow(); - scene = new Scene(root); - stage.setScene(scene); - stage.show(); + //Bring up popup X + //Let user fill in. When OK is pressed, show confirmation dialog as the name cannot be changed X + //Create maybe a folder, and Create three new files, using the budgetName + the dateMonth as the ID // + //Overwrite a "currentFile" file with the fileName + //Then switch scene to the expense vs income + + //Instantiate FXML loader and loads the popup for adding expense + + FXMLLoader loader = new FXMLLoader(); + loader.setLocation(getClass().getResource("/view/CreateBudget.fxml")); + + String budgetName; + + String dialogTitle = "Create budget"; + + Dialog<String> 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) { + showErrorDialogBox("Loading", "Error in loading dialog box", "Could not load" + + "the AddExpense window"); + } + + // Get the controller for the loaded FXML file + CreateBudgetController createBudgetController = new CreateBudgetController(); + // Show the Dialog and wait for the user to close it + dialog.setTitle(dialogTitle); + + dialog.showAndWait(); + + try { + if (FileHandlingBudget.readCurrentFile("CurrentFile") != null) { + switchNext(event); + } + } catch (IOException ioe) { + showErrorDialogBox("", "", ""); + } + } + + private void switchNext(ActionEvent event) { + try { + FXMLLoader loader = new FXMLLoader(); + loader.setLocation(SceneController.class.getResource("/view/dualList.fxml")); + 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("", "", ""); + } + } + 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(); } public void switchMainMenu(ActionEvent event) throws IOException { - FXMLLoader loader = new FXMLLoader(SceneController.class.getResource("/view/MainMenuNew.fxml")); + FXMLLoader loader = new FXMLLoader(SceneController.class.getResource("/view/dualList.fxml")); Parent root = loader.load(); - stage = (Stage)((Node)event.getSource()).getScene().getWindow(); - scene = new Scene(root); + Stage stage = (Stage)((Node)event.getSource()).getScene().getWindow(); + Scene scene = new Scene(root); stage.setScene(scene); stage.show(); } + public void closeButton(ActionEvent actionEvent) { final Node source = (Node) actionEvent.getSource(); final Stage stage = (Stage) source.getScene().getWindow(); diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/Budget/FileHandlingBudget.java b/src/main/java/no/ntnu/idatt1002/demo/data/Budget/FileHandlingBudget.java index 34d56778..5173b74f 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/data/Budget/FileHandlingBudget.java +++ b/src/main/java/no/ntnu/idatt1002/demo/data/Budget/FileHandlingBudget.java @@ -11,12 +11,13 @@ import java.io.*; * @author andreas */ public class FileHandlingBudget { - private static final String filePath = "src/main/resources/Budget/"; + private static final String filePath = "src/main/resources/Budget"; //TODO CHANGE BACK to /resources/Budget/ private static final String fileType = ".budget"; private static final String maxAmount = "maxAmount="; private static final String budgetAmount = "budgetAmount="; private static final String budgetCategory = "budgetCategory="; private static final String budgetDescription = "budgetDescription="; + public static String id = ""; /** * Method for writing (adding) a budget to a file. @@ -29,7 +30,7 @@ public class FileHandlingBudget { try (BufferedWriter bw = new BufferedWriter(new FileWriter(filePath + fileTitle + fileType))) { bw.write(generalBudget.toString()); } catch (IOException ex) { - throw new IOException("Error writing story to file: " + ex.getMessage()); + throw new IOException("Error writing to file: " + fileTitle); } } @@ -46,6 +47,11 @@ public class FileHandlingBudget { } } + public static boolean fileEmpty(String fileTitle) throws IOException { + BufferedReader br = new BufferedReader(new FileReader(System.getProperty("user.dir") + "src/main/resources/" + + readCurrentFile("CurrentFile") + fileTitle + fileType)); + return br.readLine() == null; + } /** * Method for checking if a .budget file is new (no categories) * or old (has budget categories) @@ -68,6 +74,67 @@ public class FileHandlingBudget { return false; } + public static void writeMaxAmountToFile(String fileDestination, String maxAmount) throws IOException { + try (BufferedWriter bw = new BufferedWriter(new FileWriter(System.getProperty("user.dir") + "/src/main/resources/" + + fileDestination + "/Budget.budget"))) { + bw.write("maxAmount=" + maxAmount); + } catch (IOException ex) { + throw new IOException("Error writing to file: " + fileDestination); + } + } + + public static String readCurrentFile(String fileTitle) throws IOException { + BufferedReader br; + try { + FileReader fileReader = new FileReader(System.getProperty("user.dir") + "/src/main/resources/" + fileTitle + ".txt"); + br = new BufferedReader(fileReader); + } catch (IOException ioException) { + throw new IOException("File: " + fileTitle + "does not exist"); + } + return br.readLine(); + } + + public static void updateCurrentFile(String budgetName) throws IOException { + try (BufferedWriter bw = new BufferedWriter(new FileWriter(System.getProperty("user.dir") + "/src/main/resources/CurrentFile.txt"))) { + bw.write(budgetName); + } catch (IOException ex) { + throw new IOException("Error writing to file: " + "CurrentFile.txt"); + } + } + + public static boolean deleteBudgetDirectory(String budgetID) { + File targetDirectory = new File(System.getProperty("user.dir") + "/src/main/resources/" + budgetID); + System.out.println(targetDirectory.getPath()); + + String[]entries = targetDirectory.list(); + assert entries != null; + for(String file : entries){ + File currentFile = new File(targetDirectory.getPath(),file); + System.out.println(currentFile.delete()); + } + + return targetDirectory.delete(); + } + public static boolean createBudgetDirectory(String budgetID) { + File f = new File(System.getProperty("user.dir") + "/src/main/resources/" + budgetID); + return f.mkdir(); + } + public static void createNewIncomeFile(String budgetID, String incomeFileTitle) throws IOException { + System.out.println(System.getProperty("user.dir") + "/src/main/resources/" + readCurrentFile("CurrentFile") + "/" + incomeFileTitle + fileType); + File incomeFile = new File(System.getProperty("user.dir") + "/src/main/resources/" + budgetID + "/" + incomeFileTitle + fileType); + incomeFile.createNewFile(); + } + + public static void createNewExpenseFile(String budgetID, String expenseFileTitle) throws IOException { + File expenseFile = new File(System.getProperty("user.dir") + "/src/main/resources/" + budgetID + "/" + expenseFileTitle + fileType); + expenseFile.createNewFile(); + } + + public static void createNewBudgetFile(String budgetID, String budgetFileTitle) throws IOException { + File budgetFile = new File(System.getProperty("user.dir") + "/src/main/resources/" + budgetID + "/" + budgetFileTitle + fileType); + budgetFile.createNewFile(); + } + /** * Method for reading (getting) a Budget from a file. * @@ -82,7 +149,8 @@ public class FileHandlingBudget { ExpenseCategory expenseCategory = null; String budgetDescription = null; - try (BufferedReader br = new BufferedReader(new FileReader(filePath + fileTitle + fileType))) { + try (BufferedReader br = new BufferedReader(new FileReader(System.getProperty("user.dir") + "src/main/resources" + fileTitle + fileType))) { + System.out.println(filePath + fileTitle + fileType); String line; String nextLine = br.readLine(); while ((line = nextLine) != null) { diff --git a/src/main/resources/Budget/Budget.budget b/src/main/resources/Budget/Budget.budget index e268ab22..90af344a 100644 --- a/src/main/resources/Budget/Budget.budget +++ b/src/main/resources/Budget/Budget.budget @@ -1,10 +1,10 @@ maxAmount=1000.0 -budgetAmount=500.0 +budgetAmount=100.0 budgetCategory=FOOD -budgetDescription=dd +budgetDescription= -budgetAmount=100.0 +budgetAmount=0.0 budgetCategory=CLOTHES budgetDescription= diff --git a/src/main/resources/Economics/Expense.register b/src/main/resources/Economics/Expense.register index 6f1ef622..cb93996b 100644 --- a/src/main/resources/Economics/Expense.register +++ b/src/main/resources/Economics/Expense.register @@ -1,11 +1,5 @@ -date=2023-03-01 -description=twelve -amount=12.0 -isRecurring=Not recurring -category=CLOTHES - -date=2023-03-26 -amount=200.0 +date=2023-04-17 +amount=50.0 isRecurring=Not recurring category=FOOD diff --git a/src/main/resources/Economics/Income.register b/src/main/resources/Economics/Income.register index 51f3e3d3..b2756581 100644 --- a/src/main/resources/Economics/Income.register +++ b/src/main/resources/Economics/Income.register @@ -1,10 +1,4 @@ -date=2023-03-24 -description=studie -amount=900.0 -isRecurring=Recurring -category=STUDENT_LOAN - -date=2023-03-25 +date=2023-04-17 amount=100.0 isRecurring=Not recurring category=GIFT -- GitLab