From 92e3943d8befc79670ca4a21c67c7620a5434337 Mon Sep 17 00:00:00 2001 From: Tobias Herbert Date: Tue, 6 Feb 2024 20:59:08 +0100 Subject: [PATCH 1/8] Added adminData.txt File --- adminData.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 adminData.txt diff --git a/adminData.txt b/adminData.txt new file mode 100644 index 0000000..e69de29 From 7fa88263422b50e9e7ccea68801d39981bacef5a Mon Sep 17 00:00:00 2001 From: Tobias Herbert Date: Tue, 6 Feb 2024 21:05:18 +0100 Subject: [PATCH 2/8] Added writeToFile Method in Admin Class --- src/main/java/org/example/Admin.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/main/java/org/example/Admin.java b/src/main/java/org/example/Admin.java index abe8cbc..d7b4ff0 100644 --- a/src/main/java/org/example/Admin.java +++ b/src/main/java/org/example/Admin.java @@ -1,5 +1,9 @@ package org.example; +import java.io.BufferedWriter; +import java.io.FileWriter; +import java.io.IOException; + /** * Represents an administrative user in the system. */ @@ -53,4 +57,12 @@ public class Admin { System.out.println("ID: " + getId()); System.out.println("Role: " + getRole()); } + + public void writeToFile(Admin admin, String filename) throws IOException{ + try(BufferedWriter writer = new BufferedWriter(new FileWriter(filename))){ + String attributes = admin.getId() + "\n" + admin.getName() + "\n" + admin.getRole(); + writer.write(attributes); + } + + } } From 25e58e4413d049afd8ac525e0383aab2e19ab284 Mon Sep 17 00:00:00 2001 From: Tobias Herbert Date: Tue, 6 Feb 2024 21:17:21 +0100 Subject: [PATCH 3/8] Added readFromFile Method in Admin Class --- src/main/java/org/example/Admin.java | 30 ++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/example/Admin.java b/src/main/java/org/example/Admin.java index d7b4ff0..db20809 100644 --- a/src/main/java/org/example/Admin.java +++ b/src/main/java/org/example/Admin.java @@ -1,8 +1,8 @@ package org.example; -import java.io.BufferedWriter; -import java.io.FileWriter; -import java.io.IOException; +import java.io.*; +import java.util.ArrayList; +import java.util.List; /** * Represents an administrative user in the system. @@ -60,9 +60,31 @@ public class Admin { public void writeToFile(Admin admin, String filename) throws IOException{ try(BufferedWriter writer = new BufferedWriter(new FileWriter(filename))){ - String attributes = admin.getId() + "\n" + admin.getName() + "\n" + admin.getRole(); + String attributes = admin.getName() + "\n" + admin.getId() + "\n" + admin.getRole(); writer.write(attributes); } } + public static List readFromFile(String filename) + { + List admins = new ArrayList<>(); + + try (BufferedReader reader = new BufferedReader(new FileReader(filename))) { + String line; + while ((line = reader.readLine()) != null) { + String name = line; + String id = reader.readLine(); + String role = reader.readLine(); + + // Read any other fields you saved + Admin admin = new Admin(name, id, role); + admins.add(admin); + } + } catch (IOException e) { + e.printStackTrace(); + } + + return admins; + } + } From 0c9d4334f0200ffea662faf667b541f5275ade47 Mon Sep 17 00:00:00 2001 From: Tobias Herbert Date: Tue, 6 Feb 2024 21:21:03 +0100 Subject: [PATCH 4/8] documentation: readFromFile Method in Admin Class --- src/main/java/org/example/Admin.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/main/java/org/example/Admin.java b/src/main/java/org/example/Admin.java index db20809..f8ad460 100644 --- a/src/main/java/org/example/Admin.java +++ b/src/main/java/org/example/Admin.java @@ -58,6 +58,11 @@ public class Admin { System.out.println("Role: " + getRole()); } + /** + * + * @param admin the data of this admin is being written to the file + * @param filename the name of the file to write the data to + */ public void writeToFile(Admin admin, String filename) throws IOException{ try(BufferedWriter writer = new BufferedWriter(new FileWriter(filename))){ String attributes = admin.getName() + "\n" + admin.getId() + "\n" + admin.getRole(); @@ -65,6 +70,12 @@ public class Admin { } } + + /** + * + * @param filename the name of the file to read the data from + * @return the list of admins that have been read from the file + */ public static List readFromFile(String filename) { List admins = new ArrayList<>(); From 3f7c06bcff570513950858a94e3eac90069b2658 Mon Sep 17 00:00:00 2001 From: Tobias Herbert Date: Tue, 6 Feb 2024 21:28:57 +0100 Subject: [PATCH 5/8] Added Test to printAdminInfo Method from Admin Class --- pom.xml | 14 +++++++++++++ src/test/java/org/example/AdminTest.java | 26 ++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 src/test/java/org/example/AdminTest.java diff --git a/pom.xml b/pom.xml index 2d767b6..a8fb3e3 100644 --- a/pom.xml +++ b/pom.xml @@ -13,5 +13,19 @@ 19 UTF-8 + + + junit + junit + 4.13.1 + test + + + org.junit.jupiter + junit-jupiter + 5.8.1 + test + + \ No newline at end of file diff --git a/src/test/java/org/example/AdminTest.java b/src/test/java/org/example/AdminTest.java new file mode 100644 index 0000000..0c964fe --- /dev/null +++ b/src/test/java/org/example/AdminTest.java @@ -0,0 +1,26 @@ +package org.example; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; + +import static org.junit.jupiter.api.Assertions.*; + +class AdminTest { + + @org.junit.jupiter.api.Test + void printAdminInfo() { + Admin admin = new Admin("Aaron", "A1001", "Admin"); + + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outputStream)); + + admin.printAdminInfo(); + + String printedOutput = outputStream.toString().trim(); + + assertTrue(printedOutput.contains("Aaron")); + assertTrue(printedOutput.contains("A1001")); + assertTrue(printedOutput.contains("Admin")); + + } +} \ No newline at end of file From 7c2d302bf15dad1a81e67121112bfa72cb8ffb4e Mon Sep 17 00:00:00 2001 From: Tobias Herbert Date: Tue, 6 Feb 2024 21:37:58 +0100 Subject: [PATCH 6/8] Added Test to writeToFile Method from Admin Class --- src/test/java/org/example/AdminTest.java | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/test/java/org/example/AdminTest.java b/src/test/java/org/example/AdminTest.java index 0c964fe..51def04 100644 --- a/src/test/java/org/example/AdminTest.java +++ b/src/test/java/org/example/AdminTest.java @@ -1,7 +1,12 @@ package org.example; +import org.junit.jupiter.api.Test; + import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.io.PrintStream; +import java.nio.file.Files; +import java.nio.file.Path; import static org.junit.jupiter.api.Assertions.*; @@ -22,5 +27,23 @@ class AdminTest { assertTrue(printedOutput.contains("A1001")); assertTrue(printedOutput.contains("Admin")); + } + + @Test + void writeToFile() throws IOException { + Admin admin = new Admin("Simon", "A1001", "Admin") ; + + String filename = "testAdminData.txt"; + + admin.writeToFile(admin, filename); + + String content = Files.readString(Path.of(filename)); + assertEquals("Simon\nA1001\nAdmin", content); + + Files.delete(Path.of(filename)); + + + + } } \ No newline at end of file From 80af156e68c4cb71d83d56c4f4df76ce2769a333 Mon Sep 17 00:00:00 2001 From: Tobias Herbert Date: Tue, 6 Feb 2024 21:54:23 +0100 Subject: [PATCH 7/8] Added Test to readFromFile Method from Admin Class --- src/test/java/org/example/AdminTest.java | 26 +++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/test/java/org/example/AdminTest.java b/src/test/java/org/example/AdminTest.java index 51def04..f844ece 100644 --- a/src/test/java/org/example/AdminTest.java +++ b/src/test/java/org/example/AdminTest.java @@ -2,11 +2,10 @@ package org.example; import org.junit.jupiter.api.Test; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.PrintStream; +import java.io.*; import java.nio.file.Files; import java.nio.file.Path; +import java.util.List; import static org.junit.jupiter.api.Assertions.*; @@ -42,8 +41,29 @@ class AdminTest { Files.delete(Path.of(filename)); + } + + @Test + void readFromFile() throws IOException { + + try (PrintWriter writer = new PrintWriter(new FileWriter("testAdminData.txt"))){ + writer.println("Thomas"); + writer.println("A1001"); + writer.print("Admin"); + } catch (IOException e) { + e.printStackTrace(); + } + String filename = "testAdminData.txt"; + List testAdmins = Admin.readFromFile(filename); + + assertEquals(1, testAdmins.size()); + Admin admin = testAdmins.get(0); + assertEquals("Thomas", admin.getName()); + assertEquals("A1001", admin.getId()); + assertEquals("Admin", admin.getRole()); + Files.delete(Path.of(filename)); } } \ No newline at end of file From af237c01d275adbf259320173eb482013cf93de4 Mon Sep 17 00:00:00 2001 From: Tobias Herbert Date: Tue, 6 Feb 2024 22:00:54 +0100 Subject: [PATCH 8/8] refactoring: AdminTestClass --- src/test/java/org/example/AdminTest.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/example/AdminTest.java b/src/test/java/org/example/AdminTest.java index f844ece..e8fa2fa 100644 --- a/src/test/java/org/example/AdminTest.java +++ b/src/test/java/org/example/AdminTest.java @@ -36,10 +36,11 @@ class AdminTest { admin.writeToFile(admin, filename); - String content = Files.readString(Path.of(filename)); + Path path = Path.of(filename); + String content = Files.readString(path); assertEquals("Simon\nA1001\nAdmin", content); - Files.delete(Path.of(filename)); + Files.delete(path); }