From 56a6e5e60db82890468b8f95852eb2eeb025b095 Mon Sep 17 00:00:00 2001 From: fdai7887 Date: Tue, 6 Feb 2024 23:21:00 +0100 Subject: [PATCH 1/6] Added studentData.txt file --- studentData.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 studentData.txt diff --git a/studentData.txt b/studentData.txt new file mode 100644 index 0000000..e69de29 From dda4d4f83b6181dcd8cbbce9a51f730c227fb521 Mon Sep 17 00:00:00 2001 From: fdai7887 Date: Tue, 6 Feb 2024 23:27:14 +0100 Subject: [PATCH 2/6] Added writeToFile Method to Student class --- src/main/java/org/example/Student.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/main/java/org/example/Student.java b/src/main/java/org/example/Student.java index 9708eb9..1f2f009 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.BufferedWriter; +import java.io.FileWriter; +import java.io.IOException; + /** * Represents an student user in the system. */ @@ -61,4 +66,14 @@ 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); + } + + } + + } From 10448e473d4da77e6de64877ddf84cf16ed8a314 Mon Sep 17 00:00:00 2001 From: fdai7887 Date: Tue, 6 Feb 2024 23:43:03 +0100 Subject: [PATCH 3/6] Added readFromFile Method to Student class --- src/main/java/org/example/Student.java | 28 +++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/example/Student.java b/src/main/java/org/example/Student.java index 1f2f009..5c4afe7 100644 --- a/src/main/java/org/example/Student.java +++ b/src/main/java/org/example/Student.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 student user in the system. @@ -75,5 +75,27 @@ public class Student { } + 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; + } + + } From 0a98db83cd8d6431e4df6e15b112cdb36bc9003a Mon Sep 17 00:00:00 2001 From: fdai7887 Date: Tue, 6 Feb 2024 23:53:42 +0100 Subject: [PATCH 4/6] Added StudentTest.java file and added test for writeToFile Method for Student class --- src/test/java/org/example/StudentTest.java | 28 ++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/test/java/org/example/StudentTest.java diff --git a/src/test/java/org/example/StudentTest.java b/src/test/java/org/example/StudentTest.java new file mode 100644 index 0000000..e97e7d0 --- /dev/null +++ b/src/test/java/org/example/StudentTest.java @@ -0,0 +1,28 @@ +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); + + } +} \ No newline at end of file From 2496df464cd13f9aba887e915e842a6b84c25ceb Mon Sep 17 00:00:00 2001 From: fdai7887 Date: Tue, 6 Feb 2024 23:59:42 +0100 Subject: [PATCH 5/6] Added test for readFromFile to StudentTest class --- src/test/java/org/example/StudentTest.java | 28 ++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/test/java/org/example/StudentTest.java b/src/test/java/org/example/StudentTest.java index e97e7d0..89d4d40 100644 --- a/src/test/java/org/example/StudentTest.java +++ b/src/test/java/org/example/StudentTest.java @@ -25,4 +25,32 @@ class StudentTest { 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)); + + } + + + + } \ No newline at end of file From 268bac6c43461e1662d172d8702db7a64745341c Mon Sep 17 00:00:00 2001 From: fdai7887 Date: Wed, 7 Feb 2024 00:15:40 +0100 Subject: [PATCH 6/6] Added test for printStudentInfo Method for Student class --- src/test/java/org/example/StudentTest.java | 23 +++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/test/java/org/example/StudentTest.java b/src/test/java/org/example/StudentTest.java index 89d4d40..256b017 100644 --- a/src/test/java/org/example/StudentTest.java +++ b/src/test/java/org/example/StudentTest.java @@ -26,7 +26,11 @@ class StudentTest { } -@Test + + + + + @Test void readFromFile() throws IOException { try (PrintWriter writer = new PrintWriter(new FileWriter("testStudentData.txt"))){ @@ -51,6 +55,23 @@ class StudentTest { } + @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