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 5173b74f49f094576e774c727ebfbc42cd6b7196..86a9d25e8df6bf42ae6b58199f1519170c27397e 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 @@ -10,9 +10,29 @@ import java.io.*; * * @author andreas */ + +//Få fikset ny return knapp i dualList + //Disable next i dualList før income har blitt lagt til. Kanksje filter som prevneter negativ max amount + //La next button save dataene i to nye filer for persisterign i tilfelle "back" trykkes i newBudgetbudgetr + //Gjøre user.dir path static + Fikse nye klasser for filehandling + //Fikse vindu for old budget + FIKSE TRY CATCHES I ALLE METODENE!!!!!!!!!! + //add menuitems i income og expense slik at + //Gjøre slik at når du completer et budget (next button i budget window) + //skal dette legges til i budgetreigsteret + + + //System.out.println(System.getProperty("user.dir") + path); = + //C:\Users\xulr0\IdeaProjects\skoleProsjekter\idatt1002\idatt1002_2023_9src/main/resources/ + + //(System.getProperty("user.dir") + "/" + path) = + //(path) = + //(src/main/resources) public class FileHandlingBudget { 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 path = "src/main/resources/"; + private static final String type = ".register"; private static final String maxAmount = "maxAmount="; private static final String budgetAmount = "budgetAmount="; private static final String budgetCategory = "budgetCategory="; @@ -27,7 +47,8 @@ public class FileHandlingBudget { * @throws IOException if an input or output exception occurred. */ public static void writeGeneralBudgetToFile(String fileTitle, GeneralBudget generalBudget) throws IOException { - try (BufferedWriter bw = new BufferedWriter(new FileWriter(filePath + fileTitle + fileType))) { + try (BufferedWriter bw = new BufferedWriter(new FileWriter(path + readCurrentFile() + "/" + fileTitle + fileType))) { + //try (BufferedWriter bw = new BufferedWriter(new FileWriter(filePath + fileTitle + fileType))) { bw.write(generalBudget.toString()); } catch (IOException ex) { throw new IOException("Error writing to file: " + fileTitle); @@ -42,16 +63,15 @@ public class FileHandlingBudget { * @throws IOException if an input or output exception occurred. */ public static boolean isEmpty(String fileTitle) throws IOException { - try (BufferedReader br = new BufferedReader(new FileReader(filePath + fileTitle + fileType))) { + /*try (BufferedReader br = new BufferedReader(new FileReader(filePath + fileTitle + fileType))) { return br.readLine() == null; - } - } - - 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)); + }*/ + BufferedReader br = new BufferedReader(new FileReader(path + readCurrentFile() + "/" + fileTitle + fileType)); + System.out.println("Trying budget isEmpty in: " + path + readCurrentFile() + "/" + fileTitle + fileType); return br.readLine() == null; } + + /** * Method for checking if a .budget file is new (no categories) * or old (has budget categories) @@ -61,42 +81,97 @@ public class FileHandlingBudget { */ public static boolean isNewBudget(String fileTitle) throws IOException { try (BufferedReader br = new BufferedReader( - new FileReader(filePath + fileTitle + fileType))) { + new FileReader(path + readCurrentFile() + "/" + fileTitle + fileType))) { + //new FileReader(filePath + fileTitle + fileType))) { for (int i = 0; i < 2; ++i) { br.readLine(); } if (br.readLine() == null) { + System.out.println("Is new budget - no max amount"); return true; } } + System.out.println("Is 'old' budget - has max amount"); return false; } + public static void writeBudgetRegisterToArchive(BudgetRegister budgetNames) throws IOException { + try { + BufferedWriter bw = new BufferedWriter(new FileWriter(System.getProperty("user.dir") + "/" + path + "Archive.txt")); + System.out.println(System.getProperty("user.dir") + "/" + path + "Archive.txt"); + bw.write("motherfucer"); + } catch (IOException ioe) { + ioe.printStackTrace(); + throw new IOException("Could not write to file: Archive.txt"); + } + } + + public static BudgetRegister readBudgetArchive(String fileTitle) throws IOException { + BufferedReader br; + BudgetRegister budgetRegister = null; + String budgetName; + + try { + br = new BufferedReader( + new FileReader(System.getProperty("user.dir") + "/" + path + + "Archive.txt")); + } catch (IOException ioe) { + throw new IOException("File does not exist", ioe); + } + + String line; + String nextLine = br.readLine(); + while ((line = nextLine) != null) { + System.out.println(line); + nextLine = br.readLine(); + budgetName = line; + + if(budgetRegister == null){ + budgetRegister = new BudgetRegister(); + } + budgetRegister.addBudgetName(budgetName); + } + return budgetRegister; + } + public static boolean isCurrentFileEmpty(String fileTitle) throws IOException { + try (BufferedReader br = new BufferedReader(new FileReader(System.getProperty("user.dir") + "src/main/resources/" + + "CurrentFile.txt"))) { + return br.readLine() == null; + } + } + + public static void writeSavingsToFile() { + + } 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); + System.out.println("Max amount is..."); } catch (IOException ex) { throw new IOException("Error writing to file: " + fileDestination); } } - public static String readCurrentFile(String fileTitle) throws IOException { + public static String readCurrentFile() throws IOException { BufferedReader br; try { - FileReader fileReader = new FileReader(System.getProperty("user.dir") + "/src/main/resources/" + fileTitle + ".txt"); + System.out.print("Reading current file..."); + FileReader fileReader = new FileReader(System.getProperty("user.dir") + "/src/main/resources/CurrentFile.txt"); br = new BufferedReader(fileReader); } catch (IOException ioException) { - throw new IOException("File: " + fileTitle + "does not exist"); + throw new IOException("File: CurrentFile.txt does not exist"); } + System.out.println("Successful read on current file"); 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); + System.out.println("Current file is: " + budgetName); } catch (IOException ex) { throw new IOException("Error writing to file: " + "CurrentFile.txt"); } @@ -104,33 +179,36 @@ public class FileHandlingBudget { public static boolean deleteBudgetDirectory(String budgetID) { File targetDirectory = new File(System.getProperty("user.dir") + "/src/main/resources/" + budgetID); - System.out.println(targetDirectory.getPath()); + System.out.println("Deleting directory:" + 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()); + System.out.println("Deleting file:" + currentFile.delete()); } return targetDirectory.delete(); } public static boolean createBudgetDirectory(String budgetID) { + System.out.println("Directory: " + System.getProperty("user.dir") + "/src/main/resources/" + 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); + System.out.println("Income filepath: " + System.getProperty("user.dir") + "/src/main/resources/" + budgetID + "/" + incomeFileTitle + type); + File incomeFile = new File(System.getProperty("user.dir") + "/src/main/resources/" + budgetID + "/" + incomeFileTitle + type); 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); + System.out.println("Expense filePath: " + System.getProperty("user.dir") + "/src/main/resources/" + budgetID + "/" + expenseFileTitle + type); + File expenseFile = new File(System.getProperty("user.dir") + "/src/main/resources/" + budgetID + "/" + expenseFileTitle + type); expenseFile.createNewFile(); } public static void createNewBudgetFile(String budgetID, String budgetFileTitle) throws IOException { + System.out.println("Budget filePath: " + System.getProperty("user.dir") + "/src/main/resources/" + budgetID + "/" + budgetFileTitle + fileType); File budgetFile = new File(System.getProperty("user.dir") + "/src/main/resources/" + budgetID + "/" + budgetFileTitle + fileType); budgetFile.createNewFile(); } @@ -149,8 +227,7 @@ public class FileHandlingBudget { ExpenseCategory expenseCategory = null; String budgetDescription = null; - try (BufferedReader br = new BufferedReader(new FileReader(System.getProperty("user.dir") + "src/main/resources" + fileTitle + fileType))) { - System.out.println(filePath + fileTitle + fileType); + try (BufferedReader br = new BufferedReader(new FileReader( System.getProperty("user.dir") + "/" + path + readCurrentFile() + "/" + fileTitle + fileType))) { String line; String nextLine = br.readLine(); while ((line = nextLine) != null) { diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/Economics/FileHandling.java b/src/main/java/no/ntnu/idatt1002/demo/data/Economics/FileHandling.java index 04cb5918824b7aaa6afef04e7b47bfce818f71a0..3487e3c78d6148f9b1866c54beb0d182e12cc831 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/data/Economics/FileHandling.java +++ b/src/main/java/no/ntnu/idatt1002/demo/data/Economics/FileHandling.java @@ -5,6 +5,7 @@ import java.io.FileWriter; import java.io.IOException; import java.io.*; import java.time.LocalDate; +import no.ntnu.idatt1002.demo.data.Budget.FileHandlingBudget; /** @@ -15,6 +16,8 @@ import java.time.LocalDate; */ public class FileHandling{ private static final String filePath = "src/main/resources/Economics/"; + + private static final String path = "src/main/resources/"; private static final String fileType = ".register"; private static final String date = "date="; private static final String description = "description="; @@ -30,7 +33,8 @@ public class FileHandling{ * @throws IOException if an input or output exception occurred. */ public static <T extends Item>void writeItemRegisterToFile(final ItemRegister<T> itemRegister, String fileTitle) throws IOException { - try (BufferedWriter bw = new BufferedWriter(new FileWriter(filePath + fileTitle + fileType))) { + //try (BufferedWriter bw = new BufferedWriter(new FileWriter(filePath + fileTitle + fileType))) { + try (BufferedWriter bw = new BufferedWriter(new FileWriter(path + FileHandlingBudget.readCurrentFile() + "/" + fileTitle + fileType))) { if (itemRegister.isEmpty()){ bw.write(""); } else{ @@ -49,13 +53,16 @@ public class FileHandling{ * @throws IOException if an input or output exception occurred. */ public static boolean isEmpty(String fileTitle) throws IOException { - try (BufferedReader br = new BufferedReader(new FileReader(filePath + fileTitle + fileType))) { + /*try (BufferedReader br = new BufferedReader(new FileReader(filePath + fileTitle + fileType))) { if (br.readLine() == null) { return true; } else { return false; } - } + }*/ + BufferedReader br = new BufferedReader(new FileReader(path + FileHandlingBudget.readCurrentFile() + "/" + fileTitle + fileType)); + System.out.println("Checking if income is empty in: " + path + FileHandlingBudget.readCurrentFile() + "/" + fileTitle + fileType); + return br.readLine() == null; } /** @@ -72,7 +79,8 @@ public class FileHandling{ double amount = 0; boolean reoccuring = false; IncomeCategory incomeCategory = null; - try (BufferedReader br = new BufferedReader(new FileReader(filePath + fileTitle + fileType))) { + try (BufferedReader br = new BufferedReader(new FileReader(path + FileHandlingBudget.readCurrentFile() + "/" + fileTitle + fileType))) { + //try (BufferedReader br = new BufferedReader(new FileReader(filePath + fileTitle + fileType))) { String line; String nextLine = br.readLine(); while ((line = nextLine) != null) { @@ -119,7 +127,8 @@ public class FileHandling{ double amount = 0; boolean reoccuring = false; ExpenseCategory expenseCategory = null; - try (BufferedReader br = new BufferedReader(new FileReader(filePath + fileTitle + fileType))) { + try (BufferedReader br = new BufferedReader(new FileReader(path + FileHandlingBudget.readCurrentFile() + "/" + fileTitle + fileType))) { + //try (BufferedReader br = new BufferedReader(new FileReader(filePath + fileTitle + fileType))) { String line; String nextLine = br.readLine(); while ((line = nextLine) != null) {