diff --git a/src/main/java/org/example/Student.java b/src/main/java/org/example/Student.java index 9708eb9..5c4afe7 100644 --- a/src/main/java/org/example/Student.java +++ b/src/main/java/org/example/Student.java @@ -1,4 +1,9 @@ package org.example; + +import java.io.*; +import java.util.ArrayList; +import java.util.List; + /** * Represents an student user in the system. */ @@ -61,4 +66,36 @@ public class Student { System.out.println("Role: " + getRole()); } + + public void writeToFile(Student student, String filename) throws IOException { + try(BufferedWriter writer = new BufferedWriter(new FileWriter(filename))){ + String attributes = student.getName() + "\n" + student.getId() + "\n" + student.getRole(); + writer.write(attributes); + } + + } + + public static List readFromFile(String filename) + { + List students = 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(); + + Student student = new Student(name, id, role); + students.add(student); + } + } catch (IOException e) { + e.printStackTrace(); + } + + return students; + } + + + } diff --git a/src/test/java/org/example/StudentTest.java b/src/test/java/org/example/StudentTest.java new file mode 100644 index 0000000..256b017 --- /dev/null +++ b/src/test/java/org/example/StudentTest.java @@ -0,0 +1,77 @@ +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 StudentTest { + + @org.junit.jupiter.api.Test + void writeToFile() throws IOException { + Student student = new Student("Sebastian", "S1001", "Student") ; + + String filename = "testStudentData.txt"; + + student.writeToFile(student, filename); + + Path path = Path.of(filename); + String content = Files.readString(path); + assertEquals("Sebastian\nS1001\nStudent", content); + + Files.delete(path); + + } + + + + + + @Test + void readFromFile() throws IOException { + + try (PrintWriter writer = new PrintWriter(new FileWriter("testStudentData.txt"))){ + writer.println("Samuel"); + writer.println("S1001"); + writer.print("Student"); + } catch (IOException e) { + e.printStackTrace(); + } + String filename = "testStudentData.txt"; + List testStudents = Student.readFromFile(filename); + + assertEquals(1, testStudents.size()); + Student admin = testStudents.get(0); + + assertEquals("Samuel", admin.getName()); + assertEquals("S1001", admin.getId()); + assertEquals("Student", admin.getRole()); + + Files.delete(Path.of(filename)); + + } + + + @Test + void printStudentInfo() { + Student student = new Student("Sebastian", "S1001", "Student"); + + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outputStream)); + + student.printStudentInfo(); + + String printedOutput = outputStream.toString().trim(); + + assertTrue(printedOutput.contains("Sebastian")); + assertTrue(printedOutput.contains("S1001")); + assertTrue(printedOutput.contains("Student")); + + } + + + +} \ No newline at end of file diff --git a/studentData.txt b/studentData.txt new file mode 100644 index 0000000..e69de29