From 4932036ebd853b9b075f974315bc4fecc028872b Mon Sep 17 00:00:00 2001 From: fdai7921 Date: Tue, 6 Feb 2024 23:10:47 +0100 Subject: [PATCH] Adding readFromFile method in Course class --- src/main/java/org/example/Course.java | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/main/java/org/example/Course.java b/src/main/java/org/example/Course.java index b294177..74a5f38 100644 --- a/src/main/java/org/example/Course.java +++ b/src/main/java/org/example/Course.java @@ -1,5 +1,7 @@ package org.example; import java.io.*; +import java.util.List; +import java.util.ArrayList; @@ -111,4 +113,25 @@ public class Course { } } + public static List readFromFile(String filename) { + List courses = new ArrayList<>(); + + try (BufferedReader reader = new BufferedReader(new FileReader(filename))) { + String line; + while ((line = reader.readLine()) != null) { + String courseName = line; + String courseCode = reader.readLine(); + int credits = Integer.parseInt(reader.readLine()); + // Read any other fields you saved + Course course = new Course(courseName, courseCode, credits); + courses.add(course); + } + } catch (IOException e) { + e.printStackTrace(); + } + + return courses; + } + + }