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

Made FileHandling methods static

parent 446f48a0
No related branches found
No related tags found
2 merge requests!43Merging frontend-testing into master,!39GeneralBudget set BudgetPeriod to be days left of month, and made FileHandling methods static.
Pipeline #215955 passed
......@@ -29,7 +29,7 @@ public class FileHandling{
* @param fileTitle the name of the file you want to check
* @throws IOException if an input or output exception occurred.
*/
public <T extends Item>void writeItemRegisterToFile(final ItemRegister<T> itemRegister, String fileTitle) throws IOException {
public static <T extends Item>void writeItemRegisterToFile(final ItemRegister<T> itemRegister, String fileTitle) throws IOException {
try (BufferedWriter bw = new BufferedWriter(new FileWriter(filePath + fileTitle + fileType))) {
if (itemRegister.isEmpty()){
bw.write("");
......@@ -48,7 +48,7 @@ public class FileHandling{
* @return true or false depending on if file is empty.
* @throws IOException if an input or output exception occurred.
*/
public boolean isEmpty(String fileTitle) throws IOException {
public static boolean isEmpty(String fileTitle) throws IOException {
try (BufferedReader br = new BufferedReader(new FileReader(filePath + fileTitle + fileType))) {
if (br.readLine() == null) {
return true;
......@@ -65,7 +65,7 @@ public class FileHandling{
* @return the IncomeRegister from the file.
* @throws IOException if an input or output exception occurred.
*/
public IncomeRegister readIncomeRegisterFromFile(String fileTitle) throws IOException {
public static IncomeRegister readIncomeRegisterFromFile(String fileTitle) throws IOException {
IncomeRegister incomeRegister = new IncomeRegister();
LocalDate date = null;
String description = "";
......@@ -112,7 +112,7 @@ public class FileHandling{
* @return the ExpenseRegister from the file.
* @throws IOException if an input or output exception occurred
*/
public ExpenseRegister readExpenseRegisterFromFile(String fileTitle) throws IOException {
public static ExpenseRegister readExpenseRegisterFromFile(String fileTitle) throws IOException {
ExpenseRegister expenseRegister = new ExpenseRegister();
LocalDate date = null;
String description = "";
......
......@@ -9,7 +9,6 @@ import java.time.Month;
import static org.junit.jupiter.api.Assertions.*;
class FileHandlingTest {
FileHandling fileHandling = new FileHandling();
ExpenseRegister expenseRegister = new ExpenseRegister();
IncomeRegister incomeRegister = new IncomeRegister();
......@@ -19,8 +18,8 @@ class FileHandlingTest {
@Test
@DisplayName("isEmpty returns true if a file is empty")
void isEmptyReturnsTrueIfAFileIsEmpty() throws IOException {
fileHandling.writeItemRegisterToFile(incomeRegister, "incomeRegisterTest");
assertTrue(fileHandling.isEmpty("incomeRegisterTest"));
FileHandling.writeItemRegisterToFile(incomeRegister, "incomeRegisterTest");
assertTrue(FileHandling.isEmpty("incomeRegisterTest"));
}
@Test
......@@ -28,8 +27,8 @@ class FileHandlingTest {
void isEmptyReturnsFalseIfFileIsNotEmpty() throws IOException {
Income income1 = new Income("description", 59.9f, false, IncomeCategory.GIFT, LocalDate.of(2023, Month.MARCH, 3));
incomeRegister.addItem(income1);
fileHandling.writeItemRegisterToFile(incomeRegister, "incomeRegisterTest");
assertFalse(fileHandling.isEmpty("incomeRegisterTest"));
FileHandling.writeItemRegisterToFile(incomeRegister, "incomeRegisterTest");
assertFalse(FileHandling.isEmpty("incomeRegisterTest"));
}
}
@Nested
......@@ -47,13 +46,13 @@ class FileHandlingTest {
@Test
@DisplayName("Writing to file does not throw exception")
void writingToFileDoesNotThrowException() {
assertDoesNotThrow(()->fileHandling.writeItemRegisterToFile(incomeRegister, fileTitle));
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());
assertEquals(incomeRegister.toString(), FileHandling.readIncomeRegisterFromFile(fileTitle).toString());
}
}
@Nested
......@@ -67,13 +66,13 @@ class FileHandlingTest {
@Test
@DisplayName("Writing to file does not throw exception")
void writingToFileDoesNotThrowException() {
assertDoesNotThrow(()->fileHandling.writeItemRegisterToFile(incomeRegister, fileTitle));
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());
assertEquals(incomeRegister.toString(), FileHandling.readIncomeRegisterFromFile(fileTitle).toString());
}
}
@Nested
......@@ -91,13 +90,13 @@ class FileHandlingTest {
@Test
@DisplayName("Writing to file does not throw exception")
void writingToFileDoesNotThrowException() {
assertDoesNotThrow(()->fileHandling.writeItemRegisterToFile(incomeRegister, fileTitle));
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());
assertEquals(incomeRegister.toString(), FileHandling.readIncomeRegisterFromFile(fileTitle).toString());
}
}
}
......@@ -116,13 +115,13 @@ class FileHandlingTest {
@Test
@DisplayName("Writing to file does not throw exception")
void writingToFileDoesNotThrowException() {
assertDoesNotThrow(()->fileHandling.writeItemRegisterToFile(expenseRegister, fileTitle));
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());
assertEquals(expenseRegister.toString(), FileHandling.readExpenseRegisterFromFile(fileTitle).toString());
}
}
@Nested
......@@ -136,13 +135,13 @@ class FileHandlingTest {
@Test
@DisplayName("Writing to file does not throw exception")
void writingToFileDoesNotThrowException() {
assertDoesNotThrow(()->fileHandling.writeItemRegisterToFile(expenseRegister, fileTitle));
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());
assertEquals(expenseRegister.toString(), FileHandling.readExpenseRegisterFromFile(fileTitle).toString());
}
}
@Nested
......@@ -161,13 +160,13 @@ class FileHandlingTest {
@Test
@DisplayName("Writing to file does not throw exception")
void writingToFileDoesNotThrowException() {
assertDoesNotThrow(()->fileHandling.writeItemRegisterToFile(expenseRegister, fileTitle));
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());
assertEquals(expenseRegister.toString(), FileHandling.readExpenseRegisterFromFile(fileTitle).toString());
}
}
}
......
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