jeudi 30 juin 2016

problems with Classes/Objects

I posted earlier my question. my code was messy. I apologise for that.

I rewrite it.

when I run it choice number 1 and 2 works fine but when i try choice number 3 and 4 for some reason it doesn't work properly like the output example.

Any help would be appreciated. thanks in advance

import java.util.Scanner;

public class Problem2 {

public static void main(String[] args) {

    while (true) {

        Scanner in = new Scanner(System.in);

        System.out.println("1.Enroll Student");
        System.out.print("");
        System.out.println("2.Offer Course");
        System.out.println("3.Registration ");
        System.out.println("4.Enter Result ");


        int choice = in.nextInt();

        if (choice == 1) {

            StudentClass[] studentList = new StudentClass[500];

            studentList[0] = new StudentClass();

            while (true) {

                System.out.print(" Enter name of the student: ");
                studentList[0].setName(in.next());

                System.out.println("");
                System.out.print("The student is given the unique ID =  " + studentList[0].getUniqueStudentID() + " and the number of students =  " + studentList[0].getNumberOfStudents());

                System.out.println("");

                System.out.print("Enter the e-mail address of the student: ");

                studentList[0].setEmail(in.next());

                System.out.print("Enter the semester number of the student:  ");

                studentList[0].setSemesterNumber(in.nextInt());

                System.out.print(" Add another student (Y/N): ");
                char add = in.next().charAt(0);
                if (add == 'Y' || add == 'y') {

                }

                if (add == 'N' || add == 'n') {
                    break;
                }

            }

        } else if (choice == 2) {

            while (true) {

                int num;

                CourseClass[] courseOfferedList;
                courseOfferedList = new CourseClass[500];
                courseOfferedList[0] = new CourseClass();

                System.out.print("Enter the course code : ");
                courseOfferedList[0].setCourseCode(in.next());

                System.out.println(" Enter the course title: ");
                courseOfferedList[0].setCourseTitl(in.next());

                System.out.println(" Enter the credit hours of the course: ");
                courseOfferedList[0].setCreditHours(in.nextInt());

                System.out.print(" Add another course (Y/N): ");
                char add2 = in.next().charAt(0);
                if (add2 == 'Y' || add2 == 'y') {
                }

                if (add2 == 'N' || add2 == 'n') {
                    break;
                }

            }

        } else if (choice == 3) {

            RegistrationClass[] registrationList;
            registrationList = new RegistrationClass[500];
            registrationList[0] = new RegistrationClass();

            CourseClass[] courseOfferedList;
            courseOfferedList = new CourseClass[500];
            courseOfferedList[0] = new CourseClass();

            StudentClass[] studentList = new StudentClass[500];
            studentList[0] = new StudentClass();

            while (true) {

                while (true) {
                    System.out.print("Enter the unique student ID: ");

                    registrationList[0].setUniqueStudentID(in.nextInt());

                    if (registrationList[0].getUniqueStudentID() == studentList[0].getUniqueStudentID()) {

                        while (true) {
                            System.out.print("Enter the course code: ");
                            registrationList[0].setCourseCode(in.next());

                            if (registrationList[0].getCourseCode().equalsIgnoreCase(courseOfferedList[0].getCourseCode())) {

                                System.out.println("The registration is given the unique ID = " + registrationList[0].getUniqueRegistrationID() + " and the number of registrations =  " + registrationList[0].getNumberOfRegistrations());

                                System.out.print(" Add another course (Y/N): ");
                                char add2 = in.next().charAt(0);
                                if (add2 == 'Y' || add2 == 'y') {

                                }

                                if (add2 == 'N' || add2 == 'n') {

                                    break;

                                }

                            } else {
                                System.out.println("The course has not been offered yet, try again");

                            }

                        }

                    } else {
                        System.out.println("The student does not exit, try again ");
                    }

                }

            }

        } else if (choice == 4) {

            while (true) {

                RegistrationClass[] registrationList;
                registrationList = new RegistrationClass[500];
                registrationList[0] = new RegistrationClass();

                StudentClass[] studentList;
                studentList = new StudentClass[500];
                studentList[0] = new StudentClass();

                CourseClass[] courseOfferedList;
                courseOfferedList = new CourseClass[500];
                courseOfferedList[0] = new CourseClass();

                System.out.println("Enter the registration ID: ");

                registrationList[0].setUniqueRegistrationID(in.nextInt());

                System.out.print(" Enter marks of " + studentList[0].getName() + " in " + courseOfferedList[0].getCourseCode() + " (" + courseOfferedList[0].getCourseTitl() + ") :");

                System.out.println("");
                System.out.print(" Add another registration (Y/N): ");
                char add2 = in.next().charAt(0);

                if (add2 == 'Y' || add2 == 'y') {

                }

                if (add2 == 'N' || add2 == 'n') {

                    break;
                }

            }

        } 

    }
}

}

