Fakultas Ilmu Komputer UI

Commit df804047 authored by saddamonpc's avatar saddamonpc
Browse files

Add persistent storage for list of users, and Update TODOs

parent 5ce84844
......@@ -4,6 +4,8 @@ import softeng.g4.course.Course;
import softeng.g4.course.CourseList;
import softeng.g4.user.Admin;
import softeng.g4.user.TeachingAssistant;
import softeng.g4.user.User;
import softeng.g4.user.UserList;
import java.io.*;
import java.util.*;
......@@ -11,17 +13,26 @@ import java.util.*;
public class Main {
/**
* TODO: Needs refactoring, exception handling, and test cases
* TODOS for something general
* TODO: Needs refactoring and exception handling!!!! (especially the latter one)
* It should be possible to make login of each user types to different classes,
* rather than using switch cases like a madman.
*/
/**
* MORE IMPORTANT
* TODO: Add persistent storage for UserList and Calendar + Schedule (implement serializable on all of em?)
* TODO: Create a class / method(s) for ObjectIO / Handling storage?
* TODO: Create a function for every update operation (Like everytime we add / delete / edit a list)?
* TODO: Create an abstract class of List that implements Serializable, so that other classes can just inherit them?
* TODOs for "LOG IN" USE CASE
* TODO: Implement the methods in the Student class
* TODO: Create a class / method(s) for handling storage. Maybe something like ListIO?
* TODO: Use the saveList() function everytime the user is done doing a command in the "Main Menu"
* TODO: Create classes for different user types (TACLI, STUDENTCLI, ADMINCLI), and use them in the Main class
* TODO: Implement a method to search for a user and log in (must implement the previous TODO to do this)
*/
/**
* TODOs for "APPOINTMENT" USE CASE
* TODO: Implement the methods in the Appointment class
* TODO: Implement the methods in the RequestAppointment class
* TODO: Implement the methods in the Reminder class
*/
public static void main(String[] args) {
......@@ -29,26 +40,29 @@ public class Main {
String q = "";
String currentLogin = "";
// Open all persistent storage
CourseList courseList = openCourseList();
UserList userList = openUserList();
// Uncomment to do testing
TeachingAssistant testTa =
new TeachingAssistant("akuTA", "Aku Asdos", Long.valueOf("123456789"));
testTa.setSchedule("monday", "Free", "12:00", "17:00");
testTa.setSchedule("tuesday", "Free", "08:00", "11:00");
testTa.setSchedule("tuesday", "Free", "12:00", "15:00");
testTa.setSchedule("saturday", "Busy", "07:00", "21:00");
testTa.setSchedule("sunday", "Free", "12:00", "17:00");
Admin testAdmin = new Admin("akuAdmin", "Aku Admin");
// testAdmin.createCourse("Calculus II", "CALC120", "Aku Dosen", 3);
// testAdmin.createCourse("Statistika & Probabilita", "STAT69", "Aku Professor", 3);
// testAdmin.createCourse("Rekayasa Perangkat Lunak", "RPL9000", "Aku Dosen", 4);
// // Store courselist test
TeachingAssistant testTa = (TeachingAssistant) userList.userList.get(0);
Admin testAdmin = (Admin) userList.userList.get(1);
// Uncomment here to add a new TA or Admin
// TeachingAssistant testTa =
// new TeachingAssistant("akuTA", "Aku Asdos", Long.valueOf("123456789"), userList);
// testTa.setSchedule("monday", "Free", "12:00", "17:00");
// testTa.setSchedule("tuesday", "Free", "08:00", "11:00");
// testTa.setSchedule("tuesday", "Free", "12:00", "15:00");
// testTa.setSchedule("saturday", "Busy", "07:00", "21:00");
// testTa.setSchedule("sunday", "Free", "12:00", "17:00");
// Admin testAdmin = new Admin("akuAdmin", "Aku Admin", userList);
// // Store userList test
// try {
// FileOutputStream fos = new FileOutputStream("src/softeng/g4/xyz.txt");
// FileOutputStream fos = new FileOutputStream("src/softeng/g4/userlist.txt");
// ObjectOutputStream oos = new ObjectOutputStream(fos);
// oos.writeObject(courseList);
// oos.writeObject(userList);
//
// } catch (Exception ex) {
// System.out.println("Exception is caught");
......@@ -59,8 +73,11 @@ public class Main {
intro();
q = sc.nextLine();
// EXITS THE PROGRAM AND SERIALIZE ALL LIST, IF USER COMMAND IS "EXIT"
if (q.equals("EXIT")) {
System.out.println("Thank you for using our program! Have a good day!");
saveList(courseList, userList);
break;
}
......@@ -70,6 +87,7 @@ public class Main {
if (currentLogin.equals("T")) {
while (!q.equals("LOG OUT")) {
saveList(courseList, userList);
System.out.print("Input: ");
q = sc.nextLine();
switch (q) {
......@@ -95,8 +113,8 @@ public class Main {
String startTime = sc.nextLine();
System.out.println("Add end time 'XX:XX'. Example, '09:40': ");
String endTime = sc.nextLine();
testTa.setSchedule(day, name, startTime, endTime);
System.out.println("Sucessfully added a new schedule!");
case "LOG OUT":
break;
......@@ -108,6 +126,7 @@ public class Main {
}
} else if (currentLogin.equals("A")) {
while (!q.equals("LOG OUT")) {
saveList(courseList, userList);
System.out.print("Input: ");
q = sc.nextLine();
switch (q) {
......@@ -120,7 +139,7 @@ public class Main {
break;
case "USERLIST":
testAdmin.getUserList();
testAdmin.getUserList(userList);
break;
case "ADD COURSE":
......@@ -133,17 +152,7 @@ public class Main {
System.out.println("Choose SKS: ");
int sks = Integer.valueOf(sc.nextLine());
new Course(name, courseId, professor, sks, courseList);
// Store courselist test
try {
FileOutputStream fos = new FileOutputStream("src/softeng/g4/xyz.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(courseList);
} catch (Exception ex) {
System.out.println("Exception is caught");
}
System.out.println("Sucessfully added a new course!");
break;
case "ASSIGN TA":
......@@ -186,9 +195,6 @@ public class Main {
System.out.println("CALENDAR <TA's NPM> \t\t\t See TA's calendar.");
}
public static void studentQuery() {
}
public static void introTA() {
System.out.println("You are logged in as a teaching assistant.");
System.out.println("Type 'LOG OUT' to log out.");
......@@ -211,16 +217,12 @@ public class Main {
System.out.println("ASSIGN TA \t\t\t Assign TA to a course.");
}
public static void testBuild() {
}
// Open courselist.txt for persistent storage of list of courses
public static CourseList openCourseList() {
// Deserialize courseList test
try
{
// Reading the object from a file
FileInputStream file = new FileInputStream("src/softeng/g4/xyz.txt");
FileInputStream file = new FileInputStream("src/softeng/g4/courselist.txt");
ObjectInputStream in = new ObjectInputStream(file);
// Method for deserialization of object
......@@ -245,4 +247,57 @@ public class Main {
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/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/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/softeng/g4/userlist.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(userList);
} catch (Exception ex) {
System.out.println("Exception is caught");
}
}
}
package softeng.g4.calendar;
import java.io.Serializable;
import java.util.ArrayList;
public class Calendar {
public class Calendar implements Serializable {
private ArrayList<Schedule> monday = new ArrayList<Schedule>();
private ArrayList<Schedule> tuesday = new ArrayList<Schedule>();
private ArrayList<Schedule> wednesday = new ArrayList<Schedule>();
......
package softeng.g4.calendar;
public class Schedule {
import java.io.Serializable;
public class Schedule implements Serializable {
private String name;
private String startTime;
private String endTime;
......
......@@ -8,13 +8,13 @@ public class Admin extends User {
private String fullname;
private String role;
public Admin(String username, String fullname) {
public Admin(String username, String fullname, UserList userList) {
this.username = username;
this.fullname = fullname;
this.role = "A";
// Register new Admin to the userList
UserList.userList.add(this);
userList.userList.add(this);
}
public void assignRole() {}
......@@ -35,8 +35,8 @@ public class Admin extends User {
}
public void seeAppointment() {}
public void getUserList() {
for (User user: UserList.userList) {
public void getUserList(UserList userList) {
for (User user: userList.userList) {
System.out.println(user);
}
}
......
......@@ -12,14 +12,14 @@ public class TeachingAssistant extends UserStudent {
private Calendar calendar = new Calendar();
// Constructor
public TeachingAssistant(String username, String fullname, long npm) {
public TeachingAssistant(String username, String fullname, long npm, UserList userList) {
this.username = username;
this.fullname = fullname;
this.role = "TA";
this.npm = npm;
// Register new TA to the userList
UserList.userList.add(this);
userList.userList.add(this);
}
// See all courses
......
package softeng.g4.user;
public abstract class User {
import java.io.Serializable;
public abstract class User implements Serializable {
private String username;
private String fullname;
private String role;
......
package softeng.g4.user;
import softeng.g4.course.Course;
import java.io.Serializable;
import java.util.ArrayList;
public class UserList {
public static ArrayList<User> userList = new ArrayList<User>();
public class UserList implements Serializable {
public ArrayList<User> userList = new ArrayList<User>();
}
Markdown is supported
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