A practice coding example that I would like to see the code for using LinkedList
ID: 3723116 • Letter: A
Question
A practice coding example that I would like to see the code for using LinkedLists!
Will help with upcoming exam!
You are required to use the Java LinkedList class lists in this assignment.
Enrollment Management System
A university has requested the development of an enrollment management system. This system
will be responsible for managing course information, student information, and enrollments of
students in courses. The fundamental operations required of this system are as follows:
Add a course to the system
o A course can only be added one frne to the system
Get all courses known to the system
o A well formatted string containing data on all courses
o Course data should appear in sorted order
Get all students known to the system
o A well formatted string containing data on all students
o Student data should appear in sorted order
Add a student to the system
o A student can only be added one frne to the system
Enroll a student in a course
o A student can only be enrolled in a course one frne
Get all students enrolled in a course
o A well formatted string containing data on all enrolled students
o Student data should appear in sorted order
Drop a student from a course
Get all courses a student is enrolled in
o A well formatted string containing data on the student's courses
o Course data should appear in sorted order
High Level Design Observations
There are several things which can be stated about the design of this system.
l. The system will contain a list of all courses.
2. The system will contain a list of all students.
3. Each course will contain a list of students enrolled in the course.
4. Each student will contain a list of the courses they are enrolled in.
Course info Class
The information required for each course is as follows:
course id, course name, instructor
days the course meets, frne the course meets
room number
list of enrolled students
The first 6 items are all strings.
The only constructor takes 6 parameter values and stores them into the properties.
The class should implement the toString method to return a formatted string with all 6 data
values clearly identified. An example format appears below:
CID: CS2100 Name: Data Structures Prof: Profname Days: MWF Time: 3:00-3:55 Room: RC370
This class also needs to implement the Comparable interface, thereby enabling the use of sorting tools to sort a list of courses based on the course id.
Student Info Class
The information required for each student is as follows:
student id, student name — both of these are sfrings
list of courses the student is enrolled in
The only constructor takes 2 parameter values and stores them into the properties.
The class should implement the toString method to return a formatted string with both data
values clearly identified. An example format appears below:
Name: Nebula, Georgina S.
SID: w004gsn
This class also needs to implement the Comparable interface, thereby enabling the use of sorting
tools to sort a list of students based on the student name.
Enrollment Manager Class
The information required for this class is as follows:
List of Course Info objects
List of Student Info objects
The methods which must be provided by this class include:
A method to add a course
o All 6 pieces of course information must be passed in
o If the course id is already in the list of courses, print an error message
A method to get a string containing formatted data on all courses (in sorted order)
A method to add a student
o Both pieces of student information must be passed in
o If the student id is already in the list of students, print an error message
A method to get a string containing formatted data on all students (in sorted order)
A method to enroll a student in a course
o If the student does not exist, print an error message
o If the course does not exist, print an error message
o If the student is already enrolled in the course, print an error
A method to drop a student from a course
o If the student does not exist, print an error message
o If the course does not exist, print an error message
o If the student is not enrolled in the course, print an error
A method to get a string containing formatted data about the students enrolled in a course
(in sorted order)
o If the course does not exist, print an error message
A method to get a string containing formatted data about the courses a student is enrolled
in (in sorted order)
o If the student does not exist, print an error message
Testing
The test program must thoroughly test the Enrollment Manager class. It should add at least 10
students and 10 courses. Make sure to add the courses out of order. Make sure to attempt to add a duplicate course and a duplicate student.
Create test cases to enroll at least 2 students into at least 4 different courses. Try enrolling the
same student into the same course twice.
Create test cases to enroll at least 4 students into the same class.
Create test cases to drop a student from a course.
Explanation / Answer
package enrollmentmanagementsystem;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class Course implements Comparable<Course> {
private String courseId;
private String courseName;
private String instructor;
private String days;
private String time;
private String room;
LinkedList<Student> students = new LinkedList<>();
// constructor
public Course(String courseId, String courseName, String instructor, String days, String time, String room) {
super();
this.courseId = courseId;
this.courseName = courseName;
this.instructor = instructor;
this.days = days;
this.time = time;
this.room = room;
}
// Getters and setters
public String getCourseId() {
return courseId;
}
public void setCourseId(String courseId) {
this.courseId = courseId;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public String getInstructor() {
return instructor;
}
public void setInstructor(String instructor) {
this.instructor = instructor;
}
public String getDays() {
return days;
}
public void setDays(String days) {
this.days = days;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getRoom() {
return room;
}
public void setRoom(String room) {
this.room = room;
}
public List<Student> getStudents() {
return students;
}
public void setStudents(LinkedList<Student> students) {
this.students = students;
}
// toString
@Override
public int compareTo(Course o) {
//sort based on course Id
return this.getCourseId().compareTo(o.getCourseId());
}
@Override
public String toString() {
return "CID: " + courseId + " Name: " + courseName + " Prof:" + instructor + " Days:"
+ days + " Time:" + time + " Room:" + room;
}
}
package enrollmentmanagementsystem;
public class Student implements Comparable<Student>{
private String studentId;
private String studentName;
public Student(String studentId, String studentName) {
super();
this.studentId = studentId;
this.studentName = studentName;
}
public String getStudentId() {
return studentId;
}
public void setStudentId(String studentId) {
this.studentId = studentId;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
@Override
public String toString() {
return"Name:" + studentName+ " SID:" + studentId ;
}
@Override
public int compareTo(Student o) {
return this.getStudentName().compareTo(o.getStudentName());
}
}
package enrollmentmanagementsystem;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
public class EnrollmentManager {
LinkedList<Course> courses;
LinkedList<Student> students;
public EnrollmentManager() {
courses = new LinkedList<>();
students = new LinkedList<>();
}
// method to add course
public void addCourse(String courseId, String courseName, String instructor, String days, String time,
String room) {
boolean isToAdd = true;
// create course object
Course c = new Course(courseId, courseName, instructor, days, time, room);
// check if it is already in the list
for (Course course : courses) {
if (course.getCourseId().equalsIgnoreCase(courseId))
{
System.out.println("Error !!! This course is already added.");
isToAdd = false;
break;
}
}
if (isToAdd) {
courses.add(c);
}
}
// method to add student
public void addStudent(String studentId, String studentName) {
boolean isToAdd = true;
// create student object
Student s = new Student(studentId, studentName);
// check if it is already in the list
for (Student student : students) {
if (student.getStudentId().equalsIgnoreCase(studentId))
{
System.out.println("Error !!! This student is already added.");
isToAdd = false;
break;
}
}
if (isToAdd) {
students.add(s);
}
}
// method to get String for course data
public String getCourseInfo() {
// first sort the data
Collections.sort(courses);
String s = null;
for (Course course : courses) {
s += course.toString() + " ";
}
return s;
}
// method to get String for student data
public String getStudentInfo() {
Collections.sort(students);
String s = "";
for (Student student : students) {
s += student.toString() + " ";
}
return s;
}
// method to add student to a course
public void addStudentToCourse(Course c, Student s) {
// first check the student in students
boolean studentPresent = false;
// first check the student
for (Student student : students) {
if (student.getStudentId().equalsIgnoreCase(s.getStudentId()))
{
studentPresent = true;
break;
}
}
if (!studentPresent) {
System.out.println("student is not present");
}
boolean coursePresent = false;
// first check the student in course
for (Course course : courses) {
if (course.getCourseId().equalsIgnoreCase(c.getCourseId())) {
coursePresent = true;
break;
}
}
if (!coursePresent) {
System.out.println("course is not present");
}
studentPresent = false;
// first check the student in course
for (Student student : c.getStudents()) {
if (student.getStudentId().equalsIgnoreCase(s.getStudentId()))
{
studentPresent = true;
break;
}
}
if (!studentPresent) {
System.out.println("student is not present");
} else {
// add student
c.getStudents().add(s);
System.out.println("Student ");
}
}
// method to remove student to a course
public void removeStudentFromCourse(Course c, Student s) {
// first check the student in students
boolean studentPresent = false;
// first check the student
for (Student student : students) {
if (student.getStudentId().equalsIgnoreCase(s.getStudentId()))
{
studentPresent = true;
break;
}
}
if (!studentPresent) {
System.out.println("student is not present");
}
boolean coursePresent = false;
// first check the student in course
for (Course course : courses) {
if (course.getCourseId().equalsIgnoreCase(c.getCourseId())) {
coursePresent = true;
break;
}
}
if (!coursePresent) {
System.out.println("course is not present");
}
studentPresent = false;
// first check the student in course
for (Student student : c.getStudents()) {
if (student.getStudentId().equalsIgnoreCase(s.getStudentId()))
{
studentPresent = true;
break;
}
}
if (!studentPresent) {
System.out.println("student is not present");
} else {
// add student
c.getStudents().remove(s);
System.out.println("Student ");
}
}
public String getStudentDataFromCourse(Course c) {
boolean coursePresent = false;
// first check the student in course
for (Course course : courses) {
if (course.getCourseId().equalsIgnoreCase(c.getCourseId())) {
coursePresent = true;
break;
}
}
if (!coursePresent) {
System.out.println("course is not present");
} else {
// first sort the data
Collections.sort(c.getStudents());
String s = "";
for (Student student : students) {
s += student.toString() + " ";
}
return s;
}
return null;
}
// method to get courses a student enrolled in
public List<Course> getCourseInfoOfAStudent(Student s) {
List<Course> courseList = new LinkedList<>();
// first check the student
boolean studentPresent = false;
// first check the student in course
for (Student student : students) {
if (student.getStudentId().equalsIgnoreCase(s.getStudentId()))
{
studentPresent = true;
break;
}
}
if (!studentPresent) {
System.out.println("student is not present");
} else {
for (Course course : courses) {
for (Student student : course.getStudents()) {
if (student.getStudentId().equalsIgnoreCase(s.getStudentId())) {
// add to list
courseList.add(course);
}
}
}
}
return courseList;
}
// method to get courses a student enrolled in
public String getCourseInfoOfAStudentAsString(Student s) {
String str = "";
// first check the student
boolean studentPresent = false;
// first check the student in course
for (Student student : students) {
if (student.getStudentId().equalsIgnoreCase(s.getStudentId()))
{
studentPresent = true;
break;
}
}
if (!studentPresent) {
System.out.println("student is not present");
} else {
for (Course course : courses) {
for (Student student : course.getStudents()) {
if (student.getStudentId().equalsIgnoreCase(s.getStudentId())) {
// add to list
str += course.toString() + " ";
}
}
}
}
return str;
}
}
package enrollmentmanagementsystem;
public class TestEnrollmentManager {
public static void main(String[] args) {
EnrollmentManager manager=new EnrollmentManager();
//Add students
manager.addStudent("111", "xxx");
manager.addStudent("222", "yyy");
manager.addStudent("333", "zzz");
manager.addStudent("444", "aaa");
manager.addStudent("555", "bbb");
manager.addStudent("666", "ccc");
manager.addStudent("777", "ddd");
manager.addStudent("888", "aaaaa");
//add courses
manager.addCourse("c1", "course 1", "instructor 1", "MTWF", "03:03:50", "1");
manager.addCourse("c2", "course 2", "instructor 2", "MTWF", "03:03:50", "2");
manager.addCourse("c3", "course 3", "instructor 3", "MTWF", "03:03:50", "3");
manager.addCourse("c4", "course 4", "instructor 4", "MTWF", "03:03:50", "4");
manager.addCourse("c5", "course 5", "instructor 5", "MTWF", "03:03:50", "5");
manager.addCourse("c6", "course 6", "instructor 6", "MTWF", "03:03:50", "6");
//print the information
System.out.println(manager.getStudentInfo());
System.out.println(manager.getCourseInfo());
//add duplicates
manager.addCourse("c1", "course 1", "instructor 1", "MTWF", "03:03:50", "1");
//print
System.out.println(manager.getCourseInfo());
//Please add some other test methods by your own .
}
}
output
Name:aaa
SID:444
Name:aaaaa
SID:888
Name:bbb
SID:555
Name:ccc
SID:666
Name:ddd
SID:777
Name:xxx
SID:111
Name:yyy
SID:222
Name:zzz
SID:333
nullCID: c1 Name: course 1 Prof:instructor 1 Days:MTWF Time:03:03:50 Room:1
CID: c2 Name: course 2 Prof:instructor 2 Days:MTWF Time:03:03:50 Room:2
CID: c3 Name: course 3 Prof:instructor 3 Days:MTWF Time:03:03:50 Room:3
CID: c4 Name: course 4 Prof:instructor 4 Days:MTWF Time:03:03:50 Room:4
CID: c5 Name: course 5 Prof:instructor 5 Days:MTWF Time:03:03:50 Room:5
CID: c6 Name: course 6 Prof:instructor 6 Days:MTWF Time:03:03:50 Room:6
Error !!! This course is already added.
nullCID: c1 Name: course 1 Prof:instructor 1 Days:MTWF Time:03:03:50 Room:1
CID: c2 Name: course 2 Prof:instructor 2 Days:MTWF Time:03:03:50 Room:2
CID: c3 Name: course 3 Prof:instructor 3 Days:MTWF Time:03:03:50 Room:3
CID: c4 Name: course 4 Prof:instructor 4 Days:MTWF Time:03:03:50 Room:4
CID: c5 Name: course 5 Prof:instructor 5 Days:MTWF Time:03:03:50 Room:5
CID: c6 Name: course 6 Prof:instructor 6 Days:MTWF Time:03:03:50 Room:6
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.