Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.io.UnsupportedEncodingException;
- import java.util.*;
- public class exec{
- @SuppressWarnings("unchecked")
- public static void main(String[] args) throws IOException {
- ArrayList<Course> courses = new ArrayList<Course>();
- ArrayList<Student> students = new ArrayList<Student>();
- try {
- FileInputStream fis = new FileInputStream("courses.ser");
- ObjectInputStream ois = new ObjectInputStream(fis);
- courses = (ArrayList<Course>) ois.readObject();
- ois.close();
- fis.close(); //serialize
- }
- catch (IOException ioe)
- {
- ioe.printStackTrace();
- return;
- }
- catch (ClassNotFoundException c)
- {
- System.out.println("Class not found");
- c.printStackTrace();
- return;
- }
- try {
- FileInputStream fis = new FileInputStream("students.ser");
- ObjectInputStream ois = new ObjectInputStream(fis);
- students = (ArrayList<Student>) ois.readObject();
- ois.close();
- fis.close(); //serialize
- }
- catch (IOException ioe)
- {
- ioe.printStackTrace();
- return;
- }
- catch (ClassNotFoundException c)
- {
- System.out.println("Class not found");
- c.printStackTrace();
- return;
- }
- File ser = new File("courses.ser");
- if(!ser.exists()) {
- System.out.println("csv is currently not serialized, please run Serializer.java before running this");
- }
- Scanner input = new Scanner(System.in);
- System.out.print("Enter your username: ");
- String user = input.nextLine(); //prompt user and pass
- System.out.print("Enter your password: ");
- String pass = input.nextLine();
- for(Student stu: students) {
- if( user.equals(stu.username) && pass.equals(stu.password)) {
- System.out.println("Student login successfull\n");
- System.out.print("Enter a command: \n viewCourses, viewNotFull, register, withdraw, viewRegistered, or q to exit");
- String cmd = input.nextLine();
- while(!(cmd.equals("q"))) {
- switch (cmd) {
- case "viewCourses":
- stu.viewCourses();
- break;
- case "viewNotFull":
- stu.viewNotFull();
- break;
- case "register":
- System.out.print("Enter the name of the course, the id, and your full name"); //first + last w space between
- String name = input.nextLine();
- int sec = input.nextInt();
- String studentName = input.nextLine();
- stu.register(name, sec, studentName);
- break;
- case "withdraw":
- System.out.print("Provide your full name and the name of the course you wish to drop: ");
- studentName = input.nextLine();
- String courseName = input.nextLine();
- stu.withdraw(studentName, courseName);
- case "viewRegistered":
- stu.viewRegistered();
- break;
- case "q":
- break;
- default:
- System.out.print("Command not recognized");
- break;
- }
- System.out.print("Enter a command: \n viewCourses, viewNotFull, register, withdraw, viewRegistered, or q to exit");
- cmd = input.nextLine();
- }
- break;
- }
- }
- if(user.equals("Admin") && pass.equals("Admin001")) { //whether you're admin or not
- System.out.println("Administrator login successfull\n");
- Administrator a = new Administrator(courses, "admin", "admin", user, pass);
- System.out.print("Enter a command: \n createStudent, viewCourses, viewFull, writeFull, sort, createCourse, deleteCourse, registered\n courseInfo, or q to quit");
- String cmd = input.nextLine();
- while(!(cmd.equals("q"))) {
- switch (cmd) {
- case "createStudent":
- System.out.print("Create a student, enter first name, last name, username, and password: ");
- String f= input.nextLine();
- String l = input.nextLine();
- String u = input.nextLine();
- String p = input.nextLine();
- ArrayList<Course> n = null;
- Student stuu = new Student(n,f,l,u,p);
- students.add(stuu);
- break;
- case "viewCourses":
- a.viewCourses();
- break;
- case "viewFull":
- a.viewFull(); //implement all the methods for admin
- break;
- case "writeFull":
- try {
- a.writeFull();
- } catch (FileNotFoundException | UnsupportedEncodingException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- System.out.println("Error");
- }
- break;
- case "sort":
- a.sort();
- break;
- case "createCourse":
- String name;
- String id;
- int max;
- int curr;
- String ins;
- int sec;
- String loc;
- System.out.print("Enter the course name, ID, max students, current students, instructor name, section number, and location: ");
- name = input.nextLine();
- id = input.nextLine();
- max = Integer.parseInt(input.nextLine());
- curr = Integer.parseInt(input.nextLine());
- ins = input.nextLine();
- sec = Integer.parseInt(input.nextLine());
- loc = input.nextLine();
- a.createCourse(name, id, max, curr, null, ins, sec, loc);
- break;
- case "deleteCourse":
- System.out.print("Enter a course name: ");
- name = input.nextLine();
- System.out.print("Enter a course id: ");
- id = input.nextLine();
- a.deleteCourse(name, id);
- break;
- case "registered":
- System.out.print("Enter the first name of the student: ");
- f = input.nextLine();
- System.out.print("Enter the last name of the student: ");
- l = input.nextLine();
- a.registered(f, l);
- break;
- case "courseInfo":
- System.out.print("Enter the course ID of the class you wish to get info on: ");
- id = input.nextLine();
- a.courseInfo(id);
- break;
- case "q":
- System.out.println("Quitting...");
- break;
- default:
- System.out.println("Command not recognized");
- break;
- }
- System.out.print("Enter a command: ");
- cmd = input.nextLine();
- }
- }{
- FileOutputStream fileOutC = new FileOutputStream("courses.ser"); //start serializing courses
- ObjectOutputStream outC = new ObjectOutputStream(fileOutC);
- FileOutputStream fileOutS = new FileOutputStream("students.ser"); //start serializing students
- ObjectOutputStream outS = new ObjectOutputStream(fileOutS);
- try {
- outC.writeObject(courses);
- outS.writeObject(students);
- } catch (IOException i) {
- i.printStackTrace(); //catch error
- }
- System.out.printf("Serialized data is saved in courses.ser and students.ser");
- outC.close();
- fileOutC.close();
- fileOutS.close();
- outS.close();
- // bufferedReader.close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement