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 {
}
}
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 = "";
}
}
}
......
......@@ -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
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