Skip to content
Snippets Groups Projects
Commit 3cca4c9b authored by Andreas's avatar Andreas
Browse files

Made method removeItem in ItemRegister and tests

parent 9ca8782d
No related branches found
No related tags found
7 merge requests!21Add Javadoc to GeneralBudget, and reworked method addBudget,!16Item, Income and Expense now use ObjectProperty for date and for Expense- and IncomeCategory,!15Implemented ObjectProperty for variable in Item, Income and in Expense class.,!14Item made variable date use LocalDate, and all other files support this change,!13Item changed object-variable date to use LocalDate, and made all other classes support this,!12Reworked Item class and Filehandling class, and made methods.,!10Improved and simplified code in FileHandling class
Pipeline #209566 failed
......@@ -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;
......
......@@ -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,18 @@ 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 +49,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.
*
......
......@@ -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;
......
......@@ -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.*;
......
......@@ -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