class RegistrationClass {

private int uniqueRegistrationID;
private int uniqueStudentID;
private String courseCode;
private int marks;
static int numberOfRegistrations = 0;

public RegistrationClass() {
    uniqueRegistrationID = 0;

    uniqueStudentID = 0;

    courseCode = null;

    numberOfRegistrations = 0;

}

public int getUniqueRegistrationID() {
    return uniqueRegistrationID++;
}

public void setUniqueRegistrationID(int uniqueRegistrationID) {
    this.uniqueRegistrationID = uniqueRegistrationID;
}

public int getUniqueStudentID() {
    return uniqueStudentID++;
}

public void setUniqueStudentID(int uniqueStudentID) {
    this.uniqueStudentID = uniqueStudentID;
}

public String getCourseCode() {
    return courseCode;
}

public void setCourseCode(String courseCode) {
    this.courseCode = courseCode;
}

public int getMarks() {
    return marks;
}

public void setMarks(int marks) {
    this.marks = marks;
}

public static int getNumberOfRegistrations() {
    return numberOfRegistrations++;
}

public static void setNumberOfRegistrations(int numberOfRegistrations) {
    RegistrationClass.numberOfRegistrations = numberOfRegistrations;
}

}

class StudentClass {

private String name;
private int uniqueStudentID;
private String email;
private int mobileNumber;
private int semesterNumber;
static int numberOfStudents = 0;

public StudentClass() {

    name = null;
    uniqueStudentID = 1;
    email = null;
    mobileNumber = 0;
    semesterNumber = 0;
    numberOfStudents = 1;

}

public void setName(String name) {
    this.name = name;
}

public void setUniqueStudentID(int uniqueStudentID) {
    this.uniqueStudentID = uniqueStudentID;
}

public void setEmail(String email) {
    this.email = email;
}

public void setMobileNumber(int mobileNumber) {
    this.mobileNumber = mobileNumber;
}

public void setSemesterNumber(int semesterNumber) {
    this.semesterNumber = semesterNumber;
}

public void setNumberOfStudents(int numberOfStudents) {
    this.numberOfStudents = numberOfStudents;
}

public String getName() {

    return name;
}

public int getUniqueStudentID() {

    return uniqueStudentID++;

}

public String getEmail() {

    return email;

}

public int getMobileNumber() {

    return mobileNumber;
}

public int getSemesterNumber() {

    return semesterNumber;
}

public int getNumberOfStudents() {

    return numberOfStudents++;
}

void setName() {

}

void setuniqueStudentID() {

}

void setnumberOfStudents() {

}

void setsemesterNumber() {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

void setmobileNumber() {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

void setemail() {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

void setemail(String next) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

void setsemesterNumber(int nextInt) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

}

class CourseClass {

private String courseCode;
private String courseTitl;
private int creditHours;
static int numberOfCourses = 0;

public CourseClass() {

    courseCode = null;
    courseTitl = null;
    creditHours = 0;
    numberOfCourses = 0;

}

public String getCourseCode() {
    return courseCode;
}

public void setCourseCode(String courseCode) {
    this.courseCode = courseCode;
}

public String getCourseTitl() {
    return courseTitl;
}

public void setCourseTitl(String courseTitl) {
    this.courseTitl = courseTitl;
}

public int getCreditHours() {
    return creditHours;
}

public void setCreditHours(int creditHours) {
    this.creditHours = creditHours;
}

public static int getNumberOfCourses() {
    return numberOfCourses;
}

public static void setNumberOfCourses(int numberOfCourses) {
    CourseClass.numberOfCourses = numberOfCourses;
}

boolean setCourseCode() {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

}

this is how the output should look like

is used for input and < is used for output

{Display the menu, Choose the menu item}

1

Enter name of the student: john

< The student is given the unique ID = 1 and the number of students = 1

Enter the e-mail address of the student: john@stu.kau.edu.uk

Enter the semester number of the student: 3 Add another student (Y/N): Y Enter name of the student: Ibrahim

< The student is given the unique ID = 2 and the number of students = 2

Enter the e-mail address of the student: Ibrahim@stu.kau.edu.uk

Enter the semester number of the student: 3 Add another student (Y/N): N

{Display the menu, Choose the menu item}

2 Enter the course code: CPCS203

Enter the course title: Programming 2

Enter the credit hours of the course: 4 Add another course (Y/N): Y

Enter the course code: CPCS204

Enter the course title: Data Structures

Enter the credit hours of the course: 3 Add another course (Y/N): N

{Display the menu, Choose the menu item}

3

Enter the unique student ID: 3

< The student does not exit, try again

Enter the unique student ID: 1

Enter the course code: CPCS205

< The course has not been offered yet, try again

Enter the course code: CPCS203

< The registration is given the unique ID = 1 and the number of registrations = 1

Add another registration (Y/N): Y

Enter the unique student ID: 1

Enter the course code: CPCS204

< The registration is given the unique ID = 2 and the number of registrations = 2

Add another registration (Y/N): Y

Enter the unique student ID: 2

Enter the course code: CPCS204

< The registration is given the unique ID = 3 and the number of registrations = 3

Add another registration (Y/N): Y

Enter the unique student ID: 2

Enter the course code: CPCS204

< The registration is given the unique ID = 4 and the number of registrations = 4

Add another registration (Y/N): N

{Display the menu, Choose the menu item}

4

Enter the registration ID: 1

Enter marks of john in CPCS203 (Programming 2): 85

Add another registration (Y/N): Y

Enter the registration ID: 2

Enter marks of john in CPCS204 (Data Structures): 60

Add another registration (Y/N): Y

Enter the registration ID: 3

Enter marks of Ibrahim in CPCS203 (Programming 2): 95

Add another registration (Y/N): Y

Enter the registration ID: 4

Enter marks of Ibrahim in CPCS204 (Data Structures): 85

Add another registration (Y/N): N

{Display the menu, Choose the menu item}

Aucun commentaire:

Enregistrer un commentaire