Advertisement
Profithaha

Untitled

Apr 24th, 2014
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package studentcourseregistration;
  2.  
  3. import java.util.*;
  4. import java.io.*;
  5.  
  6. /**
  7.  *
  8.  * Steven Truong, Spencer Eslocer Csc1351
  9.  *
  10.  */
  11. public class StudentCourseRegistration {
  12.  
  13.     private Student[] students;
  14.     private Course[] courses;
  15.     LinkedList<Registration> registrations;
  16.  
  17.     public StudentCourseRegistration() {
  18.         readStudentNamesEtc();
  19.         readCourseNamesEtc();
  20.         System.out.println("students: " + Arrays.toString(students));
  21.         System.out.println("courses: " + Arrays.toString(courses));
  22.         registrations = new LinkedList();
  23.     }
  24.  
  25.     private void readStudentNamesEtc() {
  26.         try {
  27.             Scanner scan = new Scanner(new File("studentNamesEtc.dat"));
  28.             int numStudents = scan.nextInt();
  29.             scan.nextLine();
  30.             students = new Student[numStudents];
  31.             for (int i = 0; i < numStudents; i++) {
  32.                 String name = scan.next();
  33.                 int id = scan.nextInt();
  34.                 if (scan.hasNextLine()) {
  35.                     scan.nextLine();
  36.                 }
  37.                 students[i] = new Student(name, id);
  38.             }
  39.             scan.close();
  40.         } catch (FileNotFoundException e) {
  41.             System.out.println("studentNamesEtc.dat was not found");
  42.         }
  43.     }
  44.  
  45.     private void readCourseNamesEtc() {
  46.         try {
  47.             Scanner in = new Scanner(new File("courseNamesEtc.dat"));
  48.             courses = new Course[in.nextInt()];
  49.             in.nextLine();
  50.             for (int i = 0; i < courses.length; i++) {
  51.                 String name = in.next(), id = in.next();
  52.                 int creditHrs = in.nextInt(), cost = in.nextInt(), maxClassSize = in.nextInt(), maxWaitSize = in.nextInt();
  53.                 if (0 == maxWaitSize) {
  54.                     Course newCourse = new Course(name, id, creditHrs, cost, maxClassSize);
  55.                     courses[i] = newCourse;
  56.                 } else {
  57.                     CourseWithWaitList newCourse = new CourseWithWaitList(name, id, creditHrs, cost, maxClassSize, maxWaitSize);
  58.                     courses[i] = newCourse;
  59.                 }
  60.                 in.nextLine();
  61.             }
  62.             in.close();
  63.         } catch (FileNotFoundException e) {
  64.             System.out.println("File not found.");
  65.         }
  66.     }
  67.  
  68.     int findStudent(int studentId) {
  69.         for (int i = 0; i < students.length; i++) {
  70.             if (studentId == students[i].id) {
  71.                 return i;
  72.             }
  73.         }
  74.         return -1;
  75.     }
  76.  
  77.     int findCourse(String courseId) {
  78.         for (int i = 0; i < courses.length; i++) {
  79.             if (courseId.equals(courses[i].id)) {
  80.                 return i;
  81.             }
  82.         }
  83.         return -1;
  84.     }
  85.  
  86.     private String AddRegistration(int studentId, String courseId) {
  87.         boolean misMatch = false;
  88.         ListIterator<Registration> it = registrations.listIterator();
  89.         while (it.hasNext()) {
  90.             Registration reg = it.next();
  91.             if ((reg.studentRef.id == studentId) && (reg.courseRef.id.equals(courseId))) {
  92.                 misMatch = true;
  93.             }
  94.         }
  95.         int studentIdIndex = findStudent(studentId);
  96.         int courseIdIndex = findCourse(courseId);
  97.         if (-1 == studentIdIndex) {
  98.             System.out.println("*** Registration of (studentId=" + studentId + ", courseId=" + courseId + ") failed for invalid studentId");
  99.             return "invalidStudentId";
  100.         } else if (-1 == courseIdIndex) {
  101.             System.out.println("*** Registration of (studentId=" + studentId + ", courseId=" + courseId + ") failed for invalid courseId");
  102.             return "invalidCourseId";
  103.         } else if (students[studentIdIndex].totalRegisteredCreditHrs + courses[courseIdIndex].creditHrs > Student.maxRegisteredCreditHours) {
  104.             System.out.println("--- Registration of (studentId=" + studentId + ", courseId=" + courseId + ") failed due to maxRegisteredCreditHours = " + Student.maxRegisteredCreditHours + " limit");
  105.             System.out.printf("    Your current totalRegisteredCreditHrs = %d and %s has creditHrs = %d\n", students[studentIdIndex].totalRegisteredCreditHrs, courseId, courses[courseIdIndex].creditHrs);
  106.             return "maxRegisteredCreditHrs";
  107.         } else if (students[studentIdIndex].numCoursesRegistered == Student.maxNumCourses) {
  108.             System.out.println("--- Registration of (studentId=" + studentId + ", courseId=" + courseId + ") failed due to maxNumCourses = " + Student.maxNumCourses + " limit");
  109.             return "maxNumRegisteredCourses";
  110.         } else if (misMatch) {
  111.             System.out.println("??? Registration of (studentId=" + studentId + ", courseId=" + courseId + ")failed due to existing registration of the same");
  112.             return "existingRegistration";
  113.         } else if (courses[courseIdIndex].classSize == courses[findCourse(courseId)].maxClassSize) {
  114.             System.out.println("--- Registration of (studentId=" + studentId + ", courseId=" + courseId + ") failed due to maxClassSieze =" + courses[findCourse(courseId)].maxClassSize + " limit");
  115.  
  116.             if (courses[courseIdIndex] instanceof CourseWithWaitList) {
  117.                 if (((CourseWithWaitList) courses[courseIdIndex]).maxWaitListSize != 0) {
  118.                     for (int i = 0; i < ((CourseWithWaitList) courses[courseIdIndex]).numWaitListedStudentIds; i++) {
  119.                         int index = -1;
  120.                         CourseWithWaitList check = (CourseWithWaitList) courses[courseIdIndex];
  121.                         if (students[studentIdIndex].id == (check.waitListedStudentIds[i])) {
  122.                             return "existingWaitListRegistration";
  123.                         }
  124.                     }
  125.                 } else if (((CourseWithWaitList) courses[courseIdIndex]).addToWaitList(studentId) == -1) {
  126.                     return "waitListFull";
  127.                 } else {
  128.                     System.out.printf("   +++ You are waitListed with rank = %d /n",)
  129.                     return "addedToWaitList";
  130.                 }
  131.             } else {
  132.                 System.out.printf("??? Registration of (studentID=%d, courseID=%s) failed; no wait list\n", studentId, courseId, Student.maxNumCourses);
  133.                return "noWaitList";
  134.             }
  135.         } else {
  136.             boolean prev = false;
  137.             if (students[studentIdIndex].numCoursesRegistered > 0) {
  138.                 prev = true;
  139.             }
  140.             courses[courseIdIndex].classSize++;
  141.             students[studentIdIndex].regCourseIds[students[studentIdIndex].index++] = courses[courseIdIndex];
  142.             students[studentIdIndex].numCoursesRegistered++;
  143.             students[studentIdIndex].totalRegisteredCreditHrs += courses[courseIdIndex].creditHrs;
  144.             System.out.println("+++ " + students[studentIdIndex] + " is now registered in " + courses[courseIdIndex]);
  145.             System.out.println("    Your numCoursesRegistered = " + students[studentIdIndex].numCoursesRegistered + "; totalRegisteredCreditHrs = " + students[studentIdIndex].totalRegisteredCreditHrs);
  146.             if (prev) {
  147.                 it = registrations.listIterator();
  148.                 while (it.hasNext()) {
  149.                     Registration reg = it.next();
  150.                     if (reg.studentRef.id == studentId) {
  151.                         System.out.println("    your previous course-registrations: " + reg.courseRef.altString() + " ");
  152.                     }
  153.                 }
  154.             }
  155.             Registration addReg = new Registration(students[studentIdIndex], courses[courseIdIndex]);
  156.             registrations.add(addReg);
  157.         }
  158.         return "addedToRegistration";
  159.     }
  160.  
  161.     private void deleteRegistration(int studentId, String courseId) {
  162.         ListIterator<Registration> it = registrations.listIterator();
  163.         it = registrations.listIterator();
  164.         while (it.hasNext()) {
  165.             Registration reg = it.next();
  166.             if (reg.studentRef.id == studentId && reg.courseRef.id == courseId) {
  167.                 it.remove();
  168.             }
  169.         }
  170.         if (courses[findCourse(courseId)] instanceof CourseWithWaitList) {
  171.             ((CourseWithWaitList) courses[findCourse(courseId)]).deleteFromWaitList(studentId);
  172.         }
  173.  
  174.     }
  175.  
  176.     public static void main(String[] args) {
  177.         StudentCourseRegistration test = new StudentCourseRegistration();
  178.        test.AddRegistration(55, "cs0");
  179.        test.AddRegistration(11, "cs5");
  180.        test.AddRegistration(11, "ee2");
  181.        test.AddRegistration(11, "ee3");
  182.        test.AddRegistration(11, "cs0");
  183.        test.AddRegistration(11, "cs1");
  184.        test.AddRegistration(22, "ee2");
  185.        test.AddRegistration(22, "ee2");
  186.        test.AddRegistration(11, "cs0");
  187.        test.AddRegistration(10, "ee2");
  188.        test.AddRegistration(10, "ee2");
  189.        test.AddRegistration(10, "ee3");
  190.        test.AddRegistration(33, "ee2");
  191.        test.AddRegistration(44, "ee2");
  192.        test.AddRegistration(33, "cs0");
  193.        test.AddRegistration(44, "cs0");
  194.        test.deleteRegistration(11, "cs0");
  195.        test.deleteRegistration(33, "ee2");
  196.        test.deleteRegistration(11, "ee2");
  197.     }
  198. }
  199.  
  200. class Student {
  201.  
  202.     final String name;
  203.     final int id;
  204.     public int index = 0;
  205.     int numCoursesRegistered = 0, totalRegisteredCreditHrs = 0;
  206.     Course regCourseIds[] = new Course[maxNumCourses];
  207.     static final int maxNumCourses = 2, maxRegisteredCreditHours = 4;
  208.  
  209.     Student(String name, int id) {
  210.         this.name = name;
  211.         this.id = id;
  212.     }
  213.  
  214.     @Override
  215.     public String toString() {
  216.         return String.format("(%s, id=%d)", name, id);
  217.     }
  218. }
  219.  
  220. class Course {
  221.  
  222.     final String name, id;
  223.     final int creditHrs, cost, maxClassSize;
  224.     int classSize = 0; //current number of students enrolled
  225.  
  226.     Course(String name, String courseId, int creditHrs, int costInDollars, int maxSize) {
  227.         this.name = name;
  228.         this.id = courseId;
  229.         this.creditHrs = creditHrs;
  230.         this.cost = costInDollars;
  231.         this.maxClassSize = maxSize;
  232.     }
  233.  
  234.     @Override
  235.     public String toString() {
  236.         return String.format("(%s, id=%s, %dhrs, cost=%d)", name, id, creditHrs, cost);
  237.     }
  238.  
  239.     public String altString() {
  240.         return String.format("(%s, id=%s, %dhrs)", name, id, creditHrs);
  241.     }
  242. }
  243.  
  244. class Registration {
  245.  
  246.     final Student studentRef;
  247.     final Course courseRef;
  248.     final Date regDate = new Date();
  249.  
  250.     Registration(Student student, Course course) {
  251.         this.studentRef = student;
  252.         this.courseRef = course;
  253.     }
  254.  
  255.     @Override
  256.     public String toString() {
  257.         return "(studentId=" + studentRef.name + ", courseId=" + courseRef.id + ")";
  258.     }
  259. }
  260.  
  261. ///////Start Here/////
  262. class CourseWithWaitList extends Course {
  263.  
  264.     int numWaitListedStudentIds, waitListedStudentIds[];
  265.     final int maxWaitListSize;
  266.  
  267.     public CourseWithWaitList(String name, String id, int creditHrs, int cost, int maxClassSize, int maxWaitListSize) {
  268.         super(name, id, creditHrs, cost, maxClassSize);
  269.         this.maxWaitListSize = maxWaitListSize; //read from courseNamesEtc.dat file
  270.         waitListedStudentIds = new int[maxWaitListSize];
  271.         numWaitListedStudentIds = 0; //waitListSize = queueSize
  272.     }
  273.  
  274.     public int addToWaitList(int studentId) {
  275.         if (numWaitListedStudentIds < maxWaitListSize) {
  276.             waitListedStudentIds[numWaitListedStudentIds++] = studentId;
  277.             System.out.println(Arrays.toString(Arrays.copyOfRange(waitListedStudentIds, 0, numWaitListedStudentIds)));
  278.             return (numWaitListedStudentIds - 1);
  279.         }
  280.         return -1;
  281.     }
  282.  
  283.     public void deleteFromWaitList(int studentId) {
  284.         int i;
  285.         for (i = 0; i < numWaitListedStudentIds; i++) {
  286.             if (studentId == waitListedStudentIds[i]) {
  287.                 numWaitListedStudentIds--;
  288.                 break;
  289.             }
  290.         }
  291.         for (int j = i; j < maxWaitListSize - 1; j++) {
  292.             waitListedStudentIds[j] = waitListedStudentIds[j + 1];
  293.         }
  294.         waitListedStudentIds[numWaitListedStudentIds] = 0;
  295.  
  296.     }
  297. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement