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 b38d598d8aefc44656635d36eed1789554a56385..279d2a2eef7b38e2ab6bb6c7db82530d6d5a834d 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 @@ -26,55 +26,85 @@ public class FileHandling { } } - public List<Income> readIncomeRegisterFromFile(String fileTitle) throws IOException { - ArrayList<Income> incomeRegister = new ArrayList<>(); + public ItemRegister<Income> readIncomeRegisterFromFile(String fileTitle) throws IOException { + ItemRegister<Income> incomeRegister = new ItemRegister<>(); + String date = ""; + String description = ""; + double amount = 0; + boolean reoccuring = false; + IncomeCategory incomeCategory = null; try (BufferedReader br = new BufferedReader(new FileReader("src/main/resources/" + fileTitle + ".itemRegister"))) { + br.readLine(); String line; - while ((line = br.readLine()) != null) { - if (!line.isEmpty()) { - String date = line; - String description = br.readLine(); - double amount = Double.parseDouble(br.readLine()); - boolean reoccuring = br.readLine().equals("Reoccurring"); - IncomeCategory incomeCategory; - line = br.readLine(); - if (line.equals("SALARY") || line.equals("STUDENT_LOAN") || line.equals("GIFT")) { - if (line.equals("SALARY")) { - incomeCategory = IncomeCategory.SALARY; - } else if (line.equals("STUDENT_LOAN")) { - incomeCategory = IncomeCategory.STUDENT_LOAN; - } else { - incomeCategory = IncomeCategory.GIFT; - } - incomeRegister.add(new Income(description,amount,reoccuring,incomeCategory,date)); + String nextLine = br.readLine(); + while ((line = nextLine) != null) { + nextLine = br.readLine(); + if(line.startsWith("date=")) { + date = line.replace("date=", ""); + } else if (line.startsWith("description=")) { + description = line.replace("description=",""); + } else if (line.startsWith("amount=")) { + amount = Double.parseDouble(line.replace("amount=","")); + } else if (line.startsWith("isReoccuring=")) { + reoccuring = line.replace("isReoccuring=","").equals("Reoccurring"); + } else if (line.startsWith("category=")) { + line = line.replace("category=",""); + incomeCategory = switch (line) { + case "GIFT" -> IncomeCategory.GIFT; + case "STUDENT_LOAN" -> IncomeCategory.STUDENT_LOAN; + default -> IncomeCategory.SALARY; + }; + } + if(line.isEmpty() || (nextLine == null)) { + if(description.equals("")){ + incomeRegister.addItem(new Income(amount, reoccuring, incomeCategory, date)); + } else{ + incomeRegister.addItem(new Income(description, amount, reoccuring, incomeCategory, date)); } + description = ""; } } } return incomeRegister; } - public List<Expense> readExpenseRegisterFromFile(String fileTitle) throws IOException { - ArrayList<Expense> expenseRegister = new ArrayList<>(); + public ItemRegister<Expense> readExpenseRegisterFromFile(String fileTitle) throws IOException { + ItemRegister<Expense> expenseRegister = new ItemRegister<>(); + String date = ""; + String description = ""; + double amount = 0; + boolean reoccuring = false; + ExpenseCategory expenseCategory = null; try (BufferedReader br = new BufferedReader(new FileReader("src/main/resources/" + fileTitle + ".itemRegister"))) { + br.readLine(); String line; - while ((line = br.readLine()) != null) { - if (!line.isEmpty()) { - String date = line; - String description = br.readLine(); - double amount = Double.parseDouble(br.readLine()); - boolean reoccuring = br.readLine().equals("Reoccurring"); - ExpenseCategory expenseCategory; - line = br.readLine(); - if (line.equals("CLOTHES") || line.equals("FOOD") || line.equals("BOOKS") || line.equals("OTHER")) { - expenseCategory = switch (line) { - case "CLOTHES" -> ExpenseCategory.CLOTHES; - case "FOOD" -> ExpenseCategory.FOOD; - case "BOOKS" -> ExpenseCategory.BOOKS; - default -> ExpenseCategory.OTHER; - }; - expenseRegister.add(new Expense(description,amount,reoccuring,expenseCategory,date)); + String nextLine = br.readLine(); + while ((line = nextLine) != null) { + nextLine = br.readLine(); + if (line.startsWith("date=")) { + date = line.replace("date=", ""); + } else if (line.startsWith("description=")) { + description = line.replace("description=", ""); + } else if (line.startsWith("amount=")) { + amount = Double.parseDouble(line.replace("amount=", "")); + } else if (line.startsWith("isReoccuring=")) { + reoccuring = line.replace("isReoccuring=", "").equals("Reoccurring"); + } else if (line.startsWith("category=")) { + line = line.replace("category=", ""); + expenseCategory = switch (line) { + case "FOOD" -> ExpenseCategory.FOOD; + case "CLOTHES" -> ExpenseCategory.CLOTHES; + case "BOOKS" -> ExpenseCategory.BOOKS; + default -> ExpenseCategory.OTHER; + }; + } + if (line.isEmpty() || (nextLine == null)) { + if (description.equals("")) { + expenseRegister.addItem(new Expense(amount, reoccuring, expenseCategory, date)); + } else { + expenseRegister.addItem(new Expense(description, amount, reoccuring, expenseCategory, date)); } + description = ""; } } } diff --git a/src/test/java/no/ntnu/idatt1002/demo/data/Economics/FileHandlingTest.java b/src/test/java/no/ntnu/idatt1002/demo/data/Economics/FileHandlingTest.java index a20d371de651cf238f1b4dd67100e5914ac39d30..70a524915c9110991609d7b9f0e1ee6b8d668786 100644 --- a/src/test/java/no/ntnu/idatt1002/demo/data/Economics/FileHandlingTest.java +++ b/src/test/java/no/ntnu/idatt1002/demo/data/Economics/FileHandlingTest.java @@ -8,19 +8,145 @@ import static org.junit.jupiter.api.Assertions.*; class FileHandlingTest { FileHandling fileHandling = new FileHandling(); - ItemRegister<Income> incomeRegister = new ItemRegister<>(); - - @Test - @DisplayName("Try to write to file an incomeRegister with one Item") - void writeIncomeRegisterToFile() throws IOException { - Income income1 = new Income("description1", 59.9f, false, IncomeCategory.SALARY, "03.03.23"); - Income income2 = new Income("description2", 62.4f, true, IncomeCategory.GIFT, "01.02.21"); - Income income3 = new Income("description3", 9.81f, false, IncomeCategory.SALARY, "05.07.23"); - incomeRegister.addItem(income1); - incomeRegister.addItem(income2); - incomeRegister.addItem(income3); - - fileHandling.writeItemRegisterToFile(incomeRegister, "income1"); - assertEquals(fileHandling.readIncomeRegisterFromFile("income1"),incomeRegister.getItems()); + ItemRegister<Expense> expenseRegister = new ItemRegister<>(); + ItemRegister<Income> incomeRegister = new ItemRegister<>(); + @Nested + @DisplayName("FileHandling IncomeRegister to file") + class fileHandlingIncomeRegisterToFile{ + String fileTitle = "incomeRegisterTest"; + @Nested + @DisplayName("FileHandling incomeRegister with income that has description") + class fileHandlingIncomeRegisterWithIncomeThatHasDescription{ + @BeforeEach + void addIncomeToIncomeRegister(){ + Income income1 = new Income("description", 59.9f, false, IncomeCategory.GIFT, "03.03.23"); + incomeRegister.addItem(income1); + } + @Test + @DisplayName("Writing to file does not throw exception") + void writingToFileDoesNotThrowException() throws IOException { + assertDoesNotThrow(()->fileHandling.writeItemRegisterToFile(incomeRegister, fileTitle)); + } + + @Test + @DisplayName("Reading from file gives correct incomeRegister back") + void readingFromFileGivesCorrectIncomeRegisterBack() throws IOException { + assertEquals(incomeRegister.toString(), fileHandling.readIncomeRegisterFromFile(fileTitle).toString()); + } + } + @Nested + @DisplayName("FileHandling incomeRegister with income that does not have description") + class fileHandlingIncomeRegisterWithIncomeThatDoesNotHaveDescription{ + @BeforeEach + void addIncomeToIncomeRegister(){ + Income income1 = new Income(59.9f, false, IncomeCategory.GIFT, "03.03.23"); + incomeRegister.addItem(income1); + } + @Test + @DisplayName("Writing to file does not throw exception") + void writingToFileDoesNotThrowException() throws IOException { + assertDoesNotThrow(()->fileHandling.writeItemRegisterToFile(incomeRegister, fileTitle)); + } + + @Test + @DisplayName("Reading from file gives correct incomeRegister back") + void readingFromFileGivesCorrectIncomeRegisterBack() throws IOException { + assertEquals(incomeRegister.toString(), fileHandling.readIncomeRegisterFromFile(fileTitle).toString()); + } + } + @Nested + @DisplayName("FileHandling incomeRegister with multiple Income") + class fileHandlingIncomeRegisterWithMultipleIncome{ + @BeforeEach + void addIncomeToIncomeRegister(){ + Income income1 = new Income(59.9f, false, IncomeCategory.GIFT, "03.03.23"); + Income income2 = new Income("description2", 62.4f, true, IncomeCategory.GIFT, "01.02.21"); + Income income3 = new Income("description3", 9.81f, false, IncomeCategory.SALARY, "05.07.23"); + incomeRegister.addItem(income1); + incomeRegister.addItem(income2); + incomeRegister.addItem(income3); + } + @Test + @DisplayName("Writing to file does not throw exception") + void writingToFileDoesNotThrowException() throws IOException { + assertDoesNotThrow(()->fileHandling.writeItemRegisterToFile(incomeRegister, fileTitle)); + } + + @Test + @DisplayName("Reading from file gives correct incomeRegister back") + void readingFromFileGivesCorrectIncomeRegisterBack() throws IOException { + assertEquals(incomeRegister.toString(), fileHandling.readIncomeRegisterFromFile(fileTitle).toString()); + } + } + } + @Nested + @DisplayName("FileHandling ExpenseRegister to file") + class fileHandlingExpenseRegisterToFile{ + String fileTitle = "expenseRegisterTest"; + @Nested + @DisplayName("FileHandling ExpenseRegister with Expense that has description") + class fileHandlingExpenseRegisterWithExpenseThatHasDescription{ + @BeforeEach + void addExpenseToExpenseRegister(){ + Expense expense1 = new Expense("description", 59.9f, false, ExpenseCategory.CLOTHES, "03.03.23"); + expenseRegister.addItem(expense1); + } + @Test + @DisplayName("Writing to file does not throw exception") + void writingToFileDoesNotThrowException() throws IOException { + assertDoesNotThrow(()->fileHandling.writeItemRegisterToFile(expenseRegister, fileTitle)); + } + + @Test + @DisplayName("Reading from file gives correct expenseRegister back") + void readingFromFileGivesCorrectIncomeRegisterBack() throws IOException { + assertEquals(expenseRegister.toString(), fileHandling.readExpenseRegisterFromFile(fileTitle).toString()); + } + } + @Nested + @DisplayName("FileHandling ExpenseRegister with Expense that does not have description") + class fileHandlingExpenseRegisterWithExpenseThatDoesNotHaveDescription{ + @BeforeEach + void addExpenseToExpenseRegister(){ + Expense expense1 = new Expense(59.9f, false, ExpenseCategory.CLOTHES, "03.03.23"); + expenseRegister.addItem(expense1); + } + @Test + @DisplayName("Writing to file does not throw exception") + void writingToFileDoesNotThrowException() throws IOException { + assertDoesNotThrow(()->fileHandling.writeItemRegisterToFile(expenseRegister, fileTitle)); + } + + @Test + @DisplayName("Reading from file gives correct expenseRegister back") + void readingFromFileGivesCorrectIncomeRegisterBack() throws IOException { + assertEquals(expenseRegister.toString(), fileHandling.readExpenseRegisterFromFile(fileTitle).toString()); + } + } + @Nested + @DisplayName("FileHandling ExpenseRegister with multiple Expense") + class fileHandlingExpenseRegisterWithMultipleExpensen{ + @BeforeEach + void addExpenseToExpenseRegister(){ + Expense expense1 = new Expense(59.9f, false, ExpenseCategory.CLOTHES, "03.03.23"); + Expense expense2 = new Expense("description2", 62.4f, true, ExpenseCategory.FOOD, "01.02.21"); + Expense expense3 = new Expense("description3", 9.81f, false, ExpenseCategory.CLOTHES, "05.07.23"); + + expenseRegister.addItem(expense1); + expenseRegister.addItem(expense2); + expenseRegister.addItem(expense3); + } + @Test + @DisplayName("Writing to file does not throw exception") + void writingToFileDoesNotThrowException() throws IOException { + assertDoesNotThrow(()->fileHandling.writeItemRegisterToFile(expenseRegister, fileTitle)); + } + + @Test + @DisplayName("Reading from file gives correct expenseRegister back") + void readingFromFileGivesCorrectIncomeRegisterBack() throws IOException { + assertEquals(expenseRegister.toString(), fileHandling.readExpenseRegisterFromFile(fileTitle).toString()); + } + } } } \ No newline at end of file