Skip to content
Snippets Groups Projects
Commit 2b9b9af0 authored by Andreas Kluge Svendsrud's avatar Andreas Kluge Svendsrud
Browse files

Merge branch 'FileHandlingAndreas' into 'master'

Improved and simplified code in FileHandling class

See merge request !10
parents 88f1e105 385a0861
No related branches found
No related tags found
1 merge request!10Improved and simplified code in FileHandling class
Pipeline #210286 passed
Showing
with 162 additions and 81 deletions
......@@ -66,6 +66,6 @@ public class Expense extends Item{
*/
@Override
public String toString() {
return super.toString()+"\ncategory="+category.toString()+"\n";
return super.toString()+"category="+category.toString()+"\n\n";
}
}
......@@ -10,7 +10,15 @@ import java.io.*;
* FileHandling is a class for writing and reading
* Item-objects to and from a file.
*/
public class FileHandling {
public class FileHandling{
private static final String filePath = "src/main/resources/Economics/";
private static final String fileType = ".register";
private static final String date = "date=";
private static final String description = "description=";
private static final String amount = "amount=";
private static final String isReoccuring = "isReoccuring=";
private static final String category = "category=";
/**
* Method for writing (adding) an ItemRegister to a file.
*
......@@ -18,36 +26,42 @@ public class FileHandling {
* @throws IOException if an input or output exception occurred.
*/
public <T extends Item>void writeItemRegisterToFile(final ItemRegister<T> itemRegister, String fileTitle) throws IOException {
try (BufferedWriter bw = new BufferedWriter(new FileWriter("src/main/resources/" + fileTitle + ".itemRegister"))) {
try (BufferedWriter bw = new BufferedWriter(new FileWriter(filePath + fileTitle + fileType))) {
bw.write(itemRegister.toString());
} catch (IOException ex) {
throw new IOException("Error writing story to file: " + ex.getMessage());
}
}
public ItemRegister<Income> readIncomeRegisterFromFile(String fileTitle) throws IOException {
/**
* Method for reading (getting) an IncomeRegister from a file.
*
* @param fileTitle the name of the file you want to read from
* @return the IncomeRegister from the file.
* @throws IOException if an input or output exception occurred
*/
public IncomeRegister readIncomeRegisterFromFile(String fileTitle) throws IOException {
IncomeRegister incomeRegister = new IncomeRegister();
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();
try (BufferedReader br = new BufferedReader(new FileReader(filePath + fileTitle + fileType))) {
String line;
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=","");
if(line.startsWith(FileHandling.date)) {
date = line.replace(FileHandling.date, "");
} else if (line.startsWith(FileHandling.description)) {
description = line.replace(FileHandling.description,"");
} else if (line.startsWith(FileHandling.amount)) {
amount = Double.parseDouble(line.replace(FileHandling.amount,""));
} else if (line.startsWith(FileHandling.isReoccuring)) {
reoccuring = line.replace(FileHandling.isReoccuring,"").equals("Reoccurring");
} else if (line.startsWith(FileHandling.category)) {
line = line.replace(FileHandling.category,"");
incomeCategory = switch (line) {
case "GIFT" -> IncomeCategory.GIFT;
case "STUDENT_LOAN" -> IncomeCategory.STUDENT_LOAN;
......@@ -67,29 +81,35 @@ public class FileHandling {
return incomeRegister;
}
public ItemRegister<Expense> readExpenseRegisterFromFile(String fileTitle) throws IOException {
/**
* Method for reading (getting) an ExpenseRegister from a file.
*
* @param fileTitle the name of the file you want to read from.
* @return the ExpenseRegister from the file.
* @throws IOException if an input or output exception occurred
*/
public ExpenseRegister readExpenseRegisterFromFile(String fileTitle) throws IOException {
ExpenseRegister expenseRegister = new ExpenseRegister();
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();
try (BufferedReader br = new BufferedReader(new FileReader(filePath + fileTitle + fileType))) {
String line;
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=", "");
if (line.startsWith(FileHandling.date)) {
date = line.replace(FileHandling.date, "");
} else if (line.startsWith(FileHandling.description)) {
description = line.replace(FileHandling.description, "");
} else if (line.startsWith(FileHandling.amount)) {
amount = Double.parseDouble(line.replace(FileHandling.amount, ""));
} else if (line.startsWith(FileHandling.isReoccuring)) {
reoccuring = line.replace(FileHandling.isReoccuring, "").equals("Reoccurring");
} else if (line.startsWith(FileHandling.category)) {
line = line.replace(FileHandling.category, "");
expenseCategory = switch (line) {
case "FOOD" -> ExpenseCategory.FOOD;
case "CLOTHES" -> ExpenseCategory.CLOTHES;
......
......@@ -73,6 +73,6 @@ public class Income extends Item{
*/
@Override
public String toString() {
return super.toString()+"\ncategory="+category.toString()+"\n";
return super.toString()+"category="+category.toString()+"\n\n";
}
}
......@@ -27,6 +27,7 @@ public class IncomeRegister extends ItemRegister<Income>{
/**
* Method for getting every Income
* in a given IncomeCategory.
*
* @param category the IncomeCategory you want to get every Income of.
* @return a List of every Income with category.
*/
......
......@@ -138,10 +138,10 @@ public abstract class Item {
isReoccuring = "Not reoccurring";
}
if(!description.isBlank()){
return "\ndate=" + date+"\ndescription=" + description+"\namount="+amount+"\nisReoccuring="+isReoccuring;
return "date=" + date+"\ndescription=" + description+"\namount="+amount+"\nisReoccuring="+isReoccuring+"\n";
}
else{
return "\ndate="+date+"\namount="+amount+"\nisReoccuring="+isReoccuring;
return "date="+date+"\namount="+amount+"\nisReoccuring="+isReoccuring+"\n";
}
}
......
......@@ -13,7 +13,7 @@ public abstract class ItemRegister<T extends Item>{
List<T> items;
/**
* Class constructor that creates an empty List for storing T=(Income or Expense).
* Class constructor that creates an empty List for storing item´s.
*/
public ItemRegister() {
this.items = new ArrayList<>();
......@@ -29,19 +29,19 @@ public abstract class ItemRegister<T extends Item>{
}
/**
* Get a List of every T=(Income or Expense).
* @return T=(Income or Expense) List.
* Get a List of every item.
*
* @return item List.
*/
public List<T> getItems() {
return items;
}
/**
* Add a new T=(Income or Expense) to the register. Throws IllegalArgumentException
* if the new T=(Income or Expense) is already registered.
* Add a new item to the register.
*
* @param newItem the T=(Income or Expense) you want to add.
* @throws IllegalArgumentException if the item is already in the register.
* @param newItem the item you want to add.
* @throws IllegalArgumentException if newItem is already in the register.
*/
public void addItem(T newItem) throws IllegalArgumentException{
if(items.contains(newItem)){
......@@ -50,6 +50,18 @@ public abstract class ItemRegister<T extends Item>{
items.add(newItem);
}
/**
* Remove an item from the register.
*
* @param itemWantRemoved the item you want to remove.
* @throws IllegalArgumentException if the itemWantRemoved is not in the register.
*/
public void removeItem(T itemWantRemoved) throws IllegalArgumentException{
if(!items.remove(itemWantRemoved)){
throw new IllegalArgumentException("The item is not in the register");
}
}
/**
* Check if items is empty.
*
......
date=03.03.23
description=description
amount=59.900001525878906
isReoccuring=Not reoccurring
category=CLOTHES
date=03.03.23
description=description
amount=59.900001525878906
isReoccuring=Not reoccurring
category=GIFT
......@@ -12,25 +12,51 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
class ExpenseRegisterTest {
ExpenseRegister expenseRegister = new ExpenseRegister();
@Test
@DisplayName("addItem method throws exception when it should")
void addItemThrows() {
Expense expense = new Expense("description", 59.9f, false, ExpenseCategory.CLOTHES, "03.03.23");
expenseRegister.addItem(expense);
assertThrows(IllegalArgumentException.class, () -> expenseRegister.addItem(expense));
@Nested
@DisplayName("Test addItem")
class testAddItem {
@Test
@DisplayName("addItem method throws exception when it should")
void addItemThrows() {
Expense expense = new Expense("description", 59.9f, false, ExpenseCategory.CLOTHES, "03.03.23");
expenseRegister.addItem(expense);
assertThrows(IllegalArgumentException.class, () -> expenseRegister.addItem(expense));
}
@Test
@DisplayName("addItem method does not throw exception when it should not")
void addItemDoesNotThrow() {
Expense expense1 = new Expense("description", 59.9f, false, ExpenseCategory.CLOTHES, "03.03.23");
Expense expense2 = new Expense("anotherDescription", 6.5f, true, ExpenseCategory.BOOKS, "02.03.23");
assertDoesNotThrow(() -> expenseRegister.addItem(expense1));
assertDoesNotThrow(() -> expenseRegister.addItem(expense2));
}
}
@Test
@DisplayName("addItem method does not throw exception when it should not")
void addItemDoesNotThrow() {
Expense expense1 = new Expense("description", 59.9f, false, ExpenseCategory.CLOTHES, "03.03.23");
Expense expense2 = new Expense("anotherDescription", 6.5f, true, ExpenseCategory.BOOKS, "02.03.23");
assertDoesNotThrow(() -> expenseRegister.addItem(expense1));
assertDoesNotThrow(() -> expenseRegister.addItem(expense2));
@Nested
@DisplayName("Test removeItem")
class testRemoveItem{
@Test
@DisplayName("removeItem does throw exception when it should")
void removeItemDoesThrow(){
Expense expense1 = new Expense("description", 59.9f, false, ExpenseCategory.CLOTHES, "03.03.23");
Expense notExpense1 = new Expense("anotherDescription", 6.5f, true, ExpenseCategory.BOOKS, "02.03.23");
expenseRegister.addItem(expense1);
assertThrows(IllegalArgumentException.class, () -> expenseRegister.removeItem(notExpense1));
}
@Test
@DisplayName("removeItem method does not throw exception when it should not")
void removeItemDoesNotThrow(){
Expense expense1 = new Expense("description", 59.9f, false, ExpenseCategory.CLOTHES, "03.03.23");
Expense expense1Copy = new Expense("description", 59.9f, false, ExpenseCategory.CLOTHES, "03.03.23");
expenseRegister.addItem(expense1);
assertDoesNotThrow(() -> expenseRegister.removeItem(expense1Copy));
}
}
@Nested
@DisplayName("Test getTotalSum and getExpenseByCategory methods")
@DisplayName("Test getTotalSum and getExpenseByCategory")
class testGetExpenseByCategoryMethod {
Expense expense1;
Expense expense2;
......
package no.ntnu.idatt1002.demo.data.Economics;
import no.ntnu.idatt1002.demo.data.Economics.Expense;
import no.ntnu.idatt1002.demo.data.Economics.ExpenseCategory;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
......@@ -14,7 +12,7 @@ class ExpenseTest {
void constructorThrows(){
assertThrows(IllegalArgumentException.class, () -> new Expense("description", 8.5f, false, null, "03.03.23"));
assertThrows(IllegalArgumentException.class, () -> new Expense("description", -10.0f, false, ExpenseCategory.BOOKS, "03.03.23"));
};
}
@Test
@DisplayName("The Expense constructor does not throw exceptions when it should not.")
......
......@@ -2,7 +2,6 @@ package no.ntnu.idatt1002.demo.data.Economics;
import org.junit.jupiter.api.*;
import java.io.FileNotFoundException;
import java.io.IOException;
import static org.junit.jupiter.api.Assertions.*;
......@@ -24,7 +23,7 @@ class FileHandlingTest {
}
@Test
@DisplayName("Writing to file does not throw exception")
void writingToFileDoesNotThrowException() throws IOException {
void writingToFileDoesNotThrowException() {
assertDoesNotThrow(()->fileHandling.writeItemRegisterToFile(incomeRegister, fileTitle));
}
......@@ -44,7 +43,7 @@ class FileHandlingTest {
}
@Test
@DisplayName("Writing to file does not throw exception")
void writingToFileDoesNotThrowException() throws IOException {
void writingToFileDoesNotThrowException() {
assertDoesNotThrow(()->fileHandling.writeItemRegisterToFile(incomeRegister, fileTitle));
}
......@@ -68,7 +67,7 @@ class FileHandlingTest {
}
@Test
@DisplayName("Writing to file does not throw exception")
void writingToFileDoesNotThrowException() throws IOException {
void writingToFileDoesNotThrowException() {
assertDoesNotThrow(()->fileHandling.writeItemRegisterToFile(incomeRegister, fileTitle));
}
......@@ -93,7 +92,7 @@ class FileHandlingTest {
}
@Test
@DisplayName("Writing to file does not throw exception")
void writingToFileDoesNotThrowException() throws IOException {
void writingToFileDoesNotThrowException() {
assertDoesNotThrow(()->fileHandling.writeItemRegisterToFile(expenseRegister, fileTitle));
}
......@@ -113,7 +112,7 @@ class FileHandlingTest {
}
@Test
@DisplayName("Writing to file does not throw exception")
void writingToFileDoesNotThrowException() throws IOException {
void writingToFileDoesNotThrowException() {
assertDoesNotThrow(()->fileHandling.writeItemRegisterToFile(expenseRegister, fileTitle));
}
......@@ -138,7 +137,7 @@ class FileHandlingTest {
}
@Test
@DisplayName("Writing to file does not throw exception")
void writingToFileDoesNotThrowException() throws IOException {
void writingToFileDoesNotThrowException() {
assertDoesNotThrow(()->fileHandling.writeItemRegisterToFile(expenseRegister, fileTitle));
}
......
......@@ -11,27 +11,52 @@ import static org.junit.jupiter.api.Assertions.*;
public class IncomeRegisterTest {
IncomeRegister incomeRegister = new IncomeRegister();
@Test
@DisplayName("addItem method throws exception when it should")
void addItemThrows() {
Income income = new Income("description", 59.9f, false, IncomeCategory.SALARY, "03.03.23");
incomeRegister.addItem(income);
assertThrows(IllegalArgumentException.class, () -> incomeRegister.addItem(income));
}
@Test
@DisplayName("addItem method does not throw exception when it should not")
void addItemDoesNotThrow() {
Income income1 = new Income("description", 59.9f, false, IncomeCategory.GIFT, "03.03.23");
Income income2 = new Income("anotherDescription", 6.5f, true, IncomeCategory.SALARY, "02.03.23");
assertDoesNotThrow(() -> incomeRegister.addItem(income1));
assertDoesNotThrow(() -> incomeRegister.addItem(income2));
@Nested
@DisplayName("Test addItem")
class testAddItem{
@Test
@DisplayName("addItem method throws exception when it should")
void addItemThrows() {
Income income = new Income("description", 59.9f, false, IncomeCategory.SALARY, "03.03.23");
incomeRegister.addItem(income);
assertThrows(IllegalArgumentException.class, () -> incomeRegister.addItem(income));
}
@Test
@DisplayName("addItem method does not throw exception when it should not")
void addItemDoesNotThrow() {
Income income1 = new Income("description", 59.9f, false, IncomeCategory.GIFT, "03.03.23");
Income income2 = new Income("anotherDescription", 6.5f, true, IncomeCategory.SALARY, "02.03.23");
assertDoesNotThrow(() -> incomeRegister.addItem(income1));
assertDoesNotThrow(() -> incomeRegister.addItem(income2));
}
}
@Nested
@DisplayName("test getTotalSum and getIncomeByCategory methods")
class testGetTotalSumAndGetIncomeByCategoryMethods {
@DisplayName("Test removeItem")
class testRemoveItem{
@Test
@DisplayName("removeItem does throw exception when it should")
void removeItemDoesThrow(){
Income income1 = new Income("description", 59.9f, false, IncomeCategory.GIFT, "03.03.23");
Income notIncome1 = new Income("anotherDescription", 6.5f, true, IncomeCategory.SALARY, "02.03.23");
incomeRegister.addItem(income1);
assertThrows(IllegalArgumentException.class, () -> incomeRegister.removeItem(notIncome1));
}
@Test
@DisplayName("removeItem method does not throw exception when it should not")
void removeItemDoesNotThrow(){
Income income1 = new Income("description", 59.9f, false, IncomeCategory.GIFT, "03.03.23");
Income income1Copy = new Income("description", 59.9f, false, IncomeCategory.GIFT, "03.03.23");
incomeRegister.addItem(income1);
assertDoesNotThrow(() -> incomeRegister.removeItem(income1Copy));
}
}
@Nested
@DisplayName("Test getTotalSum and getIncomeByCategory")
class testGetTotalSumAndGetIncomeByCategoryMethods {
Income income1;
Income income2;
Income income3;
......
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