Advertisement
Guest User

end me

a guest
Feb 25th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.06 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.io.FileReader;
  7. import java.io.IOException;
  8. import java.io.ObjectInputStream;
  9. import java.io.ObjectOutputStream;
  10. import java.io.UnsupportedEncodingException;
  11. import java.util.*;
  12.  
  13.  
  14. public class exec{
  15.     @SuppressWarnings("unchecked")
  16.     public static void main(String[] args) throws IOException {
  17.         ArrayList<Course> courses = new ArrayList<Course>();
  18.         ArrayList<Student> students = new ArrayList<Student>();
  19.  
  20.           try {
  21.                 FileInputStream fis = new FileInputStream("courses.ser");
  22.                 ObjectInputStream ois = new ObjectInputStream(fis);
  23.      
  24.                 courses =  (ArrayList<Course>) ois.readObject();
  25.      
  26.                 ois.close();
  27.                 fis.close(); //serialize
  28.             }
  29.             catch (IOException ioe)
  30.             {
  31.                 ioe.printStackTrace();
  32.                 return;
  33.             }
  34.             catch (ClassNotFoundException c)
  35.             {
  36.                 System.out.println("Class not found");
  37.                 c.printStackTrace();
  38.                 return;
  39.             }
  40.           try {
  41.                 FileInputStream fis = new FileInputStream("students.ser");
  42.                 ObjectInputStream ois = new ObjectInputStream(fis);
  43.      
  44.                 students =  (ArrayList<Student>) ois.readObject();
  45.      
  46.                 ois.close();
  47.                 fis.close(); //serialize
  48.             }
  49.             catch (IOException ioe)
  50.             {
  51.                 ioe.printStackTrace();
  52.                 return;
  53.             }
  54.             catch (ClassNotFoundException c)
  55.             {
  56.                 System.out.println("Class not found");
  57.                 c.printStackTrace();
  58.                 return;
  59.             }
  60.         File ser = new File("courses.ser");
  61.         if(!ser.exists()) {
  62.             System.out.println("csv is currently not serialized, please run Serializer.java before running this");
  63.         }
  64.         Scanner input = new Scanner(System.in);
  65.         System.out.print("Enter your username: ");
  66.         String user = input.nextLine(); //prompt user and pass
  67.         System.out.print("Enter your password: ");
  68.         String pass = input.nextLine();
  69.        
  70.         for(Student stu: students) {
  71.             if( user.equals(stu.username) && pass.equals(stu.password)) {
  72.                 System.out.println("Student login successfull\n");
  73.                 System.out.print("Enter a command: \n viewCourses, viewNotFull, register, withdraw, viewRegistered, or q to exit");
  74.                 String cmd = input.nextLine();
  75.                 while(!(cmd.equals("q"))) {
  76.                     switch (cmd) {
  77.                     case "viewCourses":
  78.                         stu.viewCourses();
  79.                         break;
  80.                     case "viewNotFull":
  81.                         stu.viewNotFull();
  82.                         break;
  83.                     case "register":
  84.                         System.out.print("Enter the name of the course, the id, and your full name"); //first + last w space between
  85.                         String name = input.nextLine();
  86.                         int sec = input.nextInt();
  87.                         String studentName = input.nextLine();
  88.                         stu.register(name, sec, studentName);
  89.                         break;
  90.                     case "withdraw":
  91.                         System.out.print("Provide your full name and the name of the course you wish to drop: ");
  92.                         studentName = input.nextLine();
  93.                         String courseName = input.nextLine();
  94.                         stu.withdraw(studentName, courseName);
  95.                     case "viewRegistered":
  96.                         stu.viewRegistered();
  97.                         break;
  98.                     case "q":
  99.                         break;
  100.                     default:
  101.                         System.out.print("Command not recognized");
  102.                         break;
  103.                        
  104.                     }
  105.                     System.out.print("Enter a command: \n viewCourses, viewNotFull, register, withdraw, viewRegistered, or q to exit");
  106.                     cmd = input.nextLine();
  107.                 }
  108.                 break;
  109.             }
  110.         }
  111.        
  112.         if(user.equals("Admin") && pass.equals("Admin001")) { //whether you're admin or not
  113.             System.out.println("Administrator login successfull\n");
  114.             Administrator a = new Administrator(courses, "admin", "admin", user, pass);
  115.             System.out.print("Enter a command: \n createStudent, viewCourses, viewFull, writeFull, sort, createCourse, deleteCourse, registered\n courseInfo, or q to quit");
  116.             String cmd = input.nextLine();
  117.             while(!(cmd.equals("q"))) {
  118.                 switch (cmd) {
  119.                 case "createStudent":
  120.                     System.out.print("Create a student, enter first name, last name, username, and password: ");
  121.                     String f= input.nextLine();
  122.                     String l = input.nextLine();
  123.                     String u = input.nextLine();
  124.                     String p = input.nextLine();
  125.                     ArrayList<Course> n = null;
  126.                     Student stuu = new Student(n,f,l,u,p);
  127.                     students.add(stuu);
  128.                     break;
  129.                    
  130.                 case "viewCourses":
  131.                     a.viewCourses();
  132.                     break;
  133.                 case "viewFull":
  134.                     a.viewFull(); //implement all the methods for admin
  135.                     break;
  136.                 case "writeFull":
  137.                     try {
  138.                         a.writeFull();
  139.                     } catch (FileNotFoundException | UnsupportedEncodingException e) {
  140.                         // TODO Auto-generated catch block
  141.                         e.printStackTrace();
  142.                         System.out.println("Error");
  143.                     }
  144.                     break;
  145.                 case "sort":
  146.                     a.sort();
  147.                     break;
  148.                 case "createCourse":
  149.                     String name;
  150.                     String id;
  151.                     int max;
  152.                     int curr;
  153.                     String ins;
  154.                     int sec;
  155.                     String loc;
  156.                     System.out.print("Enter the course name, ID, max students, current students, instructor name, section number, and location: ");
  157.                     name = input.nextLine();
  158.                     id = input.nextLine();
  159.                     max = Integer.parseInt(input.nextLine());
  160.                     curr = Integer.parseInt(input.nextLine());
  161.                     ins = input.nextLine();
  162.                     sec = Integer.parseInt(input.nextLine());
  163.                     loc = input.nextLine();
  164.                     a.createCourse(name, id, max, curr, null, ins, sec, loc);
  165.                     break;
  166.                 case "deleteCourse":
  167.                     System.out.print("Enter a course name: ");
  168.                     name = input.nextLine();
  169.                     System.out.print("Enter a course id: ");
  170.                     id = input.nextLine();
  171.                     a.deleteCourse(name, id);
  172.                     break;
  173.                 case "registered":
  174.                     System.out.print("Enter the first name of the student: ");
  175.                     f = input.nextLine();
  176.                     System.out.print("Enter the last name of the student: ");
  177.                     l = input.nextLine();
  178.                     a.registered(f, l);
  179.                     break;
  180.                 case "courseInfo":
  181.                     System.out.print("Enter the course ID of the class you wish to get info on: ");
  182.                     id = input.nextLine();
  183.                     a.courseInfo(id);
  184.                     break;
  185.                 case "q":
  186.                     System.out.println("Quitting...");
  187.                     break;
  188.                 default:
  189.                     System.out.println("Command not recognized");
  190.                     break;
  191.                 }
  192.                    
  193.                 System.out.print("Enter a command: ");
  194.                 cmd = input.nextLine();
  195.             }
  196.         }{
  197.         FileOutputStream fileOutC = new FileOutputStream("courses.ser"); //start serializing courses
  198.         ObjectOutputStream outC = new ObjectOutputStream(fileOutC);
  199.         FileOutputStream fileOutS = new FileOutputStream("students.ser"); //start serializing students
  200.         ObjectOutputStream outS = new ObjectOutputStream(fileOutS);
  201.              try {
  202.                  outC.writeObject(courses);
  203.                  outS.writeObject(students);
  204.               } catch (IOException i) {
  205.                   i.printStackTrace(); //catch error
  206.               }
  207.  
  208.              System.out.printf("Serialized data is saved in courses.ser and students.ser");
  209.              outC.close();
  210.              fileOutC.close();
  211.              fileOutS.close();
  212.              outS.close();
  213. //          bufferedReader.close();
  214.         }
  215.  
  216.     }
  217.  
  218.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement