From 25e58e4413d049afd8ac525e0383aab2e19ab284 Mon Sep 17 00:00:00 2001 From: Tobias Herbert Date: Tue, 6 Feb 2024 21:17:21 +0100 Subject: [PATCH] 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; + } + }