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 3fabc9df937c1f46a1892b15f3521eea5d345acd..e9706d65f0e3bd4e821b6cbd59011befd7e3ff80 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 @@ -12,10 +12,10 @@ import java.io.*; */ public class FileHandlingBudget { - private static final String filePath = "src/main/resources/Budget"; + private static final String filePath = "src/main/resources/budgets/Budget/"; private static final String fileType = ".budget"; - private static final String path = "src/main/resources/"; + private static final String path = "src/main/resources/budgets/"; private static final String type = ".register"; private static final String maxAmount = "maxAmount="; private static final String budgetAmount = "budgetAmount="; @@ -31,7 +31,7 @@ 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(path + readSelectedBudget() + "/" + fileTitle + fileType))) { + try (BufferedWriter bw = new BufferedWriter(new FileWriter(path + FileHandlingSelectedBudget.readSelectedBudget() + "/" + fileTitle + fileType))) { //try (BufferedWriter bw = new BufferedWriter(new FileWriter(filePath + fileTitle + fileType))) { bw.write(generalBudget.toString()); } catch (IOException ex) { @@ -50,9 +50,10 @@ public class FileHandlingBudget { /*try (BufferedReader br = new BufferedReader(new FileReader(filePath + fileTitle + fileType))) { return br.readLine() == null; }*/ - BufferedReader br = new BufferedReader(new FileReader(path + readSelectedBudget() + "/" + fileTitle + fileType)); - System.out.println("Trying budget isEmpty in: " + path + readSelectedBudget() + "/" + fileTitle + fileType); - return br.readLine() == null; + try (BufferedReader br = new BufferedReader(new FileReader(path + FileHandlingSelectedBudget.readSelectedBudget() + "/" + fileTitle + fileType))) { + System.out.println("Trying budget isEmpty in: " + path + FileHandlingSelectedBudget.readSelectedBudget() + "/" + fileTitle + fileType); + return br.readLine() == null; + } } @@ -65,7 +66,7 @@ public class FileHandlingBudget { */ public static boolean isNewBudget(String fileTitle) throws IOException { try (BufferedReader br = new BufferedReader( - new FileReader(path + readSelectedBudget() + "/" + fileTitle + fileType))) { + new FileReader(path + FileHandlingSelectedBudget.readSelectedBudget() + "/" + fileTitle + fileType))) { //new FileReader(filePath + fileTitle + fileType))) { for (int i = 0; i < 2; ++i) { @@ -80,63 +81,10 @@ public class FileHandlingBudget { 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(budgetNames.toString()); - } catch (IOException ioe) { - ioe.printStackTrace(); - throw new IOException("Could not write to file: Archive.txt"); - } - } - - public static boolean isBudgetRegisterEmpty() throws IOException { - try (BufferedReader br = new BufferedReader(new FileReader(System.getProperty("user.dir") + "/src/main/resources/" - + "Archive.txt"))) { - return br.readLine() == null; - } - } - 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 isSelectedBudgetEmpty(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"))) { + try (BufferedWriter bw = new BufferedWriter(new FileWriter(path + + fileDestination + "/Budget" + fileType))) { bw.write("maxAmount=" + maxAmount); System.out.println("Max amount is..."); } catch (IOException ex) { @@ -144,64 +92,6 @@ public class FileHandlingBudget { } } - public static String readSelectedBudget() throws IOException { - BufferedReader br; - try { - 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: CurrentFile.txt does not exist"); - } - System.out.println("Successful read on current file"); - return br.readLine(); - } - - public static void updateSelectedBudget(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"); - } - } - - public static boolean deleteBudgetDirectory(String budgetID) { - File targetDirectory = new File(System.getProperty("user.dir") + "/src/main/resources/" + budgetID); - 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("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("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 { - 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(); - } - /** * Method for reading (getting) a Budget from a file. * @@ -216,7 +106,7 @@ public class FileHandlingBudget { ExpenseCategory expenseCategory = null; String budgetDescription = null; - try (BufferedReader br = new BufferedReader(new FileReader( System.getProperty("user.dir") + "/" + path + readSelectedBudget() + "/" + fileTitle + fileType))) { + try (BufferedReader br = new BufferedReader(new FileReader( System.getProperty("user.dir") + "/" + path + FileHandlingSelectedBudget.readSelectedBudget() + "/" + fileTitle + fileType))) { String line; String nextLine = br.readLine(); while ((line = nextLine) != null) { diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/Budget/FileHandlingBudgetArchive.java b/src/main/java/no/ntnu/idatt1002/demo/data/Budget/FileHandlingBudgetArchive.java index 2e3dd4cf60e69f31a3a174613f46596ca9b26da7..c39e54a5f53503d2533f17561fb5757b3dca6fdc 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/data/Budget/FileHandlingBudgetArchive.java +++ b/src/main/java/no/ntnu/idatt1002/demo/data/Budget/FileHandlingBudgetArchive.java @@ -8,49 +8,44 @@ import java.io.FileWriter; import java.io.IOException; public class FileHandlingBudgetArchive { - private static final String path = System.getProperty("user.dir") + "/src/main/resources/"; - private static final String archiveFileType = ".arc"; + private static final String filePath = "src/main/resources/budgets/"; + private static final String fileType = ".archive"; 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"); + try (BufferedWriter bw = new BufferedWriter(new FileWriter(filePath + "Archive" + fileType))) { + System.out.println(filePath + "Archive.txt"); bw.write(budgetNames.toString()); } catch (IOException ioe) { ioe.printStackTrace(); - throw new IOException("Could not write to file: Archive.txt"); + throw new IOException("Could not write to file: Archive.txt", ioe); } } public static boolean isBudgetRegisterEmpty() throws IOException { - try (BufferedReader br = new BufferedReader(new FileReader(System.getProperty("user.dir") + "/src/main/resources/" - + "Archive.txt"))) { + try (BufferedReader br = new BufferedReader(new FileReader(filePath + + "Archive" + fileType))) { return br.readLine() == null; } } public static BudgetRegister readBudgetArchive(String fileTitle) throws IOException { - BufferedReader br; BudgetRegister budgetRegister = null; String budgetName; + String line; - try { - br = new BufferedReader( - new FileReader(System.getProperty("user.dir") + "/" + path - + "Archive.txt")); - } catch (IOException ioe) { - throw new IOException("File does not exist", ioe); - } + try (BufferedReader br = new BufferedReader( + new FileReader(filePath + "Archive" + fileType))) { - String line; - String nextLine = br.readLine(); - while ((line = nextLine) != null) { - System.out.println(line); - nextLine = br.readLine(); - budgetName = line; + String nextLine = br.readLine(); + while ((line = nextLine) != null) { + System.out.println(line); + nextLine = br.readLine(); + budgetName = line; - if(budgetRegister == null){ - budgetRegister = new BudgetRegister(); + if (budgetRegister == null) { + budgetRegister = new BudgetRegister(); + } + budgetRegister.addBudgetName(budgetName); } - budgetRegister.addBudgetName(budgetName); } return budgetRegister; } @@ -60,7 +55,7 @@ public class FileHandlingBudgetArchive { } public static boolean deleteBudgetDirectory(String budgetID) { - File targetDirectory = new File(System.getProperty("user.dir") + "/src/main/resources/" + budgetID); + File targetDirectory = new File(filePath + budgetID); System.out.println("Deleting directory:" + targetDirectory.getPath()); String[]entries = targetDirectory.list(); diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/Budget/FileHandlingSelectedBudget.java b/src/main/java/no/ntnu/idatt1002/demo/data/Budget/FileHandlingSelectedBudget.java index 9d9dc0848c480616ac5f6db55f7d119732e06138..85e95294a1529503826b2e96767e529f9c3a0d83 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/data/Budget/FileHandlingSelectedBudget.java +++ b/src/main/java/no/ntnu/idatt1002/demo/data/Budget/FileHandlingSelectedBudget.java @@ -9,61 +9,61 @@ import java.io.IOException; public class FileHandlingSelectedBudget { - private static final String path = System.getProperty("user.dir") + "/src/main/resources/"; - private static final String currentFileType = ".current"; + private static final String filePath = "src/main/resources/budgets/"; + + private static final String selectedBudgetFileType = ".current"; private static final String registerFileType = ".register"; private static final String budgetFileType = ".budget"; + private static final String slash = "/"; + public static String readSelectedBudget() throws IOException { - BufferedReader br; - try { - System.out.print("Reading current file..."); - FileReader fileReader = new FileReader(System.getProperty("user.dir") + "/src/main/resources/CurrentFile.txt"); - br = new BufferedReader(fileReader); + try (BufferedReader br = new BufferedReader(new FileReader(filePath + "SelectedBudget" + + selectedBudgetFileType))) { + return br.readLine(); } catch (IOException ioException) { - throw new IOException("File: CurrentFile.txt does not exist"); + throw new IOException("File: CurrentFile.txt does not exist", ioException); } - System.out.println("Successful read on current file"); - return br.readLine(); } public static void updateSelectedBudget(String budgetName) throws IOException { - try (BufferedWriter bw = new BufferedWriter(new FileWriter(System.getProperty("user.dir") + "/src/main/resources/CurrentFile.txt"))) { + try (BufferedWriter bw = new BufferedWriter(new FileWriter(filePath + "SelectedBudget" + + selectedBudgetFileType))) { bw.write(budgetName); System.out.println("Current file is: " + budgetName); } catch (IOException ex) { - throw new IOException("Error writing to file: " + "CurrentFile.txt"); + throw new IOException("Error writing to file: " + "SelectedBudget.current"); } } public static boolean isSelectedBudgetEmpty(String fileTitle) throws IOException { - try (BufferedReader br = new BufferedReader(new FileReader(System.getProperty("user.dir") + "src/main/resources/" - + "CurrentFile.txt"))) { + try (BufferedReader br = new BufferedReader(new FileReader(filePath + + "SelectedBudget" + selectedBudgetFileType))) { return br.readLine() == null; } } 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); + System.out.println("Directory: " + filePath + budgetId); + File f = new File(filePath + budgetId); return f.mkdir(); } public static void createNewIncomeFile(String budgetId, String incomeFileTitle) throws IOException { - System.out.println("Income filepath: " + System.getProperty("user.dir") + "/src/main/resources/" + budgetId + "/" + incomeFileTitle + registerFileType); - File incomeFile = new File(System.getProperty("user.dir") + "/src/main/resources/" + budgetId + "/" + incomeFileTitle + registerFileType); + System.out.println("Income filepath: " + filePath + budgetId + "/" + incomeFileTitle + registerFileType); + File incomeFile = new File(filePath + budgetId + "/" + incomeFileTitle + registerFileType); incomeFile.createNewFile(); } public static void createNewExpenseFile(String budgetId, String expenseFileTitle) throws IOException { - System.out.println("Expense filePath: " + System.getProperty("user.dir") + "/src/main/resources/" + budgetId + "/" + expenseFileTitle + registerFileType); - File expenseFile = new File(System.getProperty("user.dir") + "/src/main/resources/" + budgetId + "/" + expenseFileTitle + registerFileType); + System.out.println("Expense filePath: " + filePath + budgetId + "/" + expenseFileTitle + registerFileType); + File expenseFile = new File(filePath + budgetId + "/" + expenseFileTitle + registerFileType); 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 + budgetFileType); - File budgetFile = new File(System.getProperty("user.dir") + "/src/main/resources/" + budgetId + "/" + budgetFileTitle + budgetFileType); + System.out.println("Budget filePath: " + filePath + budgetId + "/" + budgetFileTitle + budgetFileType); + File budgetFile = new File(filePath + budgetId + "/" + budgetFileTitle + budgetFileType); budgetFile.createNewFile(); } } 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 27c05810bedd3d09595f8777faf970c2f4bfe89e..a9568b982d635cee356f7834560c20477fc8fa40 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 @@ -6,6 +6,7 @@ import java.io.IOException; import java.io.*; import java.time.LocalDate; import no.ntnu.idatt1002.demo.data.Budget.FileHandlingBudget; +import no.ntnu.idatt1002.demo.data.Budget.FileHandlingSelectedBudget; /** @@ -15,9 +16,9 @@ import no.ntnu.idatt1002.demo.data.Budget.FileHandlingBudget; * @author andreas */ public class FileHandling{ - private static final String filePath = "src/main/resources/Economics/"; + private static final String filePath = "src/main/resources/budgets/Economics/"; - private static final String path = "src/main/resources/"; + private static final String path = "src/main/resources/budgets/"; private static final String fileType = ".register"; private static final String date = "date="; private static final String description = "description="; @@ -34,7 +35,7 @@ public class FileHandling{ */ 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(path + FileHandlingBudget.readSelectedBudget() + "/" + fileTitle + fileType))) { + try (BufferedWriter bw = new BufferedWriter(new FileWriter(path + FileHandlingSelectedBudget.readSelectedBudget() + "/" + fileTitle + fileType))) { if (itemRegister.isEmpty()){ bw.write(""); } else{ @@ -60,9 +61,10 @@ public class FileHandling{ return false; } }*/ - BufferedReader br = new BufferedReader(new FileReader(path + FileHandlingBudget.readSelectedBudget() + "/" + fileTitle + fileType)); - System.out.println("Checking if income is empty in: " + path + FileHandlingBudget.readSelectedBudget() + "/" + fileTitle + fileType); - return br.readLine() == null; + + try (BufferedReader br = new BufferedReader(new FileReader(path + FileHandlingSelectedBudget.readSelectedBudget() + "/" + fileTitle + fileType))) { + return br.readLine() == null; + } } /** @@ -79,7 +81,7 @@ public class FileHandling{ double amount = 0; boolean reoccuring = false; IncomeCategory incomeCategory = null; - try (BufferedReader br = new BufferedReader(new FileReader(path + FileHandlingBudget.readSelectedBudget() + "/" + fileTitle + fileType))) { + try (BufferedReader br = new BufferedReader(new FileReader(path + FileHandlingSelectedBudget.readSelectedBudget() + "/" + fileTitle + fileType))) { //try (BufferedReader br = new BufferedReader(new FileReader(filePath + fileTitle + fileType))) { String line; String nextLine = br.readLine(); @@ -127,7 +129,7 @@ public class FileHandling{ double amount = 0; boolean reoccuring = false; ExpenseCategory expenseCategory = null; - try (BufferedReader br = new BufferedReader(new FileReader(path + FileHandlingBudget.readSelectedBudget() + "/" + fileTitle + fileType))) { + try (BufferedReader br = new BufferedReader(new FileReader(path + FileHandlingSelectedBudget.readSelectedBudget() + "/" + fileTitle + fileType))) { //try (BufferedReader br = new BufferedReader(new FileReader(filePath + fileTitle + fileType))) { String line; String nextLine = br.readLine();