In: Computer Science
Design and implement a program to allow a college administrator
to register new students and
add courses to the existing one. In addition, your program should
allow the administrator to do
the following:
• Add/Remove a new student
• Add/Remove a course to/from a selected student
• Search for a student either by last name or by id.
• Search for a course either by name or by id.
• Display a selected student along with the courses s/he is
registered for.
• List all registered students along with the courses each student
is registered for.
Using java
/*************************************Student.java**************************/
package student7;
import java.util.ArrayList;
public class Student {
private String id;
private String name;
private ArrayList<Course> courses;
public Student() {
this.id = "";
this.name = "";
this.courses = new
ArrayList<>();
}
public Student(String id, String name) {
super();
this.id = id;
this.name = name;
this.courses = new
ArrayList<>();
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void addCourse(Course course) {
courses.add(course);
}
public ArrayList<Course> getCourses() {
return courses;
}
@Override
public String toString() {
return "Student " + id + ", " +
name + ", courses: " + courses;
}
}
/*****************************Course.java******************************/
package student7;
public class Course {
private String id;
private String name;
public Course() {
this.id = "";
this.name = "";
}
public Course(String id, String name) {
super();
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Course "+ id + ", " +
name;
}
}
/***************************************Admin.java**********************/
package student7;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
public class Admin {
static ArrayList<Student> students;
static ArrayList<Course> courses;
public static void main(String[] args) {
students = new
ArrayList<>();
courses = new
ArrayList<>();
Scanner scan = new
Scanner(System.in);
int choice = 0;
do {
menu();
System.out.print("Please select any choice: ");
choice =
scan.nextInt();
switch (choice)
{
case 1:
addStudent();
break;
case 2:
removeStudent();
break;
case 3:
addCourse();
break;
case 4:
removeCourse();
break;
case 5:
searchStudentById();
break;
case 6:
searchStudentByName();
break;
case 7:
searchCourseById();
break;
case 8:
searchCourseByName();
break;
case 9:
displaySelectedStudent();
break;
case 10:
displayListOfStudent();
break;
case 11:
System.out.println("Thank you, Bye!");
break;
default:
System.out.println("Invalid Choice!");
break;
}
} while (choice != 11);
}
private static void addStudent() {
Scanner scan = new
Scanner(System.in);
System.out.print("Enter Student id:
");
String id = scan.nextLine();
System.out.print("Enter Student
name: ");
String name =
scan.nextLine();
students.add(new Student(id,
name));
}
private static void removeStudent() {
Scanner scan = new
Scanner(System.in);
System.out.print("Enter Student id:
");
String id = scan.nextLine();
int flag = 0;
Iterator<Student> itr =
students.iterator();
while (itr.hasNext()) {
Student
student = itr.next();
if
(student.getId().equalsIgnoreCase(id)) {
itr.remove();
flag = 1;
}
}
if (flag == 0) {
System.out.println("No Student found!");
}
}
private static void addCourse() {
Scanner scan = new
Scanner(System.in);
System.out.print("Enter Student id:
");
String id = scan.nextLine();
int flag = 0;
for (Student student : students)
{
if (student.getId().equalsIgnoreCase(id)) {
System.out.print("Enter Course id: ");
String idCourse = scan.nextLine();
System.out.print("Enter Course name: ");
String name = scan.nextLine();
student.addCourse(new Course(idCourse,
name));
courses.add(new Course(idCourse, name));
flag = 1;
}
}
if (flag == 0) {
System.out.println("No Student found!");
}
}
private static void removeCourse() {
Scanner scan = new
Scanner(System.in);
System.out.print("Enter Student id:
");
String id = scan.nextLine();
int flag = 0;
for (Student student : students)
{
if (student.getId().equalsIgnoreCase(id)) {
System.out.print("Enter Course id: ");
String idCourse = scan.nextLine();
Iterator<Course> itr =
student.getCourses().iterator();
while (itr.hasNext()) {
Course course =
itr.next();
if
(course.getId().equalsIgnoreCase(idCourse)) {
itr.remove();
flag =
1;
}
}
}
}
if (flag == 0) {
System.out.println("No Course found!");
}
}
private static void searchStudentById() {
Scanner scan = new
Scanner(System.in);
System.out.print("Enter Student id:
");
String id = scan.nextLine();
int flag = 0;
for (Student student : students)
{
if (student.getId().equalsIgnoreCase(id)) {
System.out.println(student.getId() + ", " +
student.getName());
flag = 1;
}
}
if (flag == 0) {
System.out.println("No Student Found by this id!");
}
}
private static void searchStudentByName() {
Scanner scan = new
Scanner(System.in);
System.out.print("Enter Student
name: ");
String name =
scan.nextLine();
int flag = 0;
for (Student student : students)
{
if (student.getName().equalsIgnoreCase(name)) {
System.out.println(student.getId() + ", " +
student.getName());
flag = 1;
}
}
if (flag == 0) {
System.out.println("No Student Found by this id!");
}
}
private static void searchCourseById() {
Scanner scan = new
Scanner(System.in);
System.out.print("Enter Course id:
");
String id = scan.nextLine();
int flag = 0;
for (Course course : courses) {
if (course.getId().equalsIgnoreCase(id)) {
System.out.println(course.toString());
flag = 1;
}
}
if (flag == 0) {
System.out.println("No Course Found!");
}
}
private static void searchCourseByName() {
Scanner scan = new
Scanner(System.in);
System.out.print("Enter Course
name: ");
String name =
scan.nextLine();
int flag = 0;
for (Course course : courses) {
if (course.getName().equalsIgnoreCase(name)) {
System.out.println(course.toString());
flag = 1;
}
}
if (flag == 0) {
System.out.println("No Course Found!");
}
}
private static void displaySelectedStudent() {
Scanner scan = new
Scanner(System.in);
System.out.print("Enter Student id:
");
String id = scan.nextLine();
int flag = 0;
for (Student student : students)
{
if (student.getId().equalsIgnoreCase(id)) {
System.out.println(student.toString());
flag = 1;
}
}
if (flag == 0) {
System.out.println("No Student Found by this id!");
}
}
private static void displayListOfStudent() {
Scanner scan = new
Scanner(System.in);
System.out.print("Enter Course
name: ");
String name =
scan.nextLine();
int flag = 0;
for (Student student : students)
{
for (int i = 0; i < courses.size(); i++) {
if (student.getCourses().get(i).getName().equalsIgnoreCase(name)) {
System.out.println(student.toString());
}
}
if (flag == 0)
{
System.out.println("No Student found!");
}
}
}
private static void menu() {
System.out.println("1. Add a
Student\n" + "2. Remove a Student\n" + "3. Add a Course to
Student\n"
+ "4. Remove a Course from Student\n" + "5.
Search Student by id\n" + "6. Search Student by name\n"
+ "7. Search Course by id\n" + "8. Search Course
by name\n" + "9. Display a selected Student\n"
+ "10. List Student with registered Course\n" +
"11. Exit");
}
}
/***************************output*******************************/
1. Add a Student
2. Remove a Student
3. Add a Course to Student
4. Remove a Course from Student
5. Search Student by id
6. Search Student by name
7. Search Course by id
8. Search Course by name
9. Display a selected Student
10. List Student with registered Course
11. Exit
Please select any choice: 1
Enter Student id: 101
Enter Student name: Virat
1. Add a Student
2. Remove a Student
3. Add a Course to Student
4. Remove a Course from Student
5. Search Student by id
6. Search Student by name
7. Search Course by id
8. Search Course by name
9. Display a selected Student
10. List Student with registered Course
11. Exit
Please select any choice: 3
Enter Student id: 101
Enter Course id: CSE101
Enter Course name: C Programming
1. Add a Student
2. Remove a Student
3. Add a Course to Student
4. Remove a Course from Student
5. Search Student by id
6. Search Student by name
7. Search Course by id
8. Search Course by name
9. Display a selected Student
10. List Student with registered Course
11. Exit
Please select any choice: 5
Enter Student id: 101
101, Virat
1. Add a Student
2. Remove a Student
3. Add a Course to Student
4. Remove a Course from Student
5. Search Student by id
6. Search Student by name
7. Search Course by id
8. Search Course by name
9. Display a selected Student
10. List Student with registered Course
11. Exit
Please select any choice: 6
Enter Student name: Virat
101, Virat
1. Add a Student
2. Remove a Student
3. Add a Course to Student
4. Remove a Course from Student
5. Search Student by id
6. Search Student by name
7. Search Course by id
8. Search Course by name
9. Display a selected Student
10. List Student with registered Course
11. Exit
Please select any choice: 7
Enter Course id: CSE101
Course CSE101, C Programming
1. Add a Student
2. Remove a Student
3. Add a Course to Student
4. Remove a Course from Student
5. Search Student by id
6. Search Student by name
7. Search Course by id
8. Search Course by name
9. Display a selected Student
10. List Student with registered Course
11. Exit
Please select any choice: 8
Enter Course name: C Programming
Course CSE101, C Programming
1. Add a Student
2. Remove a Student
3. Add a Course to Student
4. Remove a Course from Student
5. Search Student by id
6. Search Student by name
7. Search Course by id
8. Search Course by name
9. Display a selected Student
10. List Student with registered Course
11. Exit
Please select any choice: 9
Enter Student id: 101
Student 101, Virat, courses: [Course CSE101, C Programming]
1. Add a Student
2. Remove a Student
3. Add a Course to Student
4. Remove a Course from Student
5. Search Student by id
6. Search Student by name
7. Search Course by id
8. Search Course by name
9. Display a selected Student
10. List Student with registered Course
11. Exit
Please select any choice: 10
Enter Course name: C Programming
Student 101, Virat, courses: [Course CSE101, C Programming]
No Student found!
1. Add a Student
2. Remove a Student
3. Add a Course to Student
4. Remove a Course from Student
5. Search Student by id
6. Search Student by name
7. Search Course by id
8. Search Course by name
9. Display a selected Student
10. List Student with registered Course
11. Exit
Please select any choice: 11
Thank you, Bye!
Please let me know if you have any doubt or modify the answer, Thanks :)