Skip to content
Snippets Groups Projects
Commit 0d237107 authored by Andreas's avatar Andreas
Browse files

Made readExpenseRegisterFromFile method

parent d2d9cd79
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
......@@ -28,19 +28,15 @@ public class FileHandling {
public List<Income> readIncomeRegisterFromFile(String fileTitle) throws IOException {
ArrayList<Income> incomeRegister = new ArrayList<>();
String description = "";
double amount = 0;
boolean reoccuring;
String date = "";
IncomeCategory incomeCategory;
try (BufferedReader br = new BufferedReader(new FileReader("src/main/resources/" + fileTitle + ".itemRegister"))) {
String line;
while ((line = br.readLine()) != null) {
if (!line.isEmpty()) {
date = line;
description = br.readLine();
amount = Double.parseDouble(br.readLine());
reoccuring = br.readLine().equals("Reoccurring");
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")) {
......@@ -57,4 +53,31 @@ public class FileHandling {
}
return incomeRegister;
}
public List<Expense> readExpenseRegisterFromFile(String fileTitle) throws IOException {
ArrayList<Expense> expenseRegister = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader("src/main/resources/" + fileTitle + ".itemRegister"))) {
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));
}
}
}
}
return expenseRegister;
}
}
\ 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