0% found this document useful (0 votes)
25 views4 pages

Java Activity Manager for Students

The ActivityManager class manages a list of students and events, allowing for the addition of students and events, as well as registering students for events. It includes methods for loading and saving data from/to files, printing student events, and searching for students by ID. The class utilizes a Scanner for user input to register students for events and handles potential errors when searching for students or events.

Uploaded by

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

Java Activity Manager for Students

The ActivityManager class manages a list of students and events, allowing for the addition of students and events, as well as registering students for events. It includes methods for loading and saving data from/to files, printing student events, and searching for students by ID. The class utilizes a Scanner for user input to register students for events and handles potential errors when searching for students or events.

Uploaded by

tam.le2302220
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Chí Tâm_2302220_TTU

[Link]:
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class ActivityManager {


private List<Student> students;
private List<Event> events;

public ActivityManager() {
students = new ArrayList<>();
events = new ArrayList<>();
}

public void addStudent(Student student) {


[Link](student);
}

public void addEvent(Event event) {


[Link](event);
}

public void registerStudentForEvent(String studentId, String eventId) {


Student student = findStudentById(studentId);
Event event = findEventById(eventId);
if (student != null && event != null) {
[Link](event);
} else {
if (student == null) {
[Link]("Student ID " + studentId + " not found.");
}
if (event == null) {
[Link]("Event ID " + eventId + " not found.");
}
}
}

public void registerStudentForEventFromInput() {


/ resource /
Chí Tâm_2302220_TTU

Scanner scanner = new Scanner([Link]);


[Link]("Enter student ID: ");
String studentId = [Link]();
[Link]("Enter event ID: ");
String eventId = [Link]();
registerStudentForEvent(studentId, eventId);
}

public Student findStudentById(String studentId) {


for (Student student : students) {
if ([Link]().equals(studentId)) {
return student;
}
}
return null;
}

public Event findEventById(String eventId) {


for (Event event : events) {
if ([Link]().equals(eventId)) {
return event;
}
}
return null;
}

public void loadDataFromFile(String filename) {


try (Scanner scanner = new Scanner(new File(filename))) {
while ([Link]()) {
String line = [Link]();
String[] fields = [Link](",");

if (fields[0].startsWith("SV")) {
Student student = new Student(fields[0], fields[1], [Link](fields[2].trim()),
fields[3], fields[4], [Link](fields[5].trim()));
addStudent(student);
[Link]("Loaded student: " + [Link]());
} else if (fields[0].startsWith("EV")) {
Event event = new Event(fields[0], fields[1], [Link](fields[2].trim()));
addEvent(event);
[Link]("Loaded event: " + [Link]());
}
}
Chí Tâm_2302220_TTU

} catch (IOException e) {
[Link]();
}
}

public void saveDataToFile(String filename) {


try (FileWriter writer = new FileWriter(filename)) {
for (Student student : students) {
[Link]([Link]() + "," + [Link]() + "," +
[Link]() + "," +
[Link]() + "," + [Link]() + "," +
[Link]() + "\n");
for (Event event : [Link]()) {
[Link]([Link]() + "," + [Link]() + "\n");
}
}
for (Event event : events) {
[Link]([Link]() + "," + [Link]() + "," + [Link]()
+ "\n");
}
} catch (IOException e) {
[Link]();
}
}

public void printStudentEvents(String studentId) {


Student student = findStudentById(studentId);
if (student != null) {
[Link]("Events for student " + [Link]() + ":");
List<Event> studentEvents = [Link]();
if (![Link]()) {
for (Event event : studentEvents) {
[Link](" - " + [Link]() + " (" + [Link]() + "
hours)");
}
[Link]("Remaining hours: " + [Link]());
} else {
[Link]("No events registered.");
}
} else {
[Link]("Student not found.");
}
}
Chí Tâm_2302220_TTU

public void printAllStudents() {


for (Student student : students) {
[Link](student);
}
}

public boolean searchStudentById(String studentId) {


Student student = findStudentById(studentId);
if (student != null) {
[Link](student);
return true;
} else {
[Link]("Student ID " + studentId + " not found.");
return false;
}
}
}

Common questions

Powered by AI

During the data-loading process, the ActivityManager class outputs affirmative messages to the console for each student and event loaded, specifically mentioning the student's full name and the event's name. This feedback mechanism is significant as it provides real-time confirmation that data records are being processed and added successfully, allowing users to verify the system's actions and assist in troubleshooting potential data entry errors .

The ActivityManager implements logic to address missing or incorrect IDs by first checking if a corresponding student or event exists before attempting registration. If either the student or event is not found, it prints specific error messages pinpointing which ID (student or event) is invalid. This preemptive validation avoids runtime errors during the registration process and provides clear feedback to guide user corrections .

The ActivityManager saves student data and associated events by iterating through each student and then writing both the student's details and their registered events to the file. This nested writing prevents data redundancy by ensuring that only events related to a given student are written following that student's details, thus not duplicating unrelated or unsaved data. This design supports efficient data storage and retrieval while keeping file contents organized in a predictable and logical manner .

The ActivityManager class first invokes the findStudentById and findEventById methods to ensure that the student and event exist in the respective lists before attempting registration. If both the student and event exist, the student is registered for the event through the registerEvent method. If either the student or event is not found, the system outputs relevant error messages indicating the missing ID .

The ActivityManager class utilizes ArrayLists to maintain the lists of students and events. This approach allows dynamic resizing to accommodate an arbitrary number of Student and Event objects, making it efficient for adding and removing elements. The ease of traversing and accessing elements in an ArrayList offers significant benefits in terms of speed for operations like registration and searching, given the frequent list manipulations inherent in managing events and students .

Error handling during data-saving is crucial to ensure that files are written accurately and possible exceptions do not disrupt the application. The ActivityManager implements exception management using try-with-resources to ensure that resources like FileWriter are closed promptly after use, and a catch block to handle IOExceptions. This approach not only improves resource management but also provides robust handling of potential exceptions that might arise during file operations, thus reducing application downtime and preventing data loss .

The design choice to print a student's events with the corresponding hours, including a calculation of remaining hours required, is crucial for enhancing user experience. This comprehensive display enables students and administrators to quickly assess the student’s participation status and progress toward fulfilling required hours. By offering a clear and concise summary, this feature facilitates better planning and decision-making regarding student engagements and fulfills administrative oversight functions efficiently .

The ActivityManager class uses the loadDataFromFile method to read student and event data from a file. This method employs a Scanner to read lines, splitting each line into fields to identify whether the entry is a student or event based on prefixes ('SV' for Student, 'EV' for Event), and accordingly creates and adds Student or Event objects. It manages I/O exceptions using a try-catch block to handle any file reading errors .

The ActivityManager class uses the registerStudentForEventFromInput method to facilitate user interaction for event registration. It prompts the user for student and event IDs through console input, reads these inputs, and attempts to register the student using the registerStudentForEvent method. If the provided student ID or event ID does not match existing records, the system outputs appropriate error messages, informing the user that the IDs could not be found .

The searchStudentById method aids in maintaining data integrity by returning only valid Student objects matching the provided student ID. If no match is found, it outputs an error message indicating the non-existence of the ID. This functionality ensures that operations relying on student data, such as event registration or information display, are performed on legitimate records, thereby preserving the application's data consistency and accuracy .

You might also like