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

Activity Manager for Student Events

The ActivityManager class manages students and events, allowing for the addition of students and events, registration of students for events, and loading/saving data from/to files. It includes methods for finding students and events by ID, printing student events, and displaying all students. Error handling is implemented for cases where students or events are not found.

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 PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

Activity Manager for Student Events

The ActivityManager class manages students and events, allowing for the addition of students and events, registration of students for events, and loading/saving data from/to files. It includes methods for finding students and events by ID, printing student events, and displaying all students. Error handling is implemented for cases where students or events are not found.

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 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

The ActivityManager class manages the registration of a student to an event by utilizing the registerStudentForEvent method. This method first retrieves the student object corresponding to the provided student ID using the findStudentById method, and similarly fetches the event object using the findEventById method. If both the student and event objects are found, the student is registered for the event using the registerEvent method of the student object. However, if the student object is null (indicating an invalid student ID was provided), it prints "Student ID [studentId] not found". Similarly, if the event object is null (indicating an invalid event ID), it prints "Event ID [eventId] not found" .

The printStudentEvents method is designed to provide detailed information about a student's registered events. It first checks if the student ID corresponds to a valid student. Then, it retrieves the list of events the student is registered for and prints each event’s name and duration. Additionally, it calculates and displays the student's remaining required hours using student-related methods, offering a comprehensive view of the student's engagement without needing additional user input once invoked .

The error handling in the registerStudentForEvent method is straightforward but limited. It checks whether the student or event objects are null, which indicates an invalid ID. If so, it prints appropriate messages such as "Student ID not found" or "Event ID not found." This approach effectively informs the user of the specific error but does not prevent execution from continuing, nor does it provide a way to handle these errors programmatically (e.g., through exception throwing). For a more robust system, error-handling could be enhanced to include exceptions, logging, or retries .

The ActivityManager ensures that each student is uniquely identified by using the student ID as the key in methods like findStudentById and registerStudentForEvent. This use of a unique identifier prevents duplicate entries and allows for efficient searching and association of students with events. However, this dependency on unique IDs implies that ID management must be rigorous to avoid errors or inconsistency (e.g., input validation on IDs and ensuring ID uniqueness across input sources).

Directly printing messages within methods, such as those in registerStudentForEvent, reduces the flexibility and reusability of the class because it ties the business logic to a specific output method (console output). This can be a limitation when refactoring for different interfaces, such as GUIs or web services, where direct console output isn't suitable. Instead, returning status codes or throwing exceptions might be more appropriate, allowing the calling code to handle output according to the context in which the class is used .

To optimize the ActivityManager for scalability and modularity, consider separating concerns by increasing the use of interfaces and abstract classes. For instance, implementing an interface for student and event retrieval could allow different storage backends, such as databases, to be easily integrated. Additionally, employing a design pattern like MVC (Model-View-Controller) would separate data management, user interface, and control logic, enhancing scalability. Modularity can be improved by organizing code into smaller, function-specific components or packages that focus on discrete tasks, allowing easier maintenance and testing .

To improve interaction through the command line interface, the ActivityManager class can be extended to include more sophisticated input parsing, such as supporting commands with flags and arguments (e.g., using a command-like structure or switch cases). Methods like registerStudentForEventFromInput could handle conditional logic to manage errors, provide suggestions, or enable batch operations. Input feedback loops and validation prompts could guide users in entering valid data, enhancing user experience by reducing erroneous input and increasing task automation .

The ActivityManager class has methods such as addStudent and addEvent to manage lists of student and event objects by adding them to their respective ArrayLists. It also contains findStudentById and findEventById for searching objects based on their IDs. The registerStudentForEvent method links these by registering a student to an event using these ID retrieval methods. Additionally, printStudentEvents allows viewing events for a specific student, and printAllStudents lists all students stored in the manager .

The ActivityManager class loads data from external files through the loadDataFromFile method, which reads a file line by line using a Scanner. Each line is split by commas into fields, and entries starting with 'SV' are processed as students, while those with 'EV' are processed as events. Students and events are added to their respective lists in the ActivityManager. For saving data, the class uses the saveDataToFile method, which employs a FileWriter to write student details followed by their registered events into the specified file. Events are also written separately, each on a new line .

Using ArrayLists for storing students and events provides dynamic storage with automatic resizing, which is beneficial for general use. However, this choice has potential limitations in larger applications, such as slower performance for frequent additions or deletions, as these operations may require shifting elements. Additionally, searching operations involve linear time complexity O(n), which might become inefficient with a large number of elements. Alternatives like hashmaps could improve search performance due to constant time complexity for lookup, insertions, and deletions .

You might also like