Question 6.2: Your job is to define the above-mentioned classes as per the specification mentioned below and then write a Main/Driver class that demonstrates the functionalities of these classes.
Solution:
Notes: Please check this out, code is not 100% correct.
//Course Class
package com.company;
public class Course {
private String coureseId;
private String courseTitile;
private double creadit;
//private int numberOfStudents;
//faculty;
//studentList;
Course(){
}
public Course(String coureseId,String courseTitile,double creadit){
this.coureseId =coureseId;
this.courseTitile =courseTitile;
this.creadit =creadit;
}
public static void searchACourse(String searchId,Course[] courses){
for(int i=0;i<courses.length;i++){
if(courses[i].coureseId.equals(searchId)){
System.out.println(courses[i]);
}
}
}
@Override
public String toString() {
return "Course" +" "+
"coureseId: " + coureseId + " " +
" courseTitile: " + courseTitile + " " +
", creadit: " + creadit;
}
}
//Faculty Class
package com.company;
public class Faculty {
private String facultyId;
private String facltyName;
private String facultyposition;
Faculty(){
}
Faculty(String facultyId,String facltyName,String facultyposition){
this.facultyId =facultyId;
this.facltyName =facltyName;
this.facultyposition= facultyposition;
}
public static void searchAFacalty(String searchId,Faculty[] faculties){
for(int i=0;i<faculties.length;i++){
if(faculties[i].facultyId.equals(searchId)){
System.out.println(faculties[i]);
}
}
}
@Override
public String toString() {
return "Faculty" +" "+" faculty Id: " + facultyId + " " +
", faclty Name:" + facltyName + " " +
", faculty position: " +" "+ facultyposition + " ";
}
}
//main class
package com.company;
import java.util.Scanner;
public class Main {
public static int displayMenu(int number[], String item[]) {
//display menu ;
Scanner Obj = new Scanner(System.in);
System.out.println(" M E N U");
System.out.println("===========================");
for (int i = 0; i < number.length; i++) {
System.out.printf("%d. %s\n", number[i], item[i]);
}
System.out.println("============================");
System.out.println();
System.out.print("Enter your Opstion: ");
int choice = Obj.nextInt();
return choice;
}
//display Add Section
public static int AddScation(int[] number, String[] item) {
Scanner Obj = new Scanner(System.in);
System.out.println(" Add Section");
System.out.println("====================================");
for (int i = 0; i < number.length; i++) {
System.out.printf("%d. %s\n", number[i], item[i]);
}
System.out.println("=====================================");
System.out.println();
System.out.println("What you want to add: ");
int choice = Obj.nextInt();
return choice;
}
public static void main(String[] args) {
Scanner Obj = new Scanner(System.in);
int number[] = {1, 2, 3, 4, 5};
String item[] = {"Add", "Delete", "Update", "Print", "Search"};
Faculty[] facaltyArry = new Faculty[10000];
Student[] stdArry = new Student[10000];
Course[] courseArry = new Course[10000];
int studentCournter =0,course_counter =0, facalty_counter =0, hmfaculty=0,hmstudent=0,hmcourse=0;
while (true) {
int choice = displayMenu(number, item);
//import a value from the users
if (choice == 1) {
//add menu
int addNumber[] = {1, 2, 3};
String Additem[] = {"Add a Student", "Add a Course", "Add a Faculty"};
int choice11 = AddScation(addNumber, Additem);
if (choice11 == 1) {
//add a student section;
System.out.print("How many student you want to add: ");
int studentadd = Obj.nextInt();
for(int i=studentCournter;i<(studentadd+studentCournter);i++){
hmstudent++;
Obj.nextLine();
System.out.print("Enter the Student Id: ");
String student_Id = Obj.nextLine();
Obj.nextLine();
System.out.print("Enter the Student Name: ");
String student_Name = Obj.nextLine();
System.out.print("Student CGPA: ");
double student_CGPA = Obj.nextDouble();
Obj.nextLine();
stdArry[i] = new Student(student_Id,student_Name,student_CGPA);
}
studentCournter = studentCournter + studentadd;
System.out.println("====================================");
} else if (choice11 == 2) {
//add a course section
System.out.print("How many course you want to add: ");
int courseadd = Obj.nextInt();
System.out.println("====================================");
for(int i=course_counter;i<course_counter + courseadd;i++){
hmcourse++;
Obj.nextLine() ;
System.out.print("Enter the courseId: ");
String course_Id = Obj.nextLine();
System.out.print("Enter the coursetitle: ");
String course_Name = Obj.nextLine();
System.out.print("Student creadit: ");
double course_creadit = Obj.nextDouble();
Obj.nextLine();
System.out.println("");
courseArry[i] = new Course(course_Id,course_Name,course_creadit);
}
course_counter = course_counter + courseadd;
System.out.println("====================================");
} else if (choice11 == 3) {
//add a facalty section;
System.out.print("How many facalty you want to add: ");
int facaltyadd = Obj.nextInt();
System.out.println();
System.out.println("=========================================");System.out.println(facalty_counter);
for(int i=facalty_counter;i<(facaltyadd+facalty_counter);i++){
hmfaculty++;
System.out.print("Enter the Facalty Id: ");
String facalty_Id = Obj.next();
Obj.nextLine();
System.out.print("Enter the Facalty Name: ");
String faclty_Name = Obj.nextLine();
System.out.print("Enter the falclty Posion: ");
String falclty_position = Obj.nextLine();
System.out.println();
facaltyArry[i] = new Faculty(facalty_Id,faclty_Name,falclty_position);
}
facalty_counter = facalty_counter + facaltyadd;System.out.println(facalty_counter);
System.out.println("====================================");
}
} else if (choice == 2) {
//delete
System.out.println(" Delete Section");
System.out.println("=====================================");
System.out.println("1.Delete Student");
System.out.println("2.Delete Course ");
System.out.println("3.Delete Fecalty");
System.out.println("======================================");
System.out.println("");
System.out.println("Enter your Option:");
int value = Obj.nextInt();
if(value==1){
//delete Student
System.out.println("=========Delete Student============");
System.out.print("Enter the student ID You want to Delete: ");
String a = Obj.nextLine();
Obj.nextLine();
System.out.print("Enter the Student Name: ");
String b = Obj.nextLine();
Obj.nextLine();
System.out.print("Enter the Student CGPA: ");
double c = Obj.nextDouble();
Obj.nextLine();
Student temp = new Student(a,b,c);
Student.updateStudent(temp,stdArry);
}
else if(value==2){
//delete ourse
}
else if(value==3){
//delete facalty
}
} else if (choice == 3) {
//update
} else if (choice == 4) {
System.out.println(" Print Section");
System.out.println("====================================");
System.out.println("1. Print All Students");
System.out.println("2. Print All Course");
System.out.println("3. Print All faculties");
System.out.println("4. Print information Of a Student");
System.out.println("5. Print information of a couse");
System.out.println("6. Print information of a faculty");
System.out.println("7. Print student list and faculty information");
System.out.println("8. print Courses taken by a student");
System.out.println("=======================================");
System.out.print("What you want to Print: ");
int choice14 = Obj.nextInt();
if(choice14==1){
System.out.println("====================================");
System.out.println("==========Print All Students=========");
for(int i=0;i<hmstudent;i++)
{
System.out.println(stdArry[i]);
}
System.out.println("=======================================");
}
else if(choice14==2){
System.out.println("====================================");
System.out.println("==========Print All Courses=========");
for(int i=0;i<hmcourse;i++)
{
System.out.println(courseArry[i]);
}
}
else if(choice14==3){
System.out.println("====================================");
System.out.println("======Print All Facalty Mamber======");
for(int i=0;i<hmfaculty;i++)
{
System.out.println(facaltyArry[i]);
}
}
} else if (choice == 5) {
//seatch;
System.out.println("==============Search Section====================");
System.out.println("");
System.out.println("1. Search a Student");
System.out.println("2. Search a Course");
System.out.println("3. Search a Faculty");
System.out.println("4. Search Whether a Student take a course");
System.out.println("5. Search Whether a faculty teachers a course");
System.out.println("6. Search Courses taken by a student");
System.out.println("7. Search Courses taught by a faculty");
System.out.println();
System.out.println("===============================================");
System.out.println();
System.out.print("What you want to Search: ");
int search = Obj.nextInt();
System.out.println(" ");
System.out.println("===============================================");
if(search==1){
//search a student;
System.out.println();
System.out.println("===========Student Search Section===========");
System.out.println("Write the Student Id: ");
Obj.nextLine();
String temp = Obj.nextLine();
Student.searchAStudent(temp,stdArry);
}
else if(search == 2){
//search a course;
System.out.println("===========Course Search Section===========");
System.out.println("Write the Course code ID: ");
Obj.nextLine();
String temp = Obj.nextLine();
Course.searchACourse(temp,courseArry);
}
else if(search==3){
//search a faculty
System.out.println("===========Facalty Search Section===========");
System.out.println("Write the Facalty ID: ");
Obj.nextLine();
String temp = Obj.nextLine();
Faculty.searchAFacalty(temp,facaltyArry);
}
else if(search==4){
//search whether a student take a course
}
else if(search==5){
//search whether a faculty teachers a course
}
else if(search==6){
//Search Courses taken by a student
}
else if (search==7){
//Search Courses taught by a faculty
}
} else if (choice == 6) {
} else {
//exit
System.out.println("Good Bye ...Thank You");
}
}
}
}
//Student class
package com.company;
public class Student{
private String studentId;
private String studentName;
private double studentCGPA;
Student(){
}
public Student(String studentId,String studentName,double studentCGPA){
this.studentId =studentId;
this.studentName = studentName;
this.studentCGPA = studentCGPA;
}
public static void searchAStudent(String searchId,Student[] stdArry){
for(int i=0;i<stdArry.length;i++){
if(stdArry[i].studentId.equals(searchId)){
System.out.println(stdArry[i]);
}
}
}
public static void updateStudent(Student tamp,Student[] arry){
for(int i=0;i< arry.length;i++){
if(arry[i].studentId.equals(tamp.studentId) && arry[i].studentName.equals(tamp.studentName)
&& arry[i].studentCGPA == tamp.studentCGPA){
Student temp2 = new Student();
arry[i].studentId = null;
arry[i].studentName = null;
arry[i].studentCGPA = 0.0;
System.out.println(arry[i]);
}
}
}
@Override
public String toString() {
return "Student{" +
"studentId='" + studentId + '\'' +
", studentName='" + studentName + '\'' +
", studentCGPA=" + studentCGPA +
'}';
}
}