Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package maincontrol;
- import java.io.*;
- import java.security.*;
- import java.text.*;
- import java.util.*;
- import javax.crypto.*;
- import javax.crypto.spec.SecretKeySpec;
- /**
- * 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.
- * Ties these 3 array lists together.
- * @author Aaron Wee, Chern Yu Zi
- *
- */
- public class School {
- /**
- * A counter to use for 'for loop'.
- */
- private int i;
- /**
- * Total number of courses in school. Initialized to be the number of course files read.
- */
- private int totalNumOfCourses = Course.checkNumberOfCourses();
- /**
- * Total number of users in school. Initialized to be the number of user files read (includes students as well).
- */
- private int totalNumUser = User.checkNumberOfUser();
- /**
- * Array list containing all courses available in school.
- */
- public ArrayList<Course> coursesAvailable;
- /**
- * Array list containing all students in school.
- */
- public ArrayList<Student> studentBody;
- /**
- * Array list containing all users in school.
- */
- public ArrayList<User> userList;
- /**
- * Creates a new school with the specified parameters that describes the school.
- */
- public School() {
- studentBody = new ArrayList<Student>();
- coursesAvailable = new ArrayList<Course>();
- userList = new ArrayList<User>();
- }
- /**
- * Get the information of the course files and user files to read into the application at the start.
- * @param sch School object to add course objects and user objects into
- * @return the number of admin
- * @throws IOException Throws the IO exception
- * @throws ParseException Throws the parse exception
- */
- public static int getInfo(School sch) throws IOException, ParseException{ //*
- DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
- int numUser = sch.totalNumUser;
- int numCourse = sch.totalNumOfCourses;
- String strTemp, tempU, tempP, tempSD, tempED, tempA, tempN, tempM, tempG, tempC, tempS, vTemp = new String();
- String cTemp, iTemp, sTemp, austrTemp, tempAdd1, tempAdd2 = new String();
- String ltTemp, lgTemp, ldTemp, lstTemp, letTemp, lcTemp, lrTemp, eTemp, pTemp, optTemp =new String();
- int auTemp,stTemp,etTemp, tempV;
- Date sTime, eTime;
- int temp;
- int numAdmin = 0;
- int stdNum=0;
- for(temp = 0; temp<numCourse;temp++){
- strTemp = Integer.toString(temp+1);
- FileReader frStream = new FileReader("course/cs"+strTemp+".txt");
- BufferedReader brStream = new BufferedReader(frStream);
- cTemp = brStream.readLine();
- iTemp = brStream.readLine();
- sTemp = brStream.readLine();
- vTemp = brStream.readLine();
- tempV = Integer.parseInt(vTemp);
- austrTemp = brStream.readLine();
- auTemp = Integer.parseInt(austrTemp);
- sch.coursesAvailable.add(new Course(cTemp,iTemp,sTemp,tempV,auTemp));
- ltTemp = brStream.readLine();
- while(ltTemp.equals("//") == false){
- lgTemp = brStream.readLine();
- ldTemp = brStream.readLine();
- lstTemp = brStream.readLine();
- stTemp = Integer.parseInt(lstTemp);
- letTemp = brStream.readLine();
- etTemp = Integer.parseInt(letTemp);
- lcTemp = brStream.readLine();
- lrTemp = brStream.readLine();
- sch.coursesAvailable.get(temp).setClass(ltTemp, lgTemp, ldTemp, stTemp, etTemp , lcTemp, lrTemp);
- ltTemp = brStream.readLine();
- }
- brStream.close();
- }
- for(temp = 0; temp<numUser;temp++){ //Declaring all user values
- strTemp = Integer.toString(temp+1);
- FileReader frStream = new FileReader("users/u"+strTemp+".txt");
- BufferedReader brStream = new BufferedReader(frStream);
- tempU = brStream.readLine();
- tempP = brStream.readLine();
- tempSD = brStream.readLine();
- tempED = brStream.readLine();
- tempA = brStream.readLine();
- sTime = df.parse(tempSD);
- eTime = df.parse(tempED);
- sch.userList.add(new User(tempU,tempP,sTime,eTime,tempA));
- if(tempA.equals("admin"))
- numAdmin++;
- if(tempA.equals("student")){
- tempN = brStream.readLine();
- tempM = brStream.readLine();
- tempG = brStream.readLine();
- tempC = brStream.readLine();
- tempS = brStream.readLine();
- eTemp = brStream.readLine();
- pTemp = brStream.readLine();
- optTemp = brStream.readLine();
- sch.studentBody.add(new Student(tempN,tempM,tempG,tempC,tempS,eTemp,pTemp,optTemp));
- tempAdd1 = brStream.readLine();
- while(tempAdd1.equals("//") == false){
- tempAdd2 = brStream.readLine();
- sch.studentBody.get(stdNum).addCourse(tempAdd1, tempAdd2,sch);
- tempAdd1 = brStream.readLine();
- }
- String tempAdd3, tempAdd4, tempAdd5 = new String(); // waiting list input modification starts here
- int intTempAdd5;
- int j;
- tempAdd3 = brStream.readLine();
- while(tempAdd3.equals("//") == false){
- tempAdd4 = brStream.readLine();
- tempAdd5 = brStream.readLine();
- intTempAdd5 = Integer.parseInt(tempAdd5);
- for(j=0;j<numCourse;j++){
- if(tempAdd4.equals(sch.coursesAvailable.get(j).getIndexNumber())){
- sch.coursesAvailable.get(j).addToWaitList(sch.studentBody.get(stdNum),intTempAdd5);
- }
- }
- tempAdd3 = brStream.readLine();
- }
- stdNum++;
- }
- brStream.close();
- }
- for (int i = 0; i < numCourse;i++){
- sch.coursesAvailable.get(i).sortWaitList();
- }
- return numAdmin;
- }
- /**
- * Get the number of courses in school.
- * @return number of courses in school
- */
- public int getNumCourse() {
- return totalNumOfCourses;
- }
- /**
- * Get the array list of courses available in school.
- * @return the array list of courses available
- */
- public ArrayList<Course> getCourses() {
- return this.coursesAvailable;
- }
- /**
- * Get the size of the courses object array list in school.
- * @return size of the array list
- */
- public int getCoursesAvailableSize() {
- return coursesAvailable.size();
- }
- /**
- * Get the number of courses in school.
- * @return number of courses in school.
- */
- public int getTotalNumOfCourses(){
- return totalNumOfCourses;
- }
- /**
- * Print all course objects' index number and course code currently in the school object one by one.
- * @param sch School object containing all the course objects to be printed
- */
- public static void printCoursesAvailable (School sch) { //*
- int i;
- System.out.println("\nCourses available for students at the moment:");
- for (i = 0; i < sch.coursesAvailable.size(); i++) {
- System.out.println(i+1 + ". " + sch.coursesAvailable.get(i).getIndexNumber() + " " + sch.coursesAvailable.get(i).getCourseCode());
- }
- System.out.println();
- }
- /**
- * Print all the Courses available in the database in order, to aid the student in adding his course
- * @param sch School object containing all the course objects to be printed
- */
- public static void printSortedCourses(School sch){
- int i, j, z = 0, printedSize = 0;
- boolean print = true;
- String[] printed = new String[100];
- for (i=0;i<sch.coursesAvailable.size();i++){
- printedSize++;
- for (j=0;j<printedSize;j++){
- if (sch.coursesAvailable.get(i).getCourseCode() .equals (printed[j])){
- print = false;
- break;
- }
- print = true;
- }
- if (print == true){
- printed[printedSize-1] = sch.coursesAvailable.get(i).getCourseCode();
- System.out.println("");
- System.out.println("Course Code:");
- System.out.println(sch.coursesAvailable.get(i).getCourseCode());
- System.out.println("Available course index are:");
- System.out.println(sch.coursesAvailable.get(i).getIndexNumber());
- for (z = (i+1);z<sch.coursesAvailable.size();z++){
- if (sch.coursesAvailable.get(z).getCourseCode() . equals (sch.coursesAvailable.get(i).getCourseCode())){
- System.out.println(sch.coursesAvailable.get(z).getIndexNumber());
- }
- }
- }
- }
- }
- /**
- * Find course in school by course index number (FEP1, FEP2) and return true if found or false if not found.
- * @param sch School object containing the course
- * @param indexNumber Index number to identify the course needed
- * @return true if course is found or false if course is not found
- */
- public boolean findCourse(School sch, String indexNumber) {
- for (Course course : sch.coursesAvailable) {
- if (course.getIndexNumber().equals(indexNumber)) {
- return true;
- }
- }
- return false;
- }
- /**
- * Get course in school using the course's index number (FEP1, FEP2).
- * @param indexNumber Index number of the course you want to get
- * @return course object of the index number or null if no such course object of the index number exists
- */
- public Course getCourse(String indexNumber) {
- for (Course course : this.coursesAvailable) {
- if (course.getIndexNumber().equals(indexNumber)) {
- return course;
- }
- }
- return null;
- }
- /**
- * Get index of course object in the course object array list in school object.
- * @param course Course object you want to find the index of in the array list
- * @return the index of the course in the course array list
- */
- public int indexOfCourseObject(Course course) {
- return this.coursesAvailable.indexOf(course);
- }
- public Course getCourseViaIndexOf(int i) { //NO NEED THIS FUNCTION
- return this.coursesAvailable.get(i);
- }
- /**
- * Print a course object's details (Course code, Course index number, School of course and Course vacancy)
- * by parsing in the desired course object.
- * @param course The course that will have its details printed
- */
- public void printCourse(Course course) {
- System.out.println("Course code: " + course.getCourseCode());
- System.out.println("Course index number: " + course.getIndexNumber());
- System.out.println("School of course: " + course.getSchool());
- System.out.println("Course vacancy: " + course.getVacancy());
- System.out.println(" "); //print lessons information
- }
- /**
- * Add a course object into the course object array list of the school object.
- * @param courseCode Course code attribute of the course object
- * @param indexNumber Index number attribute of the course object
- * @param school School attribute of the course object
- * @param vacancy Vacancy attribute of the course object
- * @param AU AU attribute of the course object
- * @param sch School to assess the course object array list to add the new course into
- */
- public void addCourse(String courseCode, String indexNumber, String school, int vacancy, int AU, School sch) {
- System.out.println("Adding course...");
- sch.coursesAvailable.add(new Course(courseCode, indexNumber, school, vacancy, AU));
- System.out.println("Course added!");
- }
- /**
- * Print a course's current enrollment against the entire possible class size for that course.
- * @param course Course object to print its enrollment against the course object's class size
- */
- public void checkCourseVacancy(Course course) {
- int enrolment = course.getVacancy() - course.getCurrentEnrollment();
- System.out.println("Vacancy of course: " + enrolment + "/" + course.getVacancy());
- }
- /**
- * Get the size of the student object array list in school.
- * @return the size of the array list
- */
- public int getStudentBodySize() {
- return studentBody.size();
- }
- /**
- * Get a particular student's matriculation number based on the index of student object of the student in the student object array list.
- * @param i Index number of student object of the student
- * @return the matriculation number of the student
- */
- public String getStudentBodyMatricNo(int i) {
- return this.studentBody.get(i).getMatricNo();
- }
- /**
- * Print the entire student object array list in the school object, displaying each student object's name and matriculation number.
- *
- */
- public void printStudentBody() { //*
- System.out.println("\nStudents currently enrolled in NTU:");
- for (i = 0; i < this.studentBody.size(); i++) {
- System.out.println(i+1 + ". " + this.studentBody.get(i).getName() + ", " + this.studentBody.get(i).getMatricNo());
- }
- System.out.println();
- }
- /**
- * Add a new student object into the student object array list of the school object.
- * @param name Name attribute of the student object
- * @param matricNo Matriculation number attribute of the student object
- * @param gender Gender attribute of the student object
- * @param nationality Nationality attribute of the student object
- * @param programme Programme attribute of the student object
- * @param email Email attribute of the student object
- * @param handphoneNum Handphone number attribute of the student object
- * @param notificationOption Notification option attribute of the student object
- */
- public void addNewStudent(String name, String matricNo, String gender, String nationality, String programme, String email,String handphoneNum,String notificationOption) {
- this.studentBody.add(new Student(name, matricNo, gender, nationality, programme, email, handphoneNum, notificationOption));
- }
- public Student getStudentViaMatric(String MatricNo) { //NO NEED THIS FUNCTION
- for (Student student : this.studentBody) {
- if (student.getMatricNo().equals(MatricNo)) {
- return student;
- }
- }
- return null;
- }
- /**
- * Get index of student object in the student object array list in school object.
- * @param student Student object you want to find the index of in the array list
- * @return the index of the student in the student array list
- */
- public int indexOfStudentObject(Student student) {
- return this.studentBody.indexOf(student);
- }
- /**
- * Get student object in student object array list of the school object
- * @param indexNumber Index number of the student object in the student object array list
- * @return student object of the index number or null if no such student object of the index number exists
- */
- public Student getStudent(int i) {
- return this.studentBody.get(i);
- }
- /**
- * 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.
- * Not all courses with the old course code will be changed.
- * @param oldCourseCode Old course code that needs to be changed
- * @param newCourseCode New course code
- * @param indexNumber The index number of the the course that needs to have its course code changed
- */
- public void updateStudentCourseCode(String oldCourseCode, String newCourseCode, String indexNumber) {
- int i, j;
- int temp1;
- String[] temp2;
- String[] temp3;
- for (i = 0; i < studentBody.size(); i++) {
- temp1 = studentBody.get(i).getnoOfCourses();
- temp2 = studentBody.get(i).getStudentCourse();
- temp3 = studentBody.get(i).getStudentIndexNumber();
- for (j = 0; j < temp1; j++) {
- if (temp3[j].equals(indexNumber)) {
- if (temp2[j].equals(oldCourseCode)) {
- temp2[j] = newCourseCode;
- }
- }
- }
- }
- }
- /**
- * Updates a student's course's course index number to a new course index number.
- * @param oldIndex Old course index number that needs to be changed.
- * @param newIndex New course index
- */
- public void updateStudentIndexNumber(String oldIndex, String newIndex) {
- int i, j;
- int temp1;
- String[] temp2;
- for (i = 0; i < studentBody.size(); i++) {
- temp1 = studentBody.get(i).getnoOfCourses();
- temp2 = studentBody.get(i).getStudentIndexNumber();
- for (j = 0; j < temp1; j++) {
- if (temp2[j].equals(oldIndex)) {
- temp2[j] = newIndex;
- }
- }
- }
- }
- /**
- * Print the student name and matriculation number of the students in a particular course index.
- * @param strTemp Course index number
- */
- public void sortStudentBodyAccordingIndex(String strTemp) {
- int i,j,k;
- String strCourse = new String();
- boolean foundCourse = false;
- strCourse = strTemp;
- for(i=0;i<coursesAvailable.size();i++){
- if(coursesAvailable.get(i).getIndexNumber().equals(strCourse.toUpperCase()) == true){
- for(j=0;j<studentBody.size();j++){
- for(k=0;k<studentBody.get(j).numofRegCourse();k++){
- if(coursesAvailable.get(i).getIndexNumber().equals(studentBody.get(j).studentIndexNumber[k])){
- System.out.println(studentBody.get(j).getName()+" "+studentBody.get(j).getMatricNo());
- }
- }
- }
- foundCourse = true;
- System.out.print("\n");
- break;
- }
- }
- if(foundCourse == false){
- System.out.print("\n");
- System.out.println("Index not found\n");
- }
- }
- /**
- * Print the student name and matriculation number of the students of a particular course code.
- * @param inputText Course code
- */
- public void sortStudentBodyAccordingCourse(String inputText) {
- int i,j,k;
- String strCourse = inputText;
- boolean foundCourse = false;
- for(i=0;i<coursesAvailable.size();i++){
- if(coursesAvailable.get(i).getCourseCode().equals(strCourse.toUpperCase()) == true){
- for(j=0;j<studentBody.size();j++){
- for(k=0;k<studentBody.get(j).numofRegCourse();k++){
- if(coursesAvailable.get(i).getCourseCode().equals(studentBody.get(j).studentCourse[k])){
- System.out.println(studentBody.get(j).getName()+" "+studentBody.get(j).getMatricNo());
- }
- }
- }
- foundCourse = true;
- System.out.print("\n");
- break;
- }
- }
- if(foundCourse == false){
- System.out.print("\n");
- System.out.println("Course not found\n");
- }
- }
- /**
- * Find user in school by userid and return true if found or false if not found.
- * @param sch School object containing the user
- * @param userName Userid of user
- * @return
- */
- public boolean findUserId(School sch, String userName) {
- for (User user : sch.userList) {
- if (user.getUser().equals(userName)) {
- return true;
- }
- }
- return false;
- }
- /**
- * Encrypts a password string.
- * @param pw Password
- * @return encrypted string of the password
- * @throws NoSuchAlgorithmException Throws no such algorithm exception
- * @throws NoSuchPaddingException Throws no such padding exception
- * @throws InvalidKeyException Throws invalid key exception exception
- * @throws IllegalBlockSizeException Throws illegal block size exception
- * @throws BadPaddingException Throws bad padding exception
- */
- protected static String encrypt(String pw) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException{
- Cipher AesCipher = Cipher.getInstance("AES");
- byte[] convertPass = pw.getBytes();
- String encryptedKey = new String();
- String key1 = "1234567891012345"; //create manual key
- SecretKeySpec secret = new SecretKeySpec(key1.getBytes(), "AES");
- AesCipher.init(Cipher.ENCRYPT_MODE, secret);
- byte[] byteCipherText = AesCipher.doFinal(convertPass);
- for(byte b : byteCipherText){
- encryptedKey = encryptedKey + String.format("%02X", b);
- }
- return encryptedKey;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement