Advertisement
yuzicode

javadoc1

Apr 12th, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 21.77 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package maincontrol;
  7.  
  8. import java.io.*;
  9. import java.security.*;
  10. import java.text.*;
  11. import java.util.*;
  12. import javax.crypto.*;
  13. import javax.crypto.spec.SecretKeySpec;
  14.  
  15. /**
  16.  * School is the over-arching class that contains an array list of student objects, an array list of course objects and an array list of users.
  17.  * Ties these 3 array lists together.
  18.  * @author Aaron Wee, Chern Yu Zi
  19.  *
  20.  */
  21. public class School {
  22.     /**
  23.      * A counter to use for 'for loop'.
  24.      */
  25.     private int i;
  26.     /**
  27.      * Total number of courses in school. Initialized to be the number of course files read.
  28.      */
  29.     private int totalNumOfCourses = Course.checkNumberOfCourses();
  30.     /**
  31.      * Total number of users in school. Initialized to be the number of user files read (includes students as well).
  32.      */
  33.     private int totalNumUser = User.checkNumberOfUser();
  34.     /**
  35.      * Array list containing all courses available in school.
  36.      */
  37.     public  ArrayList<Course> coursesAvailable;
  38.     /**
  39.      * Array list containing all students in school.
  40.      */
  41.     public  ArrayList<Student> studentBody;
  42.     /**
  43.      * Array list containing all users in school.
  44.      */
  45.     public  ArrayList<User> userList;
  46.    
  47.     /**
  48.      * Creates a new school with the specified parameters that describes the school.
  49.      */
  50.     public School() {
  51.         studentBody = new ArrayList<Student>();
  52.         coursesAvailable = new ArrayList<Course>();
  53.         userList = new ArrayList<User>();
  54.     }
  55.  
  56.     /**
  57.      * Get the information of the course files and user files to read into the application at the start.
  58.      * @param sch School object to add course objects and user objects into
  59.      * @return the number of admin
  60.      * @throws IOException Throws the IO exception
  61.      * @throws ParseException Throws the parse exception
  62.      */
  63.     public static int getInfo(School sch) throws IOException, ParseException{ //*
  64.        
  65.         DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
  66.        
  67.         int numUser = sch.totalNumUser;
  68.         int numCourse = sch.totalNumOfCourses;
  69.         String strTemp, tempU, tempP, tempSD, tempED, tempA, tempN, tempM, tempG, tempC, tempS, vTemp = new String();
  70.         String cTemp, iTemp, sTemp, austrTemp, tempAdd1, tempAdd2 = new String();
  71.         String ltTemp, lgTemp, ldTemp, lstTemp, letTemp, lcTemp, lrTemp, eTemp, pTemp, optTemp =new String();
  72.         int  auTemp,stTemp,etTemp, tempV;
  73.         Date sTime, eTime;
  74.         int temp;
  75.         int numAdmin = 0;
  76.         int stdNum=0;
  77.        
  78.             for(temp = 0; temp<numCourse;temp++){
  79.            
  80.             strTemp = Integer.toString(temp+1);
  81.             FileReader frStream = new FileReader("course/cs"+strTemp+".txt");
  82.             BufferedReader brStream = new BufferedReader(frStream);
  83.            
  84.             cTemp = brStream.readLine();
  85.             iTemp = brStream.readLine();
  86.             sTemp = brStream.readLine();
  87.             vTemp = brStream.readLine();
  88.             tempV = Integer.parseInt(vTemp);
  89.             austrTemp = brStream.readLine();
  90.             auTemp = Integer.parseInt(austrTemp);
  91.             sch.coursesAvailable.add(new Course(cTemp,iTemp,sTemp,tempV,auTemp));
  92.             ltTemp = brStream.readLine();
  93.            
  94.        
  95.             while(ltTemp.equals("//") == false){
  96.                
  97.                 lgTemp = brStream.readLine();
  98.                 ldTemp = brStream.readLine();
  99.                 lstTemp =  brStream.readLine();
  100.                 stTemp = Integer.parseInt(lstTemp);
  101.                 letTemp =  brStream.readLine();
  102.                 etTemp = Integer.parseInt(letTemp);
  103.                 lcTemp = brStream.readLine();
  104.                 lrTemp = brStream.readLine();
  105.                
  106.                 sch.coursesAvailable.get(temp).setClass(ltTemp, lgTemp, ldTemp, stTemp, etTemp , lcTemp, lrTemp);
  107.                
  108.                 ltTemp = brStream.readLine();
  109.             }
  110.            
  111.             brStream.close();
  112.            
  113.             }
  114.             for(temp = 0; temp<numUser;temp++){    //Declaring all user values
  115.                
  116.                 strTemp = Integer.toString(temp+1);
  117.                 FileReader frStream = new FileReader("users/u"+strTemp+".txt");
  118.                 BufferedReader brStream = new BufferedReader(frStream);
  119.            
  120.                 tempU = brStream.readLine();
  121.                 tempP = brStream.readLine();
  122.                 tempSD = brStream.readLine();
  123.                 tempED = brStream.readLine();
  124.                 tempA = brStream.readLine();
  125.            
  126.                 sTime = df.parse(tempSD);
  127.                 eTime = df.parse(tempED);
  128.            
  129.                 sch.userList.add(new User(tempU,tempP,sTime,eTime,tempA));
  130.                
  131.                 if(tempA.equals("admin"))
  132.                 numAdmin++;
  133.        
  134.  
  135.    
  136.                 if(tempA.equals("student")){
  137.                 tempN = brStream.readLine();
  138.                 tempM = brStream.readLine();
  139.                 tempG = brStream.readLine();
  140.                 tempC = brStream.readLine();
  141.                 tempS = brStream.readLine();
  142.                 eTemp = brStream.readLine();
  143.                 pTemp = brStream.readLine();
  144.                 optTemp = brStream.readLine();
  145.                 sch.studentBody.add(new Student(tempN,tempM,tempG,tempC,tempS,eTemp,pTemp,optTemp));
  146.                 tempAdd1 = brStream.readLine();
  147.                
  148.                     while(tempAdd1.equals("//") == false){
  149.                    
  150.                         tempAdd2 = brStream.readLine();
  151.                
  152.                         sch.studentBody.get(stdNum).addCourse(tempAdd1, tempAdd2,sch);
  153.                        
  154.                         tempAdd1 = brStream.readLine();
  155.                
  156.                         }
  157.                    
  158.         String tempAdd3, tempAdd4, tempAdd5 = new String();   // waiting list input modification starts here
  159.         int intTempAdd5;
  160.         int j;
  161.    
  162.    
  163.  
  164.     tempAdd3 = brStream.readLine();  
  165.  
  166.     while(tempAdd3.equals("//") == false){
  167.  
  168.         tempAdd4 = brStream.readLine();
  169.         tempAdd5 =  brStream.readLine();
  170.        
  171.          intTempAdd5 = Integer.parseInt(tempAdd5);
  172.        
  173.         for(j=0;j<numCourse;j++){
  174.             if(tempAdd4.equals(sch.coursesAvailable.get(j).getIndexNumber())){
  175.             sch.coursesAvailable.get(j).addToWaitList(sch.studentBody.get(stdNum),intTempAdd5);
  176.         }
  177.        
  178.     }
  179.         tempAdd3 = brStream.readLine();
  180.     }
  181.     stdNum++;
  182. }
  183.    
  184.     brStream.close();
  185.  
  186.     }
  187. for (int i = 0; i < numCourse;i++){
  188.     sch.coursesAvailable.get(i).sortWaitList();
  189. }
  190. return numAdmin;
  191.     }
  192.    
  193.     /**
  194.      * Get the number of courses in school.
  195.      * @return number of courses in school
  196.      */
  197.    
  198.     public int getNumCourse() {                        
  199.             return totalNumOfCourses;
  200.     }
  201.    
  202.     /**
  203.      * Get the array list of courses available in school.
  204.      * @return the array list of courses available
  205.      */
  206.     public ArrayList<Course> getCourses() {
  207.             return this.coursesAvailable;
  208.     }
  209.    
  210.     /**
  211.      * Get the size of the courses object array list in school.
  212.      * @return size of the array list
  213.      */
  214.     public int getCoursesAvailableSize() {  
  215.             return coursesAvailable.size();
  216.         }
  217.    
  218.     /**
  219.      * Get the number of courses in school.
  220.      * @return number of courses in school.
  221.      */
  222.     public int getTotalNumOfCourses(){
  223.            return totalNumOfCourses;
  224.     }
  225.    
  226.     /**
  227.      * Print all course objects' index number and course code currently in the school object one by one.
  228.      * @param sch School object containing all the course objects to be printed
  229.      */
  230.     public static void printCoursesAvailable (School sch) { //*
  231.         int i;
  232.         System.out.println("\nCourses available for students at the moment:");
  233.         for (i = 0; i < sch.coursesAvailable.size(); i++) {
  234.             System.out.println(i+1 + ". " + sch.coursesAvailable.get(i).getIndexNumber() + " " + sch.coursesAvailable.get(i).getCourseCode());
  235.         }
  236.         System.out.println();
  237.     }
  238.    
  239.     /**
  240.      * Print all the Courses available in the database in order, to aid the student in adding his course
  241.      * @param sch School object containing all the course objects to be printed
  242.      */
  243.     public static void printSortedCourses(School sch){
  244.         int i, j, z = 0, printedSize = 0;
  245.         boolean print = true;
  246.         String[] printed = new String[100];
  247.         for (i=0;i<sch.coursesAvailable.size();i++){
  248.             printedSize++;
  249.             for (j=0;j<printedSize;j++){
  250.                if (sch.coursesAvailable.get(i).getCourseCode() .equals (printed[j])){
  251.                   print = false;
  252.                   break;
  253.                }
  254.                print = true;
  255.                }
  256.                if (print == true){
  257.                  printed[printedSize-1] = sch.coursesAvailable.get(i).getCourseCode();
  258.                  System.out.println("");
  259.                  System.out.println("Course Code:");
  260.                  System.out.println(sch.coursesAvailable.get(i).getCourseCode());
  261.                  System.out.println("Available course index are:");
  262.                  System.out.println(sch.coursesAvailable.get(i).getIndexNumber());
  263.                  for (z = (i+1);z<sch.coursesAvailable.size();z++){
  264.                      if (sch.coursesAvailable.get(z).getCourseCode() . equals (sch.coursesAvailable.get(i).getCourseCode())){
  265.                          System.out.println(sch.coursesAvailable.get(z).getIndexNumber());
  266.                              
  267.                  }
  268.              }
  269.          }
  270.      }
  271.        
  272.        
  273.        
  274. }
  275.     /**
  276.      * Find course in school by course index number (FEP1, FEP2) and return true if found or false if not found.
  277.      * @param sch School object containing the course
  278.      * @param indexNumber Index number to identify the course needed
  279.      * @return true if course is found or false if course is not found
  280.      */
  281.     public boolean findCourse(School sch, String indexNumber) {
  282.         for (Course course : sch.coursesAvailable) {
  283.             if (course.getIndexNumber().equals(indexNumber)) {
  284.                 return true;
  285.             }
  286.         }
  287.         return false;
  288.     }
  289.    
  290.     /**
  291.      * Get course in school using the course's index number (FEP1, FEP2).
  292.      * @param indexNumber Index number of the course you want to get
  293.      * @return course object of the index number or null if no such course object of the index number exists
  294.      */
  295.     public Course getCourse(String indexNumber) {
  296.         for (Course course : this.coursesAvailable) {
  297.             if (course.getIndexNumber().equals(indexNumber)) {
  298.                 return course;
  299.             }
  300.         }
  301.         return null;
  302.     }
  303.    
  304.     /**
  305.      * Get index of course object in the course object array list in school object.
  306.      * @param course Course object you want to find the index of in the array list
  307.      * @return the index of the course in the course array list
  308.      */
  309.     public int indexOfCourseObject(Course course) {        
  310.         return this.coursesAvailable.indexOf(course);
  311.     }
  312.    
  313.    
  314.     public Course getCourseViaIndexOf(int i) {    //NO NEED THIS FUNCTION          
  315.         return this.coursesAvailable.get(i);
  316.     }
  317.    
  318.     /**
  319.      * Print a course object's details (Course code, Course index number, School of course and Course vacancy)
  320.      * by parsing in the desired course object.
  321.      * @param course The course that will have its details printed
  322.      */
  323.    
  324.     public void printCourse(Course course) {
  325.         System.out.println("Course code: " + course.getCourseCode());
  326.         System.out.println("Course index number: " + course.getIndexNumber());
  327.         System.out.println("School of course: " + course.getSchool());
  328.         System.out.println("Course vacancy: " + course.getVacancy());
  329.         System.out.println(" "); //print lessons information
  330.     }      
  331.    
  332.     /**
  333.      * Add a course object into the course object array list of the school object.
  334.      * @param courseCode Course code attribute of the course object
  335.      * @param indexNumber Index number attribute of the course object
  336.      * @param school School attribute of the course object
  337.      * @param vacancy Vacancy attribute of the course object
  338.      * @param AU AU attribute of the course object
  339.      * @param sch School to assess the course object array list to add the new course into
  340.      */
  341.    
  342.     public void addCourse(String courseCode, String indexNumber, String school, int vacancy, int AU, School sch) {
  343.         System.out.println("Adding course...");
  344.         sch.coursesAvailable.add(new Course(courseCode, indexNumber, school, vacancy, AU));
  345.         System.out.println("Course added!");
  346.     }
  347.    
  348.     /**
  349.      * Print a course's current enrollment against the entire possible class size for that course.
  350.      * @param course Course object to print its enrollment against the course object's class size
  351.      */
  352.    
  353.     public void checkCourseVacancy(Course course) {
  354.         int enrolment = course.getVacancy() - course.getCurrentEnrollment();
  355.         System.out.println("Vacancy of course: " + enrolment + "/" + course.getVacancy());
  356.     }
  357.    
  358.     /**
  359.      * Get the size of the student object array list in school.
  360.      * @return the size of the array list
  361.      */
  362.    
  363.     public int getStudentBodySize() {
  364.         return studentBody.size();
  365.     }
  366.    
  367.     /**
  368.      * Get a particular student's matriculation number based on the index of student object of the student in the student object array list.
  369.      * @param i Index number of student object of the student
  370.      * @return the matriculation number of the student
  371.      */
  372.    
  373.     public String getStudentBodyMatricNo(int i) {
  374.         return this.studentBody.get(i).getMatricNo();
  375.     }
  376.    
  377.     /**
  378.      * Print the entire student object array list in the school object, displaying each student object's name and matriculation number.
  379.      *
  380.      */
  381.    
  382.     public void printStudentBody() {  //*
  383.         System.out.println("\nStudents currently enrolled in NTU:");
  384.         for (i = 0; i < this.studentBody.size(); i++) {
  385.             System.out.println(i+1 + ". " + this.studentBody.get(i).getName() + ", " + this.studentBody.get(i).getMatricNo());
  386.         }
  387.         System.out.println();
  388.     }
  389.    
  390.     /**
  391.      * Add a new student object into the student object array list of the school object.
  392.      * @param name Name attribute of the student object
  393.      * @param matricNo Matriculation number attribute of the student object
  394.      * @param gender Gender attribute of the student object
  395.      * @param nationality Nationality attribute of the student object
  396.      * @param programme Programme attribute of the student object
  397.      * @param email Email attribute of the student object
  398.      * @param handphoneNum Handphone number attribute of the student object
  399.      * @param notificationOption Notification option attribute of the student object
  400.      */
  401.    
  402.     public void addNewStudent(String name, String matricNo, String gender, String nationality, String programme, String email,String handphoneNum,String notificationOption) {
  403.         this.studentBody.add(new Student(name, matricNo, gender, nationality, programme, email, handphoneNum, notificationOption));
  404.     }
  405.    
  406.    
  407.     public Student getStudentViaMatric(String MatricNo) {       //NO NEED THIS FUNCTION
  408.         for (Student student : this.studentBody) {
  409.             if (student.getMatricNo().equals(MatricNo)) {
  410.             return student;
  411.             }
  412.         }
  413.     return null;
  414.     }
  415.    
  416.    
  417.     /**
  418.      * Get index of student object in the student object array list in school object.
  419.      * @param student Student object you want to find the index of in the array list
  420.      * @return the index of the student in the student array list
  421.      */
  422.    
  423.     public int indexOfStudentObject(Student student) {        
  424.         return this.studentBody.indexOf(student);
  425.     }
  426.    
  427.     /**
  428.      * Get student object in student object array list of the school object
  429.      * @param indexNumber Index number of the student object in the student object array list
  430.      * @return student object of the index number or null if no such student object of the index number exists
  431.      */
  432.    
  433.     public Student getStudent(int i) {            
  434.         return this.studentBody.get(i);
  435.     }
  436.      
  437.     /**
  438.      * Updates a student's course's course code to a new one. Note that it is the course code of a specific course index that will be changed.
  439.      * Not all courses with the old course code will be changed.
  440.      * @param oldCourseCode Old course code that needs to be changed
  441.      * @param newCourseCode New course code
  442.      * @param indexNumber The index number of the the course that needs to have its course code changed
  443.      */
  444.    
  445.     public void updateStudentCourseCode(String oldCourseCode, String newCourseCode, String indexNumber) {
  446.     int i, j;
  447.     int temp1;
  448.     String[] temp2;
  449.     String[] temp3;
  450.    
  451.     for (i = 0; i < studentBody.size(); i++) {
  452.         temp1 = studentBody.get(i).getnoOfCourses();
  453.         temp2 = studentBody.get(i).getStudentCourse();
  454.         temp3 = studentBody.get(i).getStudentIndexNumber();
  455.        
  456.         for (j = 0; j < temp1; j++) {
  457.             if (temp3[j].equals(indexNumber)) {
  458.                 if (temp2[j].equals(oldCourseCode)) {
  459.                 temp2[j] = newCourseCode;
  460.                 }
  461.             }
  462.         }
  463.     }
  464.    
  465. }
  466.     /**
  467.      * Updates a student's course's course index number to a new course index number.
  468.      * @param oldIndex Old course index number that needs to be changed.
  469.      * @param newIndex New course index
  470.      */
  471.    
  472.     public void updateStudentIndexNumber(String oldIndex, String newIndex) {
  473.         int i, j;
  474.         int temp1;
  475.         String[] temp2;
  476.        
  477.         for (i = 0; i < studentBody.size(); i++) {
  478.             temp1 = studentBody.get(i).getnoOfCourses();
  479.             temp2 = studentBody.get(i).getStudentIndexNumber();
  480.            
  481.             for (j = 0; j < temp1; j++) {
  482.                 if (temp2[j].equals(oldIndex)) {
  483.                     temp2[j] = newIndex;
  484.                 }
  485.             }
  486.         }
  487.        
  488.     }
  489.    
  490.     /**
  491.      * Print the student name and matriculation number of the students in a particular course index.
  492.      * @param strTemp Course index number
  493.      */
  494.                
  495.     public void sortStudentBodyAccordingIndex(String strTemp) {              
  496.              
  497.         int i,j,k;
  498.         String strCourse = new String();
  499.         boolean foundCourse = false;
  500.        
  501.         strCourse = strTemp;
  502.      
  503.        
  504.         for(i=0;i<coursesAvailable.size();i++){
  505.             if(coursesAvailable.get(i).getIndexNumber().equals(strCourse.toUpperCase()) == true){
  506.                
  507.                 for(j=0;j<studentBody.size();j++){
  508.                    
  509.                     for(k=0;k<studentBody.get(j).numofRegCourse();k++){
  510.                         if(coursesAvailable.get(i).getIndexNumber().equals(studentBody.get(j).studentIndexNumber[k])){
  511.                             System.out.println(studentBody.get(j).getName()+" "+studentBody.get(j).getMatricNo());
  512.                         }
  513.                     }
  514.                 }
  515.                 foundCourse = true;
  516.                 System.out.print("\n");
  517.                 break;
  518.             }
  519.         }
  520.  
  521.        
  522.         if(foundCourse == false){
  523.             System.out.print("\n");
  524.             System.out.println("Index not found\n");
  525.         }
  526.              
  527.     }
  528.    
  529.     /**
  530.      * Print the student name and matriculation number of the students of a particular course code.
  531.      * @param inputText Course code
  532.      */
  533.    
  534.     public void sortStudentBodyAccordingCourse(String inputText) {
  535.        
  536.         int i,j,k;
  537.         String strCourse = inputText;
  538.         boolean foundCourse = false;
  539.        
  540.         for(i=0;i<coursesAvailable.size();i++){
  541.             if(coursesAvailable.get(i).getCourseCode().equals(strCourse.toUpperCase()) == true){
  542.                
  543.                 for(j=0;j<studentBody.size();j++){
  544.                    
  545.                     for(k=0;k<studentBody.get(j).numofRegCourse();k++){
  546.                         if(coursesAvailable.get(i).getCourseCode().equals(studentBody.get(j).studentCourse[k])){
  547.                             System.out.println(studentBody.get(j).getName()+" "+studentBody.get(j).getMatricNo());
  548.                         }
  549.                     }
  550.                 }
  551.                 foundCourse = true;
  552.                 System.out.print("\n");
  553.                 break;
  554.             }
  555.         }
  556.        
  557.        
  558.         if(foundCourse == false){
  559.             System.out.print("\n");
  560.             System.out.println("Course not found\n");
  561.         }
  562.        
  563.     }
  564.         /**
  565.          * Find user in school by userid and return true if found or false if not found.
  566.          * @param sch School object containing the user
  567.          * @param userName Userid of user
  568.          * @return
  569.          */
  570.    
  571.         public boolean findUserId(School sch, String userName) {
  572.         for (User user : sch.userList) {
  573.             if (user.getUser().equals(userName)) {
  574.                 return true;
  575.             }
  576.         }
  577.         return false;
  578.         }
  579.        
  580.         /**
  581.          * Encrypts a password string.
  582.          * @param pw Password
  583.          * @return encrypted string of the password
  584.          * @throws NoSuchAlgorithmException Throws no such algorithm exception
  585.          * @throws NoSuchPaddingException Throws no such padding exception
  586.          * @throws InvalidKeyException Throws invalid key exception exception
  587.          * @throws IllegalBlockSizeException Throws illegal block size exception
  588.          * @throws BadPaddingException Throws bad padding exception
  589.          */
  590.        
  591.         protected static String encrypt(String pw) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException{
  592.             Cipher AesCipher = Cipher.getInstance("AES");
  593.            
  594.             byte[] convertPass = pw.getBytes();
  595.             String encryptedKey = new String();
  596.            
  597.             String key1 = "1234567891012345";   //create manual key
  598.             SecretKeySpec secret =  new SecretKeySpec(key1.getBytes(), "AES");
  599.            
  600.             AesCipher.init(Cipher.ENCRYPT_MODE, secret);
  601.             byte[] byteCipherText  = AesCipher.doFinal(convertPass);
  602.            
  603.             for(byte b : byteCipherText){
  604.                 encryptedKey = encryptedKey + String.format("%02X", b);
  605.         }
  606.        
  607.         return encryptedKey;
  608.     }
  609.  
  610.    
  611.    
  612. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement