diff --git a/adminData.txt b/adminData.txt new file mode 100644 index 0000000..e69de29 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/main/java/org/example/Admin.java b/src/main/java/org/example/Admin.java index abe8cbc..f8ad460 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.*; +import java.util.ArrayList; +import java.util.List; + /** * Represents an administrative user in the system. */ @@ -53,4 +57,45 @@ public class Admin { System.out.println("ID: " + getId()); 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(); + writer.write(attributes); + } + + } + + /** + * + * @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<>(); + + 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; + } + } diff --git a/src/test/java/org/example/AdminTest.java b/src/test/java/org/example/AdminTest.java new file mode 100644 index 0000000..e8fa2fa --- /dev/null +++ b/src/test/java/org/example/AdminTest.java @@ -0,0 +1,70 @@ +package org.example; + +import org.junit.jupiter.api.Test; + +import java.io.*; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; + +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")); + + } + + @Test + void writeToFile() throws IOException { + Admin admin = new Admin("Simon", "A1001", "Admin") ; + + String filename = "testAdminData.txt"; + + admin.writeToFile(admin, filename); + + Path path = Path.of(filename); + String content = Files.readString(path); + assertEquals("Simon\nA1001\nAdmin", content); + + Files.delete(path); + + } + + @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