Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package studentcourseregistration;
- import java.util.*;
- import java.io.*;
- /**
- *
- * Steven Truong, Spencer Eslocer Csc1351
- *
- */
- public class StudentCourseRegistration {
- private Student[] students;
- private Course[] courses;
- LinkedList<Registration> registrations;
- public StudentCourseRegistration() {
- readStudentNamesEtc();
- readCourseNamesEtc();
- System.out.println("students: " + Arrays.toString(students));
- System.out.println("courses: " + Arrays.toString(courses));
- registrations = new LinkedList();
- }
- private void readStudentNamesEtc() {
- try {
- Scanner scan = new Scanner(new File("studentNamesEtc.dat"));
- int numStudents = scan.nextInt();
- scan.nextLine();
- students = new Student[numStudents];
- for (int i = 0; i < numStudents; i++) {
- String name = scan.next();
- int id = scan.nextInt();
- if (scan.hasNextLine()) {
- scan.nextLine();
- }
- students[i] = new Student(name, id);
- }
- scan.close();
- } catch (FileNotFoundException e) {
- System.out.println("studentNamesEtc.dat was not found");
- }
- }
- private void readCourseNamesEtc() {
- try {
- Scanner in = new Scanner(new File("courseNamesEtc.dat"));
- courses = new Course[in.nextInt()];
- in.nextLine();
- for (int i = 0; i < courses.length; i++) {
- String name = in.next(), id = in.next();
- int creditHrs = in.nextInt(), cost = in.nextInt(), maxClassSize = in.nextInt(), maxWaitSize = in.nextInt();
- if (0 == maxWaitSize) {
- Course newCourse = new Course(name, id, creditHrs, cost, maxClassSize);
- courses[i] = newCourse;
- } else {
- CourseWithWaitList newCourse = new CourseWithWaitList(name, id, creditHrs, cost, maxClassSize, maxWaitSize);
- courses[i] = newCourse;
- }
- in.nextLine();
- }
- in.close();
- } catch (FileNotFoundException e) {
- System.out.println("File not found.");
- }
- }
- int findStudent(int studentId) {
- for (int i = 0; i < students.length; i++) {
- if (studentId == students[i].id) {
- return i;
- }
- }
- return -1;
- }
- int findCourse(String courseId) {
- for (int i = 0; i < courses.length; i++) {
- if (courseId.equals(courses[i].id)) {
- return i;
- }
- }
- return -1;
- }
- private String AddRegistration(int studentId, String courseId) {
- boolean misMatch = false;
- ListIterator<Registration> it = registrations.listIterator();
- while (it.hasNext()) {
- Registration reg = it.next();
- if ((reg.studentRef.id == studentId) && (reg.courseRef.id.equals(courseId))) {
- misMatch = true;
- }
- }
- int studentIdIndex = findStudent(studentId);
- int courseIdIndex = findCourse(courseId);
- if (-1 == studentIdIndex) {
- System.out.println("*** Registration of (studentId=" + studentId + ", courseId=" + courseId + ") failed for invalid studentId");
- return "invalidStudentId";
- } else if (-1 == courseIdIndex) {
- System.out.println("*** Registration of (studentId=" + studentId + ", courseId=" + courseId + ") failed for invalid courseId");
- return "invalidCourseId";
- } else if (students[studentIdIndex].totalRegisteredCreditHrs + courses[courseIdIndex].creditHrs > Student.maxRegisteredCreditHours) {
- System.out.println("--- Registration of (studentId=" + studentId + ", courseId=" + courseId + ") failed due to maxRegisteredCreditHours = " + Student.maxRegisteredCreditHours + " limit");
- System.out.printf(" Your current totalRegisteredCreditHrs = %d and %s has creditHrs = %d\n", students[studentIdIndex].totalRegisteredCreditHrs, courseId, courses[courseIdIndex].creditHrs);
- return "maxRegisteredCreditHrs";
- } else if (students[studentIdIndex].numCoursesRegistered == Student.maxNumCourses) {
- System.out.println("--- Registration of (studentId=" + studentId + ", courseId=" + courseId + ") failed due to maxNumCourses = " + Student.maxNumCourses + " limit");
- return "maxNumRegisteredCourses";
- } else if (misMatch) {
- System.out.println("??? Registration of (studentId=" + studentId + ", courseId=" + courseId + ")failed due to existing registration of the same");
- return "existingRegistration";
- } else if (courses[courseIdIndex].classSize == courses[findCourse(courseId)].maxClassSize) {
- System.out.println("--- Registration of (studentId=" + studentId + ", courseId=" + courseId + ") failed due to maxClassSieze =" + courses[findCourse(courseId)].maxClassSize + " limit");
- if (courses[courseIdIndex] instanceof CourseWithWaitList) {
- if (((CourseWithWaitList) courses[courseIdIndex]).maxWaitListSize != 0) {
- for (int i = 0; i < ((CourseWithWaitList) courses[courseIdIndex]).numWaitListedStudentIds; i++) {
- int index = -1;
- CourseWithWaitList check = (CourseWithWaitList) courses[courseIdIndex];
- if (students[studentIdIndex].id == (check.waitListedStudentIds[i])) {
- return "existingWaitListRegistration";
- }
- }
- } else if (((CourseWithWaitList) courses[courseIdIndex]).addToWaitList(studentId) == -1) {
- return "waitListFull";
- } else {
- System.out.printf(" +++ You are waitListed with rank = %d /n",)
- return "addedToWaitList";
- }
- } else {
- System.out.printf("??? Registration of (studentID=%d, courseID=%s) failed; no wait list\n", studentId, courseId, Student.maxNumCourses);
- return "noWaitList";
- }
- } else {
- boolean prev = false;
- if (students[studentIdIndex].numCoursesRegistered > 0) {
- prev = true;
- }
- courses[courseIdIndex].classSize++;
- students[studentIdIndex].regCourseIds[students[studentIdIndex].index++] = courses[courseIdIndex];
- students[studentIdIndex].numCoursesRegistered++;
- students[studentIdIndex].totalRegisteredCreditHrs += courses[courseIdIndex].creditHrs;
- System.out.println("+++ " + students[studentIdIndex] + " is now registered in " + courses[courseIdIndex]);
- System.out.println(" Your numCoursesRegistered = " + students[studentIdIndex].numCoursesRegistered + "; totalRegisteredCreditHrs = " + students[studentIdIndex].totalRegisteredCreditHrs);
- if (prev) {
- it = registrations.listIterator();
- while (it.hasNext()) {
- Registration reg = it.next();
- if (reg.studentRef.id == studentId) {
- System.out.println(" your previous course-registrations: " + reg.courseRef.altString() + " ");
- }
- }
- }
- Registration addReg = new Registration(students[studentIdIndex], courses[courseIdIndex]);
- registrations.add(addReg);
- }
- return "addedToRegistration";
- }
- private void deleteRegistration(int studentId, String courseId) {
- ListIterator<Registration> it = registrations.listIterator();
- it = registrations.listIterator();
- while (it.hasNext()) {
- Registration reg = it.next();
- if (reg.studentRef.id == studentId && reg.courseRef.id == courseId) {
- it.remove();
- }
- }
- if (courses[findCourse(courseId)] instanceof CourseWithWaitList) {
- ((CourseWithWaitList) courses[findCourse(courseId)]).deleteFromWaitList(studentId);
- }
- }
- public static void main(String[] args) {
- StudentCourseRegistration test = new StudentCourseRegistration();
- test.AddRegistration(55, "cs0");
- test.AddRegistration(11, "cs5");
- test.AddRegistration(11, "ee2");
- test.AddRegistration(11, "ee3");
- test.AddRegistration(11, "cs0");
- test.AddRegistration(11, "cs1");
- test.AddRegistration(22, "ee2");
- test.AddRegistration(22, "ee2");
- test.AddRegistration(11, "cs0");
- test.AddRegistration(10, "ee2");
- test.AddRegistration(10, "ee2");
- test.AddRegistration(10, "ee3");
- test.AddRegistration(33, "ee2");
- test.AddRegistration(44, "ee2");
- test.AddRegistration(33, "cs0");
- test.AddRegistration(44, "cs0");
- test.deleteRegistration(11, "cs0");
- test.deleteRegistration(33, "ee2");
- test.deleteRegistration(11, "ee2");
- }
- }
- class Student {
- final String name;
- final int id;
- public int index = 0;
- int numCoursesRegistered = 0, totalRegisteredCreditHrs = 0;
- Course regCourseIds[] = new Course[maxNumCourses];
- static final int maxNumCourses = 2, maxRegisteredCreditHours = 4;
- Student(String name, int id) {
- this.name = name;
- this.id = id;
- }
- @Override
- public String toString() {
- return String.format("(%s, id=%d)", name, id);
- }
- }
- class Course {
- final String name, id;
- final int creditHrs, cost, maxClassSize;
- int classSize = 0; //current number of students enrolled
- Course(String name, String courseId, int creditHrs, int costInDollars, int maxSize) {
- this.name = name;
- this.id = courseId;
- this.creditHrs = creditHrs;
- this.cost = costInDollars;
- this.maxClassSize = maxSize;
- }
- @Override
- public String toString() {
- return String.format("(%s, id=%s, %dhrs, cost=%d)", name, id, creditHrs, cost);
- }
- public String altString() {
- return String.format("(%s, id=%s, %dhrs)", name, id, creditHrs);
- }
- }
- class Registration {
- final Student studentRef;
- final Course courseRef;
- final Date regDate = new Date();
- Registration(Student student, Course course) {
- this.studentRef = student;
- this.courseRef = course;
- }
- @Override
- public String toString() {
- return "(studentId=" + studentRef.name + ", courseId=" + courseRef.id + ")";
- }
- }
- ///////Start Here/////
- class CourseWithWaitList extends Course {
- int numWaitListedStudentIds, waitListedStudentIds[];
- final int maxWaitListSize;
- public CourseWithWaitList(String name, String id, int creditHrs, int cost, int maxClassSize, int maxWaitListSize) {
- super(name, id, creditHrs, cost, maxClassSize);
- this.maxWaitListSize = maxWaitListSize; //read from courseNamesEtc.dat file
- waitListedStudentIds = new int[maxWaitListSize];
- numWaitListedStudentIds = 0; //waitListSize = queueSize
- }
- public int addToWaitList(int studentId) {
- if (numWaitListedStudentIds < maxWaitListSize) {
- waitListedStudentIds[numWaitListedStudentIds++] = studentId;
- System.out.println(Arrays.toString(Arrays.copyOfRange(waitListedStudentIds, 0, numWaitListedStudentIds)));
- return (numWaitListedStudentIds - 1);
- }
- return -1;
- }
- public void deleteFromWaitList(int studentId) {
- int i;
- for (i = 0; i < numWaitListedStudentIds; i++) {
- if (studentId == waitListedStudentIds[i]) {
- numWaitListedStudentIds--;
- break;
- }
- }
- for (int j = i; j < maxWaitListSize - 1; j++) {
- waitListedStudentIds[j] = waitListedStudentIds[j + 1];
- }
- waitListedStudentIds[numWaitListedStudentIds] = 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement