Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** Project 1 COP 3503
- *
- * @author shayan
- *
- */
- import java.io.*;
- import java.util.Scanner;
- public class University {
- // Properties
- private String name;
- private String currentTerm; // Fall/Spring/etc.
- private int year; // 2011...
- private Course[] courses;
- private Student[] students;
- private Instructor[] instructors;
- // Constructors
- public University(String name, String currentTerm, int year, Student[] students, Instructor[] instructors, Course[] courses) {
- this.name = name;
- this.currentTerm = currentTerm;
- this.year = year;
- this.students = students;
- this.courses = courses;
- this.instructors = instructors;
- }
- // Get/set
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getCurrentTerm() {
- return currentTerm;
- }
- public void setCurrentTerm(String currentTerm) {
- this.currentTerm = currentTerm;
- }
- public int getYear() {
- return year;
- }
- public void setYear(int year) {
- this.year = year;
- }
- public Course[] getCourses() {
- return courses;
- }
- public void setCourses(Course[] courses) {
- this.courses = courses;
- }
- public Student[] getStudents() {
- return students;
- }
- public void setStudents(Student[] students) {
- this.students = students;
- }
- public Instructor[] getInstructors() {
- return instructors;
- }
- public void setInstructors(Instructor[] instructors) {
- this.instructors = instructors;
- }
- // toString
- public String toString() {
- String str = "University Name: " + name;
- //str += "\nCurrent Semester: " + currentTerm + " " + year;
- str += "\nCurrent Term: " + currentTerm;
- str += "\nCurrent Year: " + year;
- str += "\nNumber of Students: " + students.length;
- str += "\nNumber of Instructors: " + instructors.length;
- str += "\nNumber of Courses: " + courses.length;
- return str;
- }
- public boolean writeToFile(String filename) {
- try {
- /*java.io. */ File file1 = new File(filename);
- /*java.io. */ PrintWriter output = new PrintWriter(file1);
- output.println(this.toString());
- //Ugrad Student
- for (int i=0; i < students.length; i++) {
- if (students[i] instanceof UndergradStudent) {
- output.println("Undergrad Student:");
- output.println("Name: " +students[i].getName());
- output.println("UFID: " +students[i].getUFID());
- output.println("D.O.B: " +students[i].getDob());
- output.println("GPA: " +students[i].getGpa());
- output.print("Number of Courses: " );
- int countCourse = 0;
- for(int m =0; m< students[i].getCourses().length; m++) {
- if(students[i].getCourses()[m]!=null) {
- countCourse++;
- }
- }
- output.println(countCourse);
- }
- }
- //Grad Student
- for (int i=0; i < students.length; i++) {
- if(students[i] instanceof GradStudent) {
- output.println("Grad Student:");
- output.println("Name: " +students[i].getName());
- output.println("UFID: " +students[i].getUFID());
- output.println("D.O.B: " +students[i].getDob());
- output.println("GPA: " +students[i].getGpa());
- output.print("Number of Courses: " );
- int countCourse = 0;
- for(int m =0; m< students[i].getCourses().length; m++) {
- if(students[i].getCourses()[m]!=null) {
- countCourse++;
- }
- }
- output.println(countCourse);
- }
- }
- //Instructor
- for (int i=0; i <instructors.length; i++) {
- String str = "Name: " + instructors[i].getName();
- str += "\nUFID: " + instructors[i].getUFID();
- str += "\nD.O.B: " + instructors[i].getDob();
- output.println("Instructor:");
- output.println(str);
- }
- //Courses
- for(int i=0; i < courses.length; i++) {
- output.println("Course:");
- output.println(courses[i].getType() + " " + courses[i].getNumber());
- output.println(courses[i].getTitle());
- output.println("Number of credits: " +courses[i].getNumCredits());
- output.println("Instructor UFID: " +courses[i].getInstructor().getUFID());
- output.println("Number of TAs: " +courses[i].getTAs().length);
- output.print("TA UFIDs: ");
- if (courses[i] != null) {
- for(int k =0; k<courses[i].getTAs().length; k++) {
- if (courses[i].getTAs()[k] != null) {
- if (k!=0) {
- output.print(",");
- }
- output.print(courses[i].getTAs()[k].getUFID());
- }
- }
- }
- output.println("\nCapacity: " + courses[i].getCapacity());
- output.println("Current Enrollment: " +courses[i].getCurrentEnrollment());
- output.print("Student UFIDs: ");
- if (courses[i] != null) {
- for(int j=0; j < courses[i].getStudents().length; j++) {
- if (courses[i].getStudents()[j] != null) {
- if (j!=0) {
- output.print(",");
- }
- output.print(courses[i].getStudents()[j].getUFID());
- }
- }
- }
- output.print("\n");
- }
- output.close();
- return true;
- }
- catch (FileNotFoundException ex) {
- return false;
- }
- }
- public static University readFromFile(String filename) {
- System.out.println("The program works! ...sort of...");
- try{
- File file1 = new File(filename); //the file to be read from
- Scanner fileIn = new Scanner(file1);
- if(!fileIn.hasNext()) {
- throw new EmptyFileException(filename);
- }
- //University info
- String lineIn = "";
- String[] universityName = fileIn.nextLine().split(": "); //the correct data will be read by spliting each line
- String[] universityTerm = fileIn.nextLine().split(": ");
- String[] universityYear = fileIn.nextLine().split(": ");
- String[] universityStudents = fileIn.nextLine().split(": ");
- Student[] studentsArray = new Student[Integer.parseInt(universityStudents[1])]; //Integer values parsed from the strings
- String[] universityInstructors = fileIn.nextLine().split(": ");
- Instructor[] instructorsArray = new Instructor[Integer.parseInt(universityInstructors[1])];
- String[] universityCourses = fileIn.nextLine().split(": ");
- Course[] coursesArray = new Course[Integer.parseInt(universityCourses[1])];
- //Reading in info
- lineIn = fileIn.nextLine();
- System.out.println(lineIn);
- //UndergradStudent
- String studentName = "";
- int studentUFID = 0;
- String studentDob = "";
- double studentGpa =0;
- String[] studentInfo= null;
- lineIn = fileIn.nextLine();
- int o =0;
- while (o < 4) { //loop reads in each of the 4 student's information
- studentInfo =lineIn.split(": ");
- studentName = studentInfo[1]; //Reading in student's name
- lineIn = fileIn.nextLine();
- studentInfo =lineIn.split(": ");
- studentUFID = Integer.parseInt(studentInfo[1]); //Reading in student's UFID
- lineIn = fileIn.nextLine();
- studentInfo =lineIn.split(": ");
- studentDob = studentInfo[1]; //Reading in student's DOB
- lineIn = fileIn.nextLine();
- studentInfo =lineIn.split(": ");
- studentGpa = Double.parseDouble(studentInfo[1]); //Reading in student's GPA
- lineIn = fileIn.nextLine();
- for(int i=0; i<studentsArray.length;i++) { //loop creates UndergradStudent object
- if(studentsArray[i] ==null) {
- studentsArray[i] = new UndergradStudent(studentName, studentUFID, studentDob, studentGpa);
- break;
- }
- }
- lineIn = fileIn.nextLine();
- if (o != 3) {
- lineIn=fileIn.nextLine();
- }
- o++;
- }
- //Grad Student
- studentName = "";
- studentUFID = 0;
- studentDob = "";
- studentGpa =0;
- int f = 0;
- while (f < 5) {
- lineIn = fileIn.nextLine();
- studentInfo =lineIn.split(": ");
- studentName = studentInfo[1]; //Reading in student's name
- lineIn = fileIn.nextLine();
- studentInfo =lineIn.split(": ");
- studentUFID = Integer.parseInt(studentInfo[1]); //Reading in student's UFID
- lineIn = fileIn.nextLine();
- studentInfo =lineIn.split(": ");
- studentDob = studentInfo[1]; //Reading in student's DOB
- lineIn = fileIn.nextLine();
- studentInfo =lineIn.split(": ");
- studentGpa = Double.parseDouble(studentInfo[1]); //Reading in student's GPA
- lineIn = fileIn.nextLine();
- lineIn = fileIn.nextLine();
- for(int i=0; i<studentsArray.length;i++) { //loop creates GradStudent object
- if(studentsArray[i] ==null) {
- studentsArray[i] = new GradStudent(studentName, studentUFID, studentDob, studentGpa);
- break;
- }
- }
- f++;
- }
- f =0;
- //Instructor
- while (f <3) {
- studentName = "";
- studentUFID = 0;
- studentDob = "";
- lineIn = fileIn.nextLine();
- studentInfo =lineIn.split(": ");
- studentName = studentInfo[1]; //Reading in instructor's name
- lineIn = fileIn.nextLine();
- studentInfo =lineIn.split(": ");
- studentUFID = Integer.parseInt(studentInfo[1]); //Reading in instructor's UFID
- lineIn = fileIn.nextLine();
- studentInfo =lineIn.split(": ");
- studentDob = studentInfo[1]; //Reading in instructors DOB
- lineIn = fileIn.nextLine();
- for(int i=0; i<instructorsArray.length;i++) { //loop creates instructor object
- if(instructorsArray[i] ==null) {
- instructorsArray[i] = new Instructor(studentName, studentUFID, studentDob);
- break;
- }
- }
- f++;
- }
- //Courses
- for(int k =0; k<coursesArray.length;k++) {
- if(lineIn.equals("Course:")) { //Checks to make sure current line is correct
- lineIn = fileIn.nextLine();
- String[] courseInfo = lineIn.split(" "); //Splits course string, e.g. "COP 3503" into "COP" and "3503"
- String courseType = courseInfo[0]; //Assigns course type
- int courseNum = Integer.parseInt(courseInfo[1]); //Assigns course number
- lineIn=fileIn.nextLine();
- String courseName = lineIn; //Reading in course name
- lineIn=fileIn.nextLine();
- String[] courseCredits = lineIn.split(": ");
- int numCredits = Integer.parseInt(courseCredits[1]); //Reading in credits for course
- lineIn=fileIn.nextLine();
- String[] instructUfid = lineIn.split(": ");
- int instructorUfid = Integer.parseInt(instructUfid[1]); //Reading in instructor's UFID
- Instructor instructorToCall = null; //When correct instructor is found based on UFID comparison, this will be the instructor passed into the course constructor
- for(int i =0; i<instructorsArray.length; i++) { //loop finds correct instructor based upon UFID of course instructor
- System.out.println("SubCheckpointB");
- if(instructorUfid == instructorsArray[i].getUFID()){
- instructorToCall = instructorsArray[i];
- }
- }
- lineIn=fileIn.nextLine();
- lineIn=fileIn.nextLine();
- String[] taUfidLine = lineIn.split(": "); //Splits "TA UFIDs: 118491,128191" into "TA UFIDS" and "118491,128191"
- String[] taUfid = taUfidLine[1].split(","); //Splits "118491,128191" into "118491" and "128191"
- GradStudent[] TAs = new GradStudent[taUfid.length]; //Creates an array for TAs based on number of TA UFIDs
- for (int i =0; i<taUfid.length;i++) {
- for(int j =0; j<studentsArray.length;j++) {
- if (Integer.parseInt(taUfid[i]) == studentsArray[j].getUFID()){ //Finds correct GradStudent based upon TA UFID
- TAs[i] = (GradStudent) studentsArray[j];
- }
- }
- }
- lineIn=fileIn.nextLine();
- String[] capLine = lineIn.split(": "); //Spliting "Capacity: 3" into "Capacity" and "3"
- int tempCapacity =Integer.parseInt(capLine[1]); //Reading in capacity of the course
- lineIn=fileIn.nextLine(); //Skipping a line
- lineIn=fileIn.nextLine();
- String[] studentUfidLine = lineIn.split(": "); //Splitting "Student UFIDs: 871322,871320,871312" into "Student UFIDs" and "871322,871320,871312"
- String[] studentUfid = studentUfidLine[1].split(","); //Splitting "871322,871320,871312" into "871322" "871320" "871312"
- UndergradStudent[] studentsTemp = new UndergradStudent[studentUfid.length]; //Creating array of UndergradStudents taking the course
- GradStudent[] gradTemp = new GradStudent[studentUfid.length]; //Creating array of GradStudents taking the course
- for (int i =0; i<studentUfid.length;i++) {
- for(int j =0; j<studentsArray.length;j++) {
- if (Integer.parseInt(studentUfid[i]) == studentsArray[j].getUFID() && k !=1){ //Excludes graduate level course at k=1
- studentsTemp[i] = (UndergradStudent) studentsArray[j];
- }
- if (Integer.parseInt(studentUfid[i]) == studentsArray[j].getUFID() && k ==1){ //Graduate level course is at k=1
- gradTemp[i] = (GradStudent) studentsArray[j];
- }
- }
- }
- coursesArray[k] = new Course(courseType, courseNum, courseName, numCredits,
- instructorToCall, TAs, tempCapacity, studentsTemp);
- for(int m =0; m<TAs.length; m++) {
- TAs[m].setCourseTA(coursesArray[k]);
- }
- if (k!=1) { //Excludes graduate course
- for (int m=0; m<studentsTemp.length; m++) {
- for (int x = 0; x <studentsArray.length; x++) {
- System.out.println("SubCheckpointH");
- if (Integer.parseInt(studentUfid[m])== studentsArray[x].getUFID() && k!=1) {
- studentsArray[x].addCourse(coursesArray[k]);
- }
- }
- }
- }
- //If Course k is a graduate course, this loop should add Student m to that course after that
- //student is found based on the UFIDs of the students taking the course
- if (k==1) {
- for (int m=0; m<gradTemp.length; m++) {
- System.out.println("SubCheckpointG");
- for (int x = 0; x <studentsArray.length; x++) {
- System.out.println("SubCheckpointH");
- System.out.println(studentsArray[x]);
- if (Integer.parseInt(studentUfid[m])== studentsArray[x].getUFID() && k==1) {
- (studentsArray[x]).addCourse(coursesArray[k]);
- }
- }
- }
- }
- }
- if (!(lineIn.equals("Course:"))&& fileIn.hasNext()) {
- lineIn=fileIn.nextLine();
- }
- }
- fileIn.close();
- return new University(universityName[1], universityTerm[1], Integer.parseInt(universityYear[1]), studentsArray, instructorsArray, coursesArray);
- }
- catch(EmptyFileException ex) {
- System.out.println(ex.toString());
- return null;
- }
- catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
- }
Add Comment
Please, Sign In to add comment