0% found this document useful (0 votes)
63 views

Import Import Import Class Private Private Private Private Int Public Super This New

This document defines classes for courses, students, and faculty members with methods to add, remove, and retrieve data from objects of these classes. It also contains a main method that allows a user to interactively: 1) Create objects of student, faculty, and course classes by inputting their details 2) Register a student in a course after checking that the faculty teaching it exists 3) Remove a student from a course 4) Display a student or course's information by ID
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views

Import Import Import Class Private Private Private Private Int Public Super This New

This document defines classes for courses, students, and faculty members with methods to add, remove, and retrieve data from objects of these classes. It also contains a main method that allows a user to interactively: 1) Create objects of student, faculty, and course classes by inputting their details 2) Register a student in a course after checking that the faculty teaching it exists 3) Remove a student from a course 4) Display a student or course's information by ID
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

import java.util.

ArrayList;
import java.util.List;
import java.util.Scanner;

class Course{

private String courseName;


private String courseCode;
private String[] students;
private int numberOfStudents;

public Course(String courseName){


super();
this.courseName = courseName;
numberOfStudents = 0;
students = new String[15]; // assuming,
maximum 15 students can enroll in one course...
}

public short RemoveStudent(String s) // return 1 for success, 0


for failure.
{
for(int i = 0; i<numberOfStudents; ++i)
{
if(students[i].equals(s))
{
for(int j = i; j<numberOfStudents - 1; ++j)
students[j] = students[j + 1];

numberOfStudents--;
return 1;
}
}
return 0;
}

public String getCourseName() {


return courseName;
}

public void addStudents(String student) {


this.students[numberOfStudents++] = student;
}

public String[] getStudents() {


return students;
}

public int getNumberOfStudents() {


return numberOfStudents;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}

public String getCourseCode() {


return courseCode;
}

public void setCourseCode(String courseCode) {


this.courseCode = courseCode;
}

class Student{

private String studentName;


private String studentId;
private String[] coursesRegistered;
private int numberOfCourses;

public Student(String studentName) {


super();
this.studentName = studentName;
numberOfCourses = 0;
coursesRegistered = new String[15]; //
assuming a Student can registered in maximum of 15 courses
}

public short RemoveCourse(String s) // return 1 for success, 0


for failure.
{
for(int i = 0; i<numberOfCourses; ++i)
{
if(coursesRegistered[i].equals(s))
{
for(int j = i; j<numberOfCourses - 1; ++j)
coursesRegistered[j] = coursesRegistered[j + 1];

numberOfCourses--;
return 1;
}
}
return 0;
}

public String getStudentName() {


return studentName;
}

public void setStudentName(String studentName) {


this.studentName = studentName;
}
public String getStudentId() {
return studentId;
}

public void setStudentId(String studentId) {


this.studentId = studentId;
}

public String[] getCoursesRegistered() {


return coursesRegistered;
}

public void setCoursesRegistered(String coursesRegistered) {


this.coursesRegistered[numberOfCourses++] = coursesRegistered;
}

public int getNumberOfCourses() {


return numberOfCourses;
}
}

class Faculty{

private String facultyName;


private String departmentName;
private String facultyId;
private String[] coursesTeaching;
private int numberOfCourses;

public Faculty(String facultyName)


{
super();
this.facultyName = facultyName;
coursesTeaching = new String[15]; //
assuming a teacher can teach maximum 15 courses
numberOfCourses = 0;
}

public String getFacultyName() {


return facultyName;
}

public void setFacultyName(String facultyName) {


this.facultyName = facultyName;
}

public String getDepartmentName() {


return departmentName;
}

public void setDepartmentName(String departmentName) {


this.departmentName = departmentName;
}

public String getFacultyId() {


return facultyId;
}

public void setFacultyId(String facultyId) {


this.facultyId = facultyId;
}

public String[] getCoursesTeaching() {


return coursesTeaching;
}

public void setCoursesTeaching(String coursesTeaching) {


this.coursesTeaching[numberOfCourses++] = coursesTeaching;
}
}

public class testRegistration {

public static void main(String[] args) {


// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);

List<Student> students = new ArrayList<Student>();


// creating A List of Students
List<Faculty> faculty = new ArrayList<Faculty>();
// creating A List of Faculty
List<Course> courses = new ArrayList<Course>();
// creating A List of Course

int id = 0;

byte choice;
String tempInput;
boolean flag;

do {

System.out.println("\n\nPlease Select any Option(Type in the


Option Number):\n");
System.out.println("1) Input data of a new student (create a new
object of student class)");
System.out.println("2) Input data of a new faculty member (create
a new object of faculty) ");
System.out.println("3) Create a new course ");
System.out.println("4) Register student in the course ");
System.out.println("5) Remove a student from a course");
System.out.println("6) Display a student’s information by giving
his/her ID");
System.out.println("7) Display a course information by giving its
ID");
System.out.println("8) Exit from the program");
System.out.println("\nEnter your Choice(1-8): ");

choice = input.nextByte();
input.nextLine(); // skipping \n charater left by
nextByte

switch (choice)
{
case 1:
System.out.print("Please Enter the Name of a Student: ");
tempInput = input.nextLine();

Student s = new Student(tempInput);


// add a new Student
s.setStudentId(Integer.toString(++id));

students.add(s);
// adding the object to the list
System.out.println("A Student with an ID " + id + " has
been successfully Added");

break;

case 2:
{
System.out.print("Please Enter the Name of a Faculty
Member: ");
tempInput = input.nextLine();

Faculty f = new Faculty(tempInput);


// add a new faculty
f.setFacultyId(Integer.toString(++id));

System.out.print("Please Enter the Name of the Department:


");
tempInput = input.nextLine();
f.setDepartmentName(tempInput);

faculty.add(f);
// adding the faculty object to the list
System.out.println("A Faculty with an ID " + id + " has
been successfully Added");
break;
}

case 3:
{
flag = false;

System.out.print("Pease Enter the name of the Course: ");


tempInput = input.nextLine();

System.out.print("Please Enter the name of the Teacher to


teach this Course: ");
String tempInput_2 = input.nextLine();

// checking the existence of the teacher


for(int i = 0; i<faculty.size(); ++i)
{
Faculty f = faculty.get(i);

if(f.getFacultyName().equals(tempInput_2))
{
f.setCoursesTeaching(tempInput);
faculty.set(i, f);

flag = true;
break;
}
}

if(flag == true) {
Course c = new Course(tempInput); //add a new
course
c.setCourseCode(Integer.toString(++id));

courses.add(c);
System.out.println("A Course with an ID " + id + "
has been successfully Added");
}
else
System.out.print("Faculty Member Not Found, Please
Try Again\n");

break;
}

//case 4 and 5 have almost the same code


case 4:
case 5:
{
flag = false;
System.out.print("Please Enter the ID of the Student: ");
tempInput = input.nextLine();

// checking the existence of the student

int studentIndex = 0;
for( ; studentIndex < students.size(); ++studentIndex)
{
Student temp = students.get(studentIndex);

if(tempInput.equals(temp.getStudentId()))
//Student successfully found
{
flag = true;
break;
}
}

if(flag==false)
System.out.println("There is no Such Student in our
Record. Please try Again");

else
{
flag = false;
// resetting the flag to reuse it.

System.out.print("Please Enter the Name of the


Course: ");
String tempInput_2 = input.nextLine();

int courseIndex = 0;
for( ; courseIndex < courses.size(); ++courseIndex)
{
Course temp = courses.get(courseIndex);

if(tempInput_2.equals(temp.getCourseName()))
//Course successfully found
{
flag = true;

if(choice == 4)
// Add Student in course
{
// updating students and course
object

Student temp_1 =
students.get(studentIndex);

temp_1.setCoursesRegistered(tempInput_2);
students.set(studentIndex,
temp_1);

Course temp_2 =
courses.get(courseIndex);
temp_2.addStudents(tempInput);
courses.set(courseIndex,
temp_2);

System.out.println("A Student: "


+ temp_1.getStudentName() + " is Successfully Registered in Course: " +
temp_2.getCourseName());
break;
}

else if(choice == 5)
// Remove student from course
{
Student temp_1 =
students.get(studentIndex);

temp_1.RemoveCourse(tempInput_2);
students.set(studentIndex,
temp_1);

Course temp_2 =
courses.get(courseIndex);
temp_2.RemoveStudent(tempInput);
courses.set(courseIndex,
temp_2);

System.out.println("A Student: "


+ temp_1.getStudentName() + " is Successfully Removed from Course: " +
temp_2.getCourseName());
break;
}
}
}

if(flag == false)
System.out.println("There is no Such Course
in our Record. Please try Again");
}
break;
}

case 6:
{
flag = false;

System.out.print("Please Enter the ID of the student to


Display the Data: ");
tempInput = input.nextLine();

for(Student temp : students)


{
if(temp.getStudentId().equals(tempInput))
// Student successfully found
{
System.out.println("\nStudent Name: " +
temp.getStudentName());
System.out.print("Registered Courses: ");

String regCourses[] =
temp.getCoursesRegistered();
for(int i = 0; i<temp.getNumberOfCourses();
++i)
System.out.print(regCourses[i] + " ");
System.out.println("");

flag = true;
break;
}
}

if(flag == false)
System.out.print("Student Not Found, Please Try
Again" );

break;
}

case 7:
{
flag = false;

System.out.print("Please Enter the Code of the course: ");


tempInput = input.nextLine();

for( Course temp : courses)


{
if(temp.getCourseCode().equals(tempInput))
{
System.out.println("\nCourse Name: " +
temp.getCourseName());
System.out.print("All IDs of Students
Registered: ");

String regStudents[] = temp.getStudents();


for(int i = 0; i<temp.getNumberOfStudents();
++i)
System.out.print(regStudents[i] + " ");

System.out.println("");
flag = true;
break;
}
}

if(flag == false)
System.out.print("Course Not Found, Please Try
Again" );

break;
}

case 8:
System.out.println("Exiting the Program. Thanks!");
break;

default:
System.out.print("\nInvalid Option. Try again");
} // switch

} // do-while
while(choice != 8);

} // main
} //class

You might also like