Browse Source

Adding readFromFile method in Course class

remotes/origin/fdai7780
fdai7921 11 months ago
parent
commit
4932036ebd
  1. 23
      src/main/java/org/example/Course.java

23
src/main/java/org/example/Course.java

@ -1,5 +1,7 @@
package org.example; package org.example;
import java.io.*; import java.io.*;
import java.util.List;
import java.util.ArrayList;
@ -111,4 +113,25 @@ public class Course {
} }
} }
public static List<Course> readFromFile(String filename) {
List<Course> 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;
}
} }
Loading…
Cancel
Save