Skip to content
Snippets Groups Projects
Commit 1f9b2a26 authored by Andreas's avatar Andreas
Browse files

Finished making FileHandling for IncomeRegister and ExpenseRegister and tests

parent 4bb1961f
No related branches found
No related tags found
2 merge requests!9Added FileHandling class for ItemRegister objects, and reworked ItemRegister with new subclasses,!8Remade ItemRegister class and made FileHandling for ItemRegister-objects with tests
Pipeline #208272 passed
...@@ -26,55 +26,85 @@ public class FileHandling { ...@@ -26,55 +26,85 @@ public class FileHandling {
} }
} }
public List<Income> readIncomeRegisterFromFile(String fileTitle) throws IOException { public ItemRegister<Income> readIncomeRegisterFromFile(String fileTitle) throws IOException {
ArrayList<Income> incomeRegister = new ArrayList<>(); 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"))) { try (BufferedReader br = new BufferedReader(new FileReader("src/main/resources/" + fileTitle + ".itemRegister"))) {
br.readLine();
String line; String line;
while ((line = br.readLine()) != null) { String nextLine = br.readLine();
if (!line.isEmpty()) { while ((line = nextLine) != null) {
String date = line; nextLine = br.readLine();
String description = br.readLine(); if(line.startsWith("date=")) {
double amount = Double.parseDouble(br.readLine()); date = line.replace("date=", "");
boolean reoccuring = br.readLine().equals("Reoccurring"); } else if (line.startsWith("description=")) {
IncomeCategory incomeCategory; description = line.replace("description=","");
line = br.readLine(); } else if (line.startsWith("amount=")) {
if (line.equals("SALARY") || line.equals("STUDENT_LOAN") || line.equals("GIFT")) { amount = Double.parseDouble(line.replace("amount=",""));
if (line.equals("SALARY")) { } else if (line.startsWith("isReoccuring=")) {
incomeCategory = IncomeCategory.SALARY; reoccuring = line.replace("isReoccuring=","").equals("Reoccurring");
} else if (line.equals("STUDENT_LOAN")) { } else if (line.startsWith("category=")) {
incomeCategory = IncomeCategory.STUDENT_LOAN; line = line.replace("category=","");
} else { incomeCategory = switch (line) {
incomeCategory = IncomeCategory.GIFT; case "GIFT" -> IncomeCategory.GIFT;
} case "STUDENT_LOAN" -> IncomeCategory.STUDENT_LOAN;
incomeRegister.add(new Income(description,amount,reoccuring,incomeCategory,date)); 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; return incomeRegister;
} }
public List<Expense> readExpenseRegisterFromFile(String fileTitle) throws IOException { public ItemRegister<Expense> readExpenseRegisterFromFile(String fileTitle) throws IOException {
ArrayList<Expense> expenseRegister = new ArrayList<>(); 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"))) { try (BufferedReader br = new BufferedReader(new FileReader("src/main/resources/" + fileTitle + ".itemRegister"))) {
br.readLine();
String line; String line;
while ((line = br.readLine()) != null) { String nextLine = br.readLine();
if (!line.isEmpty()) { while ((line = nextLine) != null) {
String date = line; nextLine = br.readLine();
String description = br.readLine(); if (line.startsWith("date=")) {
double amount = Double.parseDouble(br.readLine()); date = line.replace("date=", "");
boolean reoccuring = br.readLine().equals("Reoccurring"); } else if (line.startsWith("description=")) {
ExpenseCategory expenseCategory; description = line.replace("description=", "");
line = br.readLine(); } else if (line.startsWith("amount=")) {
if (line.equals("CLOTHES") || line.equals("FOOD") || line.equals("BOOKS") || line.equals("OTHER")) { amount = Double.parseDouble(line.replace("amount=", ""));
expenseCategory = switch (line) { } else if (line.startsWith("isReoccuring=")) {
case "CLOTHES" -> ExpenseCategory.CLOTHES; reoccuring = line.replace("isReoccuring=", "").equals("Reoccurring");
case "FOOD" -> ExpenseCategory.FOOD; } else if (line.startsWith("category=")) {
case "BOOKS" -> ExpenseCategory.BOOKS; line = line.replace("category=", "");
default -> ExpenseCategory.OTHER; expenseCategory = switch (line) {
}; case "FOOD" -> ExpenseCategory.FOOD;
expenseRegister.add(new Expense(description,amount,reoccuring,expenseCategory,date)); 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 = "";
} }
} }
} }
......
...@@ -8,19 +8,145 @@ import static org.junit.jupiter.api.Assertions.*; ...@@ -8,19 +8,145 @@ import static org.junit.jupiter.api.Assertions.*;
class FileHandlingTest { class FileHandlingTest {
FileHandling fileHandling = new FileHandling(); FileHandling fileHandling = new FileHandling();
ItemRegister<Income> incomeRegister = new ItemRegister<>(); ItemRegister<Expense> expenseRegister = new ItemRegister<>();
ItemRegister<Income> incomeRegister = new ItemRegister<>();
@Test @Nested
@DisplayName("Try to write to file an incomeRegister with one Item") @DisplayName("FileHandling IncomeRegister to file")
void writeIncomeRegisterToFile() throws IOException { class fileHandlingIncomeRegisterToFile{
Income income1 = new Income("description1", 59.9f, false, IncomeCategory.SALARY, "03.03.23"); String fileTitle = "incomeRegisterTest";
Income income2 = new Income("description2", 62.4f, true, IncomeCategory.GIFT, "01.02.21"); @Nested
Income income3 = new Income("description3", 9.81f, false, IncomeCategory.SALARY, "05.07.23"); @DisplayName("FileHandling incomeRegister with income that has description")
incomeRegister.addItem(income1); class fileHandlingIncomeRegisterWithIncomeThatHasDescription{
incomeRegister.addItem(income2); @BeforeEach
incomeRegister.addItem(income3); void addIncomeToIncomeRegister(){
Income income1 = new Income("description", 59.9f, false, IncomeCategory.GIFT, "03.03.23");
fileHandling.writeItemRegisterToFile(incomeRegister, "income1"); incomeRegister.addItem(income1);
assertEquals(fileHandling.readIncomeRegisterFromFile("income1"),incomeRegister.getItems()); }
@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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment