Guest User

Untitled

a guest
Jul 20th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.16 KB | None | 0 0
  1. /** Project 1 COP 3503
  2. *
  3. * @author shayan
  4. *
  5. */
  6. import java.io.*;
  7. import java.util.Scanner;
  8.  
  9. public class University {
  10. // Properties
  11. private String name;
  12. private String currentTerm; // Fall/Spring/etc.
  13. private int year; // 2011...
  14.  
  15. private Course[] courses;
  16. private Student[] students;
  17. private Instructor[] instructors;
  18.  
  19. // Constructors
  20. public University(String name, String currentTerm, int year, Student[] students, Instructor[] instructors, Course[] courses) {
  21. this.name = name;
  22. this.currentTerm = currentTerm;
  23. this.year = year;
  24.  
  25. this.students = students;
  26. this.courses = courses;
  27. this.instructors = instructors;
  28. }
  29.  
  30. // Get/set
  31. public String getName() {
  32. return name;
  33. }
  34.  
  35. public void setName(String name) {
  36. this.name = name;
  37. }
  38.  
  39. public String getCurrentTerm() {
  40. return currentTerm;
  41. }
  42.  
  43. public void setCurrentTerm(String currentTerm) {
  44. this.currentTerm = currentTerm;
  45. }
  46.  
  47. public int getYear() {
  48. return year;
  49. }
  50.  
  51. public void setYear(int year) {
  52. this.year = year;
  53. }
  54.  
  55. public Course[] getCourses() {
  56. return courses;
  57. }
  58.  
  59. public void setCourses(Course[] courses) {
  60. this.courses = courses;
  61. }
  62.  
  63. public Student[] getStudents() {
  64. return students;
  65. }
  66.  
  67. public void setStudents(Student[] students) {
  68. this.students = students;
  69. }
  70.  
  71. public Instructor[] getInstructors() {
  72. return instructors;
  73. }
  74.  
  75. public void setInstructors(Instructor[] instructors) {
  76. this.instructors = instructors;
  77. }
  78.  
  79. // toString
  80. public String toString() {
  81. String str = "University Name: " + name;
  82. //str += "\nCurrent Semester: " + currentTerm + " " + year;
  83. str += "\nCurrent Term: " + currentTerm;
  84. str += "\nCurrent Year: " + year;
  85. str += "\nNumber of Students: " + students.length;
  86. str += "\nNumber of Instructors: " + instructors.length;
  87. str += "\nNumber of Courses: " + courses.length;
  88. return str;
  89. }
  90.  
  91. public boolean writeToFile(String filename) {
  92. try {
  93. /*java.io. */ File file1 = new File(filename);
  94. /*java.io. */ PrintWriter output = new PrintWriter(file1);
  95. output.println(this.toString());
  96. //Ugrad Student
  97. for (int i=0; i < students.length; i++) {
  98. if (students[i] instanceof UndergradStudent) {
  99. output.println("Undergrad Student:");
  100. output.println("Name: " +students[i].getName());
  101. output.println("UFID: " +students[i].getUFID());
  102. output.println("D.O.B: " +students[i].getDob());
  103. output.println("GPA: " +students[i].getGpa());
  104. output.print("Number of Courses: " );
  105. int countCourse = 0;
  106. for(int m =0; m< students[i].getCourses().length; m++) {
  107. if(students[i].getCourses()[m]!=null) {
  108. countCourse++;
  109. }
  110. }
  111. output.println(countCourse);
  112.  
  113. }
  114. }
  115. //Grad Student
  116. for (int i=0; i < students.length; i++) {
  117. if(students[i] instanceof GradStudent) {
  118. output.println("Grad Student:");
  119. output.println("Name: " +students[i].getName());
  120. output.println("UFID: " +students[i].getUFID());
  121. output.println("D.O.B: " +students[i].getDob());
  122. output.println("GPA: " +students[i].getGpa());
  123. output.print("Number of Courses: " );
  124. int countCourse = 0;
  125. for(int m =0; m< students[i].getCourses().length; m++) {
  126. if(students[i].getCourses()[m]!=null) {
  127. countCourse++;
  128. }
  129. }
  130. output.println(countCourse);
  131. }
  132. }
  133. //Instructor
  134. for (int i=0; i <instructors.length; i++) {
  135. String str = "Name: " + instructors[i].getName();
  136. str += "\nUFID: " + instructors[i].getUFID();
  137. str += "\nD.O.B: " + instructors[i].getDob();
  138. output.println("Instructor:");
  139. output.println(str);
  140. }
  141. //Courses
  142. for(int i=0; i < courses.length; i++) {
  143. output.println("Course:");
  144. output.println(courses[i].getType() + " " + courses[i].getNumber());
  145. output.println(courses[i].getTitle());
  146. output.println("Number of credits: " +courses[i].getNumCredits());
  147. output.println("Instructor UFID: " +courses[i].getInstructor().getUFID());
  148. output.println("Number of TAs: " +courses[i].getTAs().length);
  149. output.print("TA UFIDs: ");
  150. if (courses[i] != null) {
  151. for(int k =0; k<courses[i].getTAs().length; k++) {
  152. if (courses[i].getTAs()[k] != null) {
  153. if (k!=0) {
  154. output.print(",");
  155. }
  156. output.print(courses[i].getTAs()[k].getUFID());
  157. }
  158. }
  159. }
  160. output.println("\nCapacity: " + courses[i].getCapacity());
  161. output.println("Current Enrollment: " +courses[i].getCurrentEnrollment());
  162. output.print("Student UFIDs: ");
  163. if (courses[i] != null) {
  164. for(int j=0; j < courses[i].getStudents().length; j++) {
  165. if (courses[i].getStudents()[j] != null) {
  166. if (j!=0) {
  167. output.print(",");
  168. }
  169. output.print(courses[i].getStudents()[j].getUFID());
  170.  
  171. }
  172. }
  173. }
  174. output.print("\n");
  175. }
  176. output.close();
  177. return true;
  178. }
  179. catch (FileNotFoundException ex) {
  180. return false;
  181. }
  182. }
  183.  
  184. public static University readFromFile(String filename) {
  185. System.out.println("The program works! ...sort of...");
  186.  
  187. try{
  188. File file1 = new File(filename); //the file to be read from
  189. Scanner fileIn = new Scanner(file1);
  190.  
  191. if(!fileIn.hasNext()) {
  192. throw new EmptyFileException(filename);
  193. }
  194. //University info
  195. String lineIn = "";
  196. String[] universityName = fileIn.nextLine().split(": "); //the correct data will be read by spliting each line
  197. String[] universityTerm = fileIn.nextLine().split(": ");
  198. String[] universityYear = fileIn.nextLine().split(": ");
  199. String[] universityStudents = fileIn.nextLine().split(": ");
  200. Student[] studentsArray = new Student[Integer.parseInt(universityStudents[1])]; //Integer values parsed from the strings
  201. String[] universityInstructors = fileIn.nextLine().split(": ");
  202. Instructor[] instructorsArray = new Instructor[Integer.parseInt(universityInstructors[1])];
  203. String[] universityCourses = fileIn.nextLine().split(": ");
  204. Course[] coursesArray = new Course[Integer.parseInt(universityCourses[1])];
  205.  
  206. //Reading in info
  207.  
  208.  
  209. lineIn = fileIn.nextLine();
  210. System.out.println(lineIn);
  211.  
  212. //UndergradStudent
  213.  
  214. String studentName = "";
  215. int studentUFID = 0;
  216. String studentDob = "";
  217. double studentGpa =0;
  218. String[] studentInfo= null;
  219. lineIn = fileIn.nextLine();
  220. int o =0;
  221. while (o < 4) { //loop reads in each of the 4 student's information
  222. studentInfo =lineIn.split(": ");
  223.  
  224. studentName = studentInfo[1]; //Reading in student's name
  225. lineIn = fileIn.nextLine();
  226.  
  227. studentInfo =lineIn.split(": ");
  228. studentUFID = Integer.parseInt(studentInfo[1]); //Reading in student's UFID
  229. lineIn = fileIn.nextLine();
  230.  
  231. studentInfo =lineIn.split(": ");
  232. studentDob = studentInfo[1]; //Reading in student's DOB
  233. lineIn = fileIn.nextLine();
  234.  
  235. studentInfo =lineIn.split(": ");
  236. studentGpa = Double.parseDouble(studentInfo[1]); //Reading in student's GPA
  237. lineIn = fileIn.nextLine();
  238.  
  239. for(int i=0; i<studentsArray.length;i++) { //loop creates UndergradStudent object
  240. if(studentsArray[i] ==null) {
  241.  
  242. studentsArray[i] = new UndergradStudent(studentName, studentUFID, studentDob, studentGpa);
  243. break;
  244. }
  245. }
  246. lineIn = fileIn.nextLine();
  247. if (o != 3) {
  248. lineIn=fileIn.nextLine();
  249. }
  250.  
  251. o++;
  252.  
  253. }
  254.  
  255. //Grad Student
  256.  
  257.  
  258. studentName = "";
  259. studentUFID = 0;
  260. studentDob = "";
  261. studentGpa =0;
  262.  
  263. int f = 0;
  264. while (f < 5) {
  265.  
  266. lineIn = fileIn.nextLine();
  267. studentInfo =lineIn.split(": ");
  268.  
  269. studentName = studentInfo[1]; //Reading in student's name
  270.  
  271. lineIn = fileIn.nextLine();
  272.  
  273. studentInfo =lineIn.split(": ");
  274. studentUFID = Integer.parseInt(studentInfo[1]); //Reading in student's UFID
  275. lineIn = fileIn.nextLine();
  276.  
  277. studentInfo =lineIn.split(": ");
  278. studentDob = studentInfo[1]; //Reading in student's DOB
  279. lineIn = fileIn.nextLine();
  280.  
  281. studentInfo =lineIn.split(": ");
  282. studentGpa = Double.parseDouble(studentInfo[1]); //Reading in student's GPA
  283. lineIn = fileIn.nextLine();
  284.  
  285. lineIn = fileIn.nextLine();
  286. for(int i=0; i<studentsArray.length;i++) { //loop creates GradStudent object
  287. if(studentsArray[i] ==null) {
  288. studentsArray[i] = new GradStudent(studentName, studentUFID, studentDob, studentGpa);
  289. break;
  290. }
  291. }
  292.  
  293. f++;
  294. }
  295.  
  296. f =0;
  297.  
  298. //Instructor
  299. while (f <3) {
  300.  
  301. studentName = "";
  302. studentUFID = 0;
  303. studentDob = "";
  304. lineIn = fileIn.nextLine();
  305.  
  306. studentInfo =lineIn.split(": ");
  307.  
  308. studentName = studentInfo[1]; //Reading in instructor's name
  309. lineIn = fileIn.nextLine();
  310.  
  311. studentInfo =lineIn.split(": ");
  312. studentUFID = Integer.parseInt(studentInfo[1]); //Reading in instructor's UFID
  313. lineIn = fileIn.nextLine();
  314.  
  315. studentInfo =lineIn.split(": ");
  316. studentDob = studentInfo[1]; //Reading in instructors DOB
  317. lineIn = fileIn.nextLine();
  318. for(int i=0; i<instructorsArray.length;i++) { //loop creates instructor object
  319. if(instructorsArray[i] ==null) {
  320. instructorsArray[i] = new Instructor(studentName, studentUFID, studentDob);
  321. break;
  322. }
  323. }
  324.  
  325. f++;
  326. }
  327.  
  328.  
  329. //Courses
  330.  
  331.  
  332. for(int k =0; k<coursesArray.length;k++) {
  333.  
  334. if(lineIn.equals("Course:")) { //Checks to make sure current line is correct
  335.  
  336. lineIn = fileIn.nextLine();
  337. String[] courseInfo = lineIn.split(" "); //Splits course string, e.g. "COP 3503" into "COP" and "3503"
  338. String courseType = courseInfo[0]; //Assigns course type
  339. int courseNum = Integer.parseInt(courseInfo[1]); //Assigns course number
  340. lineIn=fileIn.nextLine();
  341. String courseName = lineIn; //Reading in course name
  342. lineIn=fileIn.nextLine();
  343. String[] courseCredits = lineIn.split(": ");
  344. int numCredits = Integer.parseInt(courseCredits[1]); //Reading in credits for course
  345. lineIn=fileIn.nextLine();
  346. String[] instructUfid = lineIn.split(": ");
  347. int instructorUfid = Integer.parseInt(instructUfid[1]); //Reading in instructor's UFID
  348. Instructor instructorToCall = null; //When correct instructor is found based on UFID comparison, this will be the instructor passed into the course constructor
  349. for(int i =0; i<instructorsArray.length; i++) { //loop finds correct instructor based upon UFID of course instructor
  350. System.out.println("SubCheckpointB");
  351. if(instructorUfid == instructorsArray[i].getUFID()){
  352. instructorToCall = instructorsArray[i];
  353.  
  354. }
  355.  
  356. }
  357. lineIn=fileIn.nextLine();
  358.  
  359. lineIn=fileIn.nextLine();
  360. String[] taUfidLine = lineIn.split(": "); //Splits "TA UFIDs: 118491,128191" into "TA UFIDS" and "118491,128191"
  361. String[] taUfid = taUfidLine[1].split(","); //Splits "118491,128191" into "118491" and "128191"
  362. GradStudent[] TAs = new GradStudent[taUfid.length]; //Creates an array for TAs based on number of TA UFIDs
  363. for (int i =0; i<taUfid.length;i++) {
  364.  
  365. for(int j =0; j<studentsArray.length;j++) {
  366.  
  367. if (Integer.parseInt(taUfid[i]) == studentsArray[j].getUFID()){ //Finds correct GradStudent based upon TA UFID
  368. TAs[i] = (GradStudent) studentsArray[j];
  369.  
  370. }
  371. }
  372.  
  373. }
  374. lineIn=fileIn.nextLine();
  375. String[] capLine = lineIn.split(": "); //Spliting "Capacity: 3" into "Capacity" and "3"
  376. int tempCapacity =Integer.parseInt(capLine[1]); //Reading in capacity of the course
  377. lineIn=fileIn.nextLine(); //Skipping a line
  378. lineIn=fileIn.nextLine();
  379. String[] studentUfidLine = lineIn.split(": "); //Splitting "Student UFIDs: 871322,871320,871312" into "Student UFIDs" and "871322,871320,871312"
  380. String[] studentUfid = studentUfidLine[1].split(","); //Splitting "871322,871320,871312" into "871322" "871320" "871312"
  381. UndergradStudent[] studentsTemp = new UndergradStudent[studentUfid.length]; //Creating array of UndergradStudents taking the course
  382. GradStudent[] gradTemp = new GradStudent[studentUfid.length]; //Creating array of GradStudents taking the course
  383. for (int i =0; i<studentUfid.length;i++) {
  384.  
  385. for(int j =0; j<studentsArray.length;j++) {
  386.  
  387. if (Integer.parseInt(studentUfid[i]) == studentsArray[j].getUFID() && k !=1){ //Excludes graduate level course at k=1
  388. studentsTemp[i] = (UndergradStudent) studentsArray[j];
  389.  
  390. }
  391. if (Integer.parseInt(studentUfid[i]) == studentsArray[j].getUFID() && k ==1){ //Graduate level course is at k=1
  392. gradTemp[i] = (GradStudent) studentsArray[j];
  393.  
  394. }
  395. }
  396.  
  397. }
  398. coursesArray[k] = new Course(courseType, courseNum, courseName, numCredits,
  399. instructorToCall, TAs, tempCapacity, studentsTemp);
  400. for(int m =0; m<TAs.length; m++) {
  401.  
  402. TAs[m].setCourseTA(coursesArray[k]);
  403. }
  404. if (k!=1) { //Excludes graduate course
  405. for (int m=0; m<studentsTemp.length; m++) {
  406.  
  407. for (int x = 0; x <studentsArray.length; x++) {
  408. System.out.println("SubCheckpointH");
  409.  
  410. if (Integer.parseInt(studentUfid[m])== studentsArray[x].getUFID() && k!=1) {
  411. studentsArray[x].addCourse(coursesArray[k]);
  412.  
  413. }
  414.  
  415. }
  416.  
  417. }
  418. }
  419. //If Course k is a graduate course, this loop should add Student m to that course after that
  420. //student is found based on the UFIDs of the students taking the course
  421. if (k==1) {
  422. for (int m=0; m<gradTemp.length; m++) {
  423. System.out.println("SubCheckpointG");
  424. for (int x = 0; x <studentsArray.length; x++) {
  425. System.out.println("SubCheckpointH");
  426. System.out.println(studentsArray[x]);
  427.  
  428. if (Integer.parseInt(studentUfid[m])== studentsArray[x].getUFID() && k==1) {
  429. (studentsArray[x]).addCourse(coursesArray[k]);
  430.  
  431. }
  432. }
  433. }
  434. }
  435.  
  436. }
  437. if (!(lineIn.equals("Course:"))&& fileIn.hasNext()) {
  438. lineIn=fileIn.nextLine();
  439. }
  440.  
  441. }
  442.  
  443. fileIn.close();
  444.  
  445.  
  446. return new University(universityName[1], universityTerm[1], Integer.parseInt(universityYear[1]), studentsArray, instructorsArray, coursesArray);
  447. }
  448. catch(EmptyFileException ex) {
  449. System.out.println(ex.toString());
  450. return null;
  451. }
  452. catch (Exception e) {
  453. e.printStackTrace();
  454. return null;
  455. }
  456.  
  457. }
  458. }
Add Comment
Please, Sign In to add comment