0% found this document useful (0 votes)
4 views43 pages

File Based System

The document describes a Java-based Student Management System that uses file storage for managing students, lecturers, courses, and results. It includes details about the team members involved in the project and outlines the structure of the code with various classes for handling different functionalities. The system is designed to perform basic academic operations while adhering to object-oriented principles.

Uploaded by

allankimutai16
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views43 pages

File Based System

The document describes a Java-based Student Management System that uses file storage for managing students, lecturers, courses, and results. It includes details about the team members involved in the project and outlines the structure of the code with various classes for handling different functionalities. The system is designed to perform basic academic operations while adhering to object-oriented principles.

Uploaded by

allankimutai16
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

File-based Student Management System

A Java-based Student Management System that utilizes file storage for data persistence instead
of a traditional database. This system enables basic academic operations such as managing
students, lecturers, courses, programmes, and student results using object-oriented principles.

https://github.com/DRAVIS55/JAVA-STUDENT-IMPLEMENTATION-FILE-BASED

MEMBERS

1. Kiptoo Moffat Ngetich: EB1/66856/23

2. Oswan Baraka Yunus: EB1/66784/23

3. Kimutai Allan: EB1/66848/23

4. Samuel Kibunja Macharia: EB1/66791/23

5. Akoko James: EB1/68809/23

6. Mukabi Keith: EB1/61384/22

7. Darius Rotich: EB1/68808/23

8. Omollo David: EB1/66890/23

9. Angela Aoko Achieng: EB1/66779/23

10. Leon Mwangi: EB1/66782/23

11. Emmanuel Owino :EB1/66896/23

12. Tracy Mideva: EB1/66872/23

Table of Contents
APP.java ...................................................................................................................................... 2
Course.java ................................................................................................................................. 5
Lecture.java .............................................................................................................................. 12
Person ....................................................................................................................................... 17
Programme.java ....................................................................................................................... 20
Student.java.............................................................................................................................. 27
SystemGUI.java......................................................................................................................... 35

1
APP.java
import java.io.File;

import java.io.IOException;

public class App {

// Constructor: Accepts a file name and ensures it exists

public App(String fileName) throws IOException {

checkAndCreateFile(fileName);

// Method to check if a file exists, is readable, and writable

private void checkAndCreateFile(String fileName) throws IOException {

File file = new File(fileName);

// Check if the file exists

if (!file.exists()) {

if (file.createNewFile()) {

System.out.println("File created: " + file.getName());

} else {

System.out.println("Failed to create file: " + file.getName());

2
} else {

System.out.println("File already exists: " + file.getName());

// Check if the file is readable and writable

if (!file.canRead() || !file.canWrite()) {

System.out.println("File is not readable or writable. Attempting to adjust


permissions...");

// Attempt to set the file as readable and writable

try {

if (file.setReadable(true) && file.setWritable(true)) {

System.out.println("Permissions set: File is now readable and writable.");

} else {

System.out.println("Failed to set file permissions.");

} catch (SecurityException e) {

System.out.println("SecurityException: Could not adjust file permissions.");

e.printStackTrace();

// If still not readable or writable, delete and recreate the file

if (!file.canRead() || !file.canWrite()) {

System.out.println("File permissions cannot be fixed. Deleting and recreating file...");

if (file.delete() && file.createNewFile()) {

System.out.println("File deleted and recreated: " + file.getName());

} else {

3
System.out.println("Failed to delete and recreate the file: " + file.getName());

4
Course.java
import java.io.*;

import java.util.ArrayList;

public class Course {

private static final String FILE_PATH = "course_data.txt";

// Add a new course

public void addCourse(String courseCode, String name, String creditHours) {

try (BufferedReader reader = new BufferedReader(new FileReader(FILE_PATH))) {

String line;

boolean courseExists = false;

// Check if course code already exists in the file

while ((line = reader.readLine()) != null) {

if (line.contains(courseCode)) {

courseExists = true;

break;

if (courseExists) {

new ReusableClass().printMessage("The course code already exists!");

return;

5
// If course doesn't exist, add it to the file

try (BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_PATH, true))) {

String courseData = courseCode + "," + name + "," + creditHours;

writer.write(courseData);

writer.newLine();

new ReusableClass().printMessage("Course registered successfully!");

} catch (IOException e) {

e.printStackTrace();

new ReusableClass().printMessage("App error: Unable to add course! " +


e.getMessage());

// Modify an existing course

public void modifyCourse(String courseCode, String creditHours) {

try {

ArrayList<String> fileContent = new ArrayList<>();

BufferedReader reader = new BufferedReader(new FileReader(FILE_PATH));

String line;

boolean courseFound = false;

while ((line = reader.readLine()) != null) {

String[] data = line.split(",");

if (data[0].equals(courseCode)) {

line = courseCode + "," + data[1] + "," + creditHours;

6
courseFound = true;

fileContent.add(line);

reader.close();

if (!courseFound) {

new ReusableClass().printMessage("The course code is not registered!");

return;

try (BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_PATH))) {

for (String content : fileContent) {

writer.write(content);

writer.newLine();

new ReusableClass().printMessage("Course modified successfully!");

} catch (IOException e) {

e.printStackTrace();

new ReusableClass().printMessage("App error: Unable to modify course! " +


e.getMessage());

// Delete an existing course

7
public void deleteCourse(String courseCode) {

try {

ArrayList<String> fileContent = new ArrayList<>();

BufferedReader reader = new BufferedReader(new FileReader(FILE_PATH));

String line;

boolean courseFound = false;

while ((line = reader.readLine()) != null) {

String[] data = line.split(",");

if (data[0].equals(courseCode)) {

courseFound = true;

continue;

fileContent.add(line);

reader.close();

if (!courseFound) {

new ReusableClass().printMessage("Course does not exist!");

return;

try (BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_PATH))) {

for (String content : fileContent) {

writer.write(content);

writer.newLine();

8
}

new ReusableClass().printMessage("Course deleted successfully!");

} catch (IOException e) {

e.printStackTrace();

new ReusableClass().printMessage("App error: Unable to delete course! " +


e.getMessage());

// Show all courses

public ArrayList<String> showCourses() {

ArrayList<String> contentList = new ArrayList<>();

try (BufferedReader reader = new BufferedReader(new FileReader(FILE_PATH))) {

String line;

while ((line = reader.readLine()) != null) {

String[] data = line.split(",");

contentList.add(data[0] + " - " + data[1]);

} catch (IOException e) {

e.printStackTrace();

new ReusableClass().printMessage("App Error: Unable to load courses!");

return contentList;

9
// Search for students registered in a specific course

public ArrayList<String> searchRegisteredStudents(String courseId) {

ArrayList<String> students = new ArrayList<>();

try (BufferedReader reader = new BufferedReader(new FileReader("course_data.txt"))) {

String line;

boolean studentsFound = false;

while ((line = reader.readLine()) != null) {

String[] data = line.split(",");

if (data[1].equals(courseId)) {

studentsFound = true;

students.add("Student ID: " + data[0] + " - Course Code: " + data[1]);

if (!studentsFound) {

students.add("No students found for this course.");

} catch (IOException e) {

e.printStackTrace();

students.add("Error retrieving registered students.");

return students;

// Get lecturers assigned to a specific course

10
public ArrayList<String[]> getLecturersByCourse(String courseId) {

ArrayList<String[]> lecturers = new ArrayList<>();

try (BufferedReader reader = new BufferedReader(new FileReader("lecturer_data.txt"))) {

String line;

boolean lecturersFound = false;

while ((line = reader.readLine()) != null) {

String[] data = line.split(",");

if (data[1].equals(courseId)) {

lecturersFound = true;

lecturers.add(new String[]{data[0], data[2], data[3]});

if (!lecturersFound) {

lecturers.add(new String[]{"No lecturers found for this course."});

} catch (IOException e) {

e.printStackTrace();

lecturers.add(new String[]{"Error retrieving lecturers."});

return lecturers;

11
Lecture.java
import java.io.*;

import java.util.ArrayList;

public class Lecturer extends Person {

private File file;

// Constructor: Initialize file object

public Lecturer(String fileName) {

this.file = new File(fileName);

// Register a new Lecturer

public boolean registerLecturer(String lecturerID, String f_name, String s_name, String phone,
String department, String email) {

try (BufferedWriter writer = new BufferedWriter(new FileWriter(file, true))) {

writer.write(lecturerID + "," + f_name + "," + s_name + "," + phone + "," + department +


"," + email);

writer.newLine();

return true;

} catch (IOException e) {

e.printStackTrace();

return false;

12
// Update Lecturer Details

public boolean updateLecturer(String lecturerID, String newName, String newEmail) {

try (BufferedReader reader = new BufferedReader(new FileReader(file))) {

StringBuilder content = new StringBuilder();

String line;

boolean updated = false;

while ((line = reader.readLine()) != null) {

String[] details = line.split(",");

if (details[0].equals(lecturerID)) {

details[1] = newName;

details[5] = newEmail;

line = String.join(",", details);

updated = true;

content.append(line).append("\n");

if (updated) {

try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {

writer.write(content.toString());

return updated;

} catch (IOException e) {

e.printStackTrace();

13
return false;

// Assign courses to a lecturer

public boolean assignCourses(String lecturerIdNo, ArrayList<String> courseCodes) {

try (BufferedReader reader = new BufferedReader(new FileReader(file))) {

String line;

boolean lecturerFound = false;

while ((line = reader.readLine()) != null) {

String[] details = line.split(",");

if (details[0].equals(lecturerIdNo)) {

lecturerFound = true;

break;

if (lecturerFound) {

try (BufferedWriter writer = new BufferedWriter(new FileWriter(file, true))) {

for (String courseCode : courseCodes) {

writer.write(lecturerIdNo + " is assigned to course " + courseCode);

writer.newLine();

return true;

14
} else {

new ReusableClass().printMessage("Lecturer with ID " + lecturerIdNo + " does not


exist.");

return false;

} catch (IOException e) {

e.printStackTrace();

return false;

// Search for a Lecturer by ID

public ArrayList<String> searchLecturer(String lecturerID) {

ArrayList<String> lecturerDetails = new ArrayList<>();

try (BufferedReader reader = new BufferedReader(new FileReader(file))) {

String line;

while ((line = reader.readLine()) != null) {

String[] details = line.split(",");

if (details[0].equals(lecturerID)) {

lecturerDetails.add("Lecturer ID: " + details[0]);

lecturerDetails.add("First Name: " + details[1]);

lecturerDetails.add("Last Name: " + details[2]);

lecturerDetails.add("Phone: " + details[3]);

lecturerDetails.add("Department: " + details[4]);

lecturerDetails.add("Email: " + details[5]);

return lecturerDetails;

15
}

lecturerDetails.add("Lecturer not found!");

} catch (IOException e) {

e.printStackTrace();

return lecturerDetails;

16
Person
public class Person {

// Fields (attributes)

private String firstName;

private String lastName;

private String id; // This could be a student ID, employee ID, etc.

// Default constructor

public Person() {

// Initialize with default values

this.firstName = "";

this.lastName = "";

this.id = "";

// Constructor with parameters

public Person(String firstName, String lastName, String id) {

this.firstName = firstName;

this.lastName = lastName;

this.id = id;

// Getter and Setter methods for firstName

public String getFirstName() {

return firstName;

17
}

public void setFirstName(String firstName) {

this.firstName = firstName;

// Getter and Setter methods for lastName

public String getLastName() {

return lastName;

public void setLastName(String lastName) {

this.lastName = lastName;

// Getter and Setter methods for id

public String getId() {

return id;

public void setId(String id) {

this.id = id;

// toString method to provide a string representation of the Person object

@Override

18
public String toString() {

return "Person [ID=" + id + ", Name=" + firstName + " " + lastName + "]";

19
Programme.java

import java.io.*;

import java.math.BigDecimal;

import java.util.ArrayList;

import java.util.Scanner;

public class Programme {

// Path for the storage files (for simplicity, storing as plain text files)

private static final String PROGRAMME_FILE = "programme_data.txt";

private static final String PROGRAMME_COURSES_FILE = "programme_data.txt";

// Explicitly call the superclass constructor (no parameters in this case)

public Programme() { // Call the App constructor

// Add a programme to the file

public void addProgram(App app, String programmeName, String programmeCode,


BigDecimal programmeCost) throws IOException {

File programmeFile = new File(PROGRAMME_FILE);

// Check if the file is writable

if (!hasWritePermission(programmeFile)) {

20
new ReusableClass().printMessage("The file is not writable. Please check the file
permissions.");

return;

File myobj=new File("programme_data.txt");

Scanner reader=new Scanner(myobj);

// BufferedReader reader = new BufferedReader(new FileReader(programmeFile));

String line;

// Check if the programme already exists

while ( reader.hasNextLine()) {

line=reader.nextLine();

String[] parts = line.split(",");

if (parts[1].equals(programmeCode)) {

new ReusableClass().printMessage("The Programme code or programme already


exists!");

reader.close();

return;

reader.close();

// Add new programme if writable

BufferedWriter writer = new BufferedWriter(new FileWriter(programmeFile, true));

writer.write(programmeName + "," + programmeCode + "," + programmeCost + "\n");

writer.close();

21
new ReusableClass().printMessage("The programme is updated successfully\nProgramme
code: " + programmeCode + "\nProgramme Name: " + programmeName);

// Check if the file has write permission

public boolean hasWritePermission(File file) {

if (file.exists()) {

return file.canWrite(); // Check if the file has write permissions

return false;

// Modify programme details in the file

public void modifyProgram(App app, BigDecimal cost, String code) throws IOException {

File tempFile = new File("temp_programmes.txt");

BufferedReader reader = new BufferedReader(new FileReader(PROGRAMME_FILE));

BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));

String line;

boolean found = false;

while ((line = reader.readLine()) != null) {

String[] parts = line.split(",");

if (parts[1].equals(code)) {

line = parts[0] + "," + parts[1] + "," + cost; // Modify cost

found = true;

22
writer.write(line + "\n");

reader.close();

writer.close();

if (found) {

tempFile.renameTo(new File(PROGRAMME_FILE)); // Replace original file with modified


file

new ReusableClass().printMessage("Updated successfully: Programme code: " + code +


"\nNew cost: " + cost);

} else {

tempFile.delete();

new ReusableClass().printMessage("The programme does not exist! Consider creating


one.");

// Remove programme from the file

public void removeProgram(App app, String programme_code) throws IOException {

File tempFile = new File("temp_programmes.txt");

BufferedReader reader = new BufferedReader(new FileReader(PROGRAMME_FILE));

BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));

String line;

boolean found = false;

23
while ((line = reader.readLine()) != null) {

String[] parts = line.split(",");

if (parts[1].equals(programme_code)) {

found = true; // Skip this programme

continue;

writer.write(line + "\n");

reader.close();

writer.close();

if (found) {

tempFile.renameTo(new File(PROGRAMME_FILE)); // Replace original file with modified


file

new ReusableClass().printMessage("Programme removed successfully!");

} else {

tempFile.delete();

new ReusableClass().printMessage("The programme does not exist!");

// Attach courses to a programme in the file

public void attachProgramCourses(App app, String program, String course) throws


IOException {

BufferedReader reader = new BufferedReader(new


FileReader(PROGRAMME_COURSES_FILE));

24
String line;

// Check if the combination already exists

while ((line = reader.readLine()) != null) {

String[] parts = line.split(",");

if (parts[0].equals(program) && parts[1].equals(course)) {

new ReusableClass().printMessage("The program and course already exist in the


table!");

reader.close();

return;

reader.close();

// Add new programme-course pair

BufferedWriter writer = new BufferedWriter(new FileWriter(PROGRAMME_COURSES_FILE,


true));

writer.write(program + "," + course + "\n");

writer.close();

new ReusableClass().printMessage("Course assigned successfully!");

// Search for students by programme in the file

public ArrayList<String> searchStudentsByProgramme(App app, String programmeId) throws


IOException {

ArrayList<String> students = new ArrayList<>();

25
BufferedReader reader = new BufferedReader(new FileReader("students.txt"));

String line;

boolean found = false;

while ((line = reader.readLine()) != null) {

String[] parts = line.split(",");

if (parts[3].equals(programmeId)) {

students.add(parts[0] + " - " + parts[1] + " " + parts[2]);

found = true;

reader.close();

if (!found) {

students.add("No students found for this programme.");

return students;

26
Student.java
import java.io.*;

import java.util.ArrayList;

public class Student extends Person {

// Instance of ReusableClass for printing messages

// Constructor

public Student() {

// Register a student via file operations

public void registerStudent(String firstName, String lastName, String email, String phone,
String gender, String programmeID) {

try (BufferedReader reader = new BufferedReader(new FileReader("programme_data.txt")))


{

String line;

boolean programmeExists = false;

// Check if the programme ID exists

while ((line = reader.readLine()) != null) {

if (line.contains(programmeID)) {

27
programmeExists = true;

break;

if (!programmeExists) {

new ReusableClass().printMessage(" Error: programme_id '" + programmeID + "'


does not exist!");

return;

// Save the student information

try (BufferedWriter writer = new BufferedWriter(new FileWriter("student_data.txt",


true))) {

String studentData = firstName + "," + lastName + "," + email + "," + phone + "," +
gender + "," + programmeID;

writer.write(studentData);

writer.newLine();

new ReusableClass().printMessage(" Student registered successfully!");

} catch (IOException e) {

new ReusableClass().printMessage(" Error registering student!");

// Enroll a student in a course via file operations

public void enrollStudent(String studentID, String courseCode, String semester, String score) {

28
try {

// Check if the student exists

BufferedReader studentReader = new BufferedReader(new


FileReader("student_data.txt"));

String line;

boolean studentExists = false;

while ((line = studentReader.readLine()) != null) {

if (line.split(",")[0].equals(studentID)) {

studentExists = true;

break;

if (!studentExists) {

new ReusableClass().printMessage(" Student not found!");

return;

// Check if the course exists

BufferedReader courseReader = new BufferedReader(new FileReader("course_data.txt"));

boolean courseExists = false;

while ((line = courseReader.readLine()) != null) {

if (line.contains(courseCode)) {

courseExists = true;

break;

29
}

if (!courseExists) {

new ReusableClass().printMessage(" Course not found!");

return;

// Save enrollment data

try (BufferedWriter writer = new BufferedWriter(new FileWriter("student_courses.txt",


true))) {

String enrollmentData = studentID + "," + courseCode + "," + semester + "," + score;

writer.write(enrollmentData);

writer.newLine();

new ReusableClass().printMessage(" Student enrolled successfully in the course!");

} catch (IOException e) {

new ReusableClass().printMessage(" Error enrolling student to course!");

// Method to assign scores to a student for a specific course

public void assignScores(String studentID, String courseCode, String score) {

try {

ArrayList<String> fileContent = new ArrayList<>();

BufferedReader reader = new BufferedReader(new FileReader("student_courses.txt"));

30
String line;

boolean updated = false;

// Check and update the score for the student-course pair

while ((line = reader.readLine()) != null) {

String[] data = line.split(",");

if (data[0].equals(studentID) && data[1].equals(courseCode)) {

line = studentID + "," + courseCode + "," + data[2] + "," + score; // Update score

updated = true;

fileContent.add(line);

if (updated) {

try (BufferedWriter writer = new BufferedWriter(new


FileWriter("student_courses.txt"))) {

for (String content : fileContent) {

writer.write(content);

writer.newLine();

new ReusableClass().printMessage(" Score successfully assigned!");

} else {

new ReusableClass().printMessage(" Student-course pair not found!");

} catch (IOException e) {

31
new ReusableClass().printMessage(" Error assigning score!");

// Method to search a student by ID and return details

public ArrayList<String> searchStudent(String studentID) {

ArrayList<String> details = new ArrayList<>();

try (BufferedReader reader = new BufferedReader(new FileReader("student_data.txt"))) {

String line;

boolean studentFound = false;

while ((line = reader.readLine()) != null) {

String[] data = line.split(",");

if (data[0].equals(studentID)) {

studentFound = true;

details.add("Student ID: " + data[0]);

details.add("First Name: " + data[1]);

details.add("Last Name: " + data[2]);

details.add("Email: " + data[3]);

details.add("Phone: " + data[4]);

details.add("Gender: " + data[5]);

details.add("Programme ID: " + data[6]);

break;

if (!studentFound) {

32
details.add(" Student not found!");

} catch (IOException e) {

details.add(" Error searching student!");

return details;

// Search student results by ID

public ArrayList<String> searchStudentResults(String studentId) throws IOException {

ArrayList<String> results = new ArrayList<>();

BufferedReader scReader = new BufferedReader(new FileReader("student_courses.txt"));

String line;

boolean found = false;

while ((line = scReader.readLine()) != null) {

String[] parts = line.split(",");

if (parts[0].equals(studentId)) {

String courseCode = parts[1];

String score = parts[2];

String courseName = getCourseName(courseCode);

results.add(courseName + " - Score: " + score);

found = true;

33
}

scReader.close();

if (!found) {

results.add("No courses found for this student.");

return results;

// Helper method to get course name by course code

private String getCourseName(String courseCode) throws IOException {

BufferedReader courseReader = new BufferedReader(new FileReader("course_data.txt"));

String line;

while ((line = courseReader.readLine()) != null) {

String[] parts = line.split(",");

if (parts[0].equals(courseCode)) {

courseReader.close();

return parts[1];

courseReader.close();

return "Course not found";

}}

34
SystemGUI.java
import java.io.*;

import java.util.ArrayList;

public class Student {

// Instance of ReusableClass for printing messages

// Constructor

public Student() {

// Register a student via file operations

public void registerStudent(String firstName, String lastName, String email, String phone,
String gender, String programmeID) {

try (BufferedReader reader = new BufferedReader(new FileReader("programme_data.txt")))


{

String line;

boolean programmeExists = false;

// Check if the programme ID exists

while ((line = reader.readLine()) != null) {

35
if (line.contains(programmeID)) {

programmeExists = true;

break;

if (!programmeExists) {

new ReusableClass().printMessage(" Error: programme_id '" + programmeID + "'


does not exist!");

return;

// Save the student information

try (BufferedWriter writer = new BufferedWriter(new FileWriter("student_data.txt",


true))) {

String studentData = firstName + "," + lastName + "," + email + "," + phone + "," +
gender + "," + programmeID;

writer.write(studentData);

writer.newLine();

new ReusableClass().printMessage(" Student registered successfully!");

} catch (IOException e) {

new ReusableClass().printMessage(" Error registering student!");

// Enroll a student in a course via file operations

36
public void enrollStudent(String studentID, String courseCode, String semester, String score) {

try {

// Check if the student exists

BufferedReader studentReader = new BufferedReader(new


FileReader("student_data.txt"));

String line;

boolean studentExists = false;

while ((line = studentReader.readLine()) != null) {

if (line.split(",")[0].equals(studentID)) {

studentExists = true;

break;

if (!studentExists) {

new ReusableClass().printMessage(" Student not found!");

return;

// Check if the course exists

BufferedReader courseReader = new BufferedReader(new FileReader("course_data.txt"));

boolean courseExists = false;

while ((line = courseReader.readLine()) != null) {

if (line.contains(courseCode)) {

courseExists = true;

37
break;

if (!courseExists) {

new ReusableClass().printMessage(" Course not found!");

return;

// Save enrollment data

try (BufferedWriter writer = new BufferedWriter(new FileWriter("student_courses.txt",


true))) {

String enrollmentData = studentID + "," + courseCode + "," + semester + "," + score;

writer.write(enrollmentData);

writer.newLine();

new ReusableClass().printMessage(" Student enrolled successfully in the course!");

} catch (IOException e) {

new ReusableClass().printMessage(" Error enrolling student to course!");

// Method to assign scores to a student for a specific course

public void assignScores(String studentID, String courseCode, String score) {

try {

ArrayList<String> fileContent = new ArrayList<>();

38
BufferedReader reader = new BufferedReader(new FileReader("student_courses.txt"));

String line;

boolean updated = false;

// Check and update the score for the student-course pair

while ((line = reader.readLine()) != null) {

String[] data = line.split(",");

if (data[0].equals(studentID) && data[1].equals(courseCode)) {

line = studentID + "," + courseCode + "," + data[2] + "," + score; // Update score

updated = true;

fileContent.add(line);

if (updated) {

try (BufferedWriter writer = new BufferedWriter(new


FileWriter("student_courses.txt"))) {

for (String content : fileContent) {

writer.write(content);

writer.newLine();

new ReusableClass().printMessage(" Score successfully assigned!");

} else {

new ReusableClass().printMessage(" Student-course pair not found!");

39
} catch (IOException e) {

new ReusableClass().printMessage(" Error assigning score!");

// Method to search a student by ID and return details

public ArrayList<String> searchStudent(String studentID) {

ArrayList<String> details = new ArrayList<>();

try (BufferedReader reader = new BufferedReader(new FileReader("student_data.txt"))) {

String line;

boolean studentFound = false;

while ((line = reader.readLine()) != null) {

String[] data = line.split(",");

if (data[0].equals(studentID)) {

studentFound = true;

details.add("Student ID: " + data[0]);

details.add("First Name: " + data[1]);

details.add("Last Name: " + data[2]);

details.add("Email: " + data[3]);

details.add("Phone: " + data[4]);

details.add("Gender: " + data[5]);

details.add("Programme ID: " + data[6]);

break;

40
if (!studentFound) {

details.add(" Student not found!");

} catch (IOException e) {

details.add(" Error searching student!");

return details;

// Search student results by ID

public ArrayList<String> searchStudentResults(String studentId) throws IOException {

ArrayList<String> results = new ArrayList<>();

BufferedReader scReader = new BufferedReader(new FileReader("student_courses.txt"));

String line;

boolean found = false;

while ((line = scReader.readLine()) != null) {

String[] parts = line.split(",");

if (parts[0].equals(studentId)) {

String courseCode = parts[1];

String score = parts[2];

String courseName = getCourseName(courseCode);

results.add(courseName + " - Score: " + score);

found = true;

41
}

scReader.close();

if (!found) {

results.add("No courses found for this student.");

return results;

// Helper method to get course name by course code

private String getCourseName(String courseCode) throws IOException {

BufferedReader courseReader = new BufferedReader(new FileReader("course_data.txt"));

String line;

while ((line = courseReader.readLine()) != null) {

String[] parts = line.split(",");

if (parts[0].equals(courseCode)) {

courseReader.close();

return parts[1];

courseReader.close();

return "Course not found";

42
}

43

You might also like