Fakultas Ilmu Komputer UI

Commit 426926fe authored by saddamonpc's avatar saddamonpc
Browse files

Refactor Main.java such that it uses ListIO

parent b0977895
...@@ -2,6 +2,7 @@ package softeng.g4; ...@@ -2,6 +2,7 @@ package softeng.g4;
import jdk.swing.interop.SwingInterOpUtils; import jdk.swing.interop.SwingInterOpUtils;
import softeng.g4.appointment.Appointment; import softeng.g4.appointment.Appointment;
import softeng.g4.cli.ListIO;
import softeng.g4.course.Course; import softeng.g4.course.Course;
import softeng.g4.course.CourseList; import softeng.g4.course.CourseList;
import softeng.g4.user.*; import softeng.g4.user.*;
...@@ -87,24 +88,13 @@ public class Main { ...@@ -87,24 +88,13 @@ public class Main {
// new Course("Fisika", "PHYS101", "Aku Dosen Fisika", 4, courseList); // new Course("Fisika", "PHYS101", "Aku Dosen Fisika", 4, courseList);
// //
// // Save the course // // Save the course
// saveList(courseList, userList); // ListIO.saveList(courseList, userList);
// //
// // ---------------- END OF "HARDCODED" VERSION // // ---------------- END OF "HARDCODED" VERSION
// PRODUCTION BUILD: Open all persistent storage // PRODUCTION BUILD: Open all persistent storage
courseList = openCourseList(); courseList = ListIO.openCourseList();
userList = openUserList(); userList = ListIO.openUserList();
// // Store userList test
// try {
// FileOutputStream fos = new FileOutputStream("src/softeng/g4/userlist.txt");
// ObjectOutputStream oos = new ObjectOutputStream(fos);
// oos.writeObject(userList);
//
// } catch (Exception ex) {
// System.out.println("Exception is caught");
// }
while (true) { while (true) {
...@@ -114,7 +104,7 @@ public class Main { ...@@ -114,7 +104,7 @@ public class Main {
// EXITS THE PROGRAM AND SERIALIZE ALL LIST, IF USER COMMAND IS "EXIT" // EXITS THE PROGRAM AND SERIALIZE ALL LIST, IF USER COMMAND IS "EXIT"
if (q.equals("EXIT")) { if (q.equals("EXIT")) {
System.out.println("Thank you for using our program! Have a good day!"); System.out.println("Thank you for using our program! Have a good day!");
saveList(courseList, userList); ListIO.saveList(courseList, userList);
break; break;
} }
...@@ -144,7 +134,7 @@ public class Main { ...@@ -144,7 +134,7 @@ public class Main {
introStudent(currentStudent); // If we reach here, that means we found the user, and we are now logged in. introStudent(currentStudent); // If we reach here, that means we found the user, and we are now logged in.
while (!q.equals("LOG OUT")) { while (!q.equals("LOG OUT")) {
saveList(courseList, userList); // Saves the list everytime the user is in the "Main menu" ListIO.saveList(courseList, userList); // Saves the list everytime the user is in the "Main menu"
System.out.print("Input: "); System.out.print("Input: ");
q = sc.nextLine(); q = sc.nextLine();
switch (q) { switch (q) {
...@@ -200,7 +190,7 @@ public class Main { ...@@ -200,7 +190,7 @@ public class Main {
introTA(currentTA); // If we reach here, that means we found the user, and we are now logged in. introTA(currentTA); // If we reach here, that means we found the user, and we are now logged in.
while (!q.equals("LOG OUT")) { while (!q.equals("LOG OUT")) {
saveList(courseList, userList); // Saves the list everytime the user is in the "Main menu" ListIO.saveList(courseList, userList); // Saves the list everytime the user is in the "Main menu"
System.out.print("Input: "); System.out.print("Input: ");
q = sc.nextLine(); q = sc.nextLine();
switch (q) { switch (q) {
...@@ -264,7 +254,7 @@ public class Main { ...@@ -264,7 +254,7 @@ public class Main {
introAdmin(currentAdmin); // If we reach here, that means we found the user, and we are now logged in. introAdmin(currentAdmin); // If we reach here, that means we found the user, and we are now logged in.
while (!q.equals("LOG OUT")) { while (!q.equals("LOG OUT")) {
saveList(courseList, userList); // Saves the list everytime the user is in the "Main menu" ListIO.saveList(courseList, userList); // Saves the list everytime the user is in the "Main menu"
System.out.print("Input: "); System.out.print("Input: ");
q = sc.nextLine(); q = sc.nextLine();
switch (q) { switch (q) {
...@@ -375,73 +365,4 @@ public class Main { ...@@ -375,73 +365,4 @@ public class Main {
System.out.println(); System.out.println();
} }
// Open courselist.txt for persistent storage of list of courses
public static CourseList openCourseList() {
try {
// Reading the object from a file
FileInputStream file = new FileInputStream("src/main/java/softeng/g4/courselist.txt");
ObjectInputStream in = new ObjectInputStream(file);
// Method for deserialization of object
CourseList courseList = (CourseList) in.readObject();
in.close();
file.close();
return courseList;
} catch (IOException ex) {
System.out.println("IOException is caught");
} catch (ClassNotFoundException ex) {
System.out.println("ClassNotFoundException is caught");
}
return null;
}
// Open userlist.txt for persistent storage of list of users
public static UserList openUserList() {
try {
// Reading the object from a file
FileInputStream file = new FileInputStream("src/main/java/softeng/g4/userlist.txt");
ObjectInputStream in = new ObjectInputStream(file);
// Method for deserialization of object
UserList userList = (UserList) in.readObject();
in.close();
file.close();
return userList;
} catch (IOException ex) {
System.out.println("IOException is caught");
} catch (ClassNotFoundException ex) {
System.out.println("ClassNotFoundException is caught");
}
return null;
}
// Store all list
public static void saveList(CourseList courseList, UserList userList) {
// Store courseList
try {
FileOutputStream fos = new FileOutputStream("src/main/java/softeng/g4/courselist.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(courseList);
} catch (Exception ex) {
System.out.println("Exception is caught");
}
// Store userList
try {
FileOutputStream fos = new FileOutputStream("src/main/java/softeng/g4/userlist.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(userList);
} catch (Exception ex) {
System.out.println("Exception is caught");
}
}
} }
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment