Fakultas Ilmu Komputer UI

Commit 71ce3093 authored by Mohammad Saddam Mashuri's avatar Mohammad Saddam Mashuri
Browse files

Merge branch 'saddam-gradle' into 'staging'

Refactor Calendar Module

See merge request !9
parents a4c25774 00657f08
Pipeline #82892 passed with stage
in 1 minute and 41 seconds
package softeng.g4;
import jdk.swing.interop.SwingInterOpUtils;
import softeng.g4.appointment.Appointment;
import softeng.g4.cli.AdminCLI;
import softeng.g4.cli.ListIO;
import softeng.g4.cli.StudentCLI;
......@@ -10,45 +8,10 @@ import softeng.g4.course.Course;
import softeng.g4.course.CourseList;
import softeng.g4.user.*;
import java.io.*;
import java.util.*;
public class Main {
/**
* TODOS for something general
* TODO: Needs refactoring and exception handling!!!! (especially the latter one)
* TODO: Create a class / method(s) for handling storage. Maybe something like ListIO?
* TODO: Create classes for different user types (TACLI, STUDENTCLI, ADMINCLI), and use them in the Main class
* It should be possible to make login of each user types to different classes,
* rather than using switch cases like a madman.
*/
/**
* TODOs for "LOG IN" FEATURE
* IT's DONE. You can login by using their username.
* Ex: S akuStudent
* Ex: T akuTA
* Ex: A akuAdmin
*
* If you want to register a new user,
* you'll have to run a constructor of Student / TeachingAssistant / Admin
* Ex: New Student("exampleUsername", "Example Fullname", Long.valueOf("exampleNPM"), userList);
*/
/**
* TODOs for "APPOINTMENT" FEATURE
* TODO: Implement the methods and attributes in the Appointment.java
* TODO: Implement the methods and attributes in the RequestAppointment.java
* TODO: Edit Main.java's intro methods, switch cases, Student.java, TeachingAssistant.java, Admin.java,
* Appointment.java, RequestAppointment.java, and maybe more, such that:
* - Student can add appointments
* - TeachingAssistant can accept / reject appointment by changing the appointment status
* - Both Student and TeachingAssistant can see the appointment details
* - Admin can ask permission to see the appointment details, which then they're able to see the appointment
* TODO: After all this TODOs are done, change Main.java such that it doesn't use the hardcoded version again
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String q = "";
......@@ -56,44 +19,8 @@ public class Main {
CourseList courseList;
UserList userList;
// // --------Uncomment all of this for the "hardcoded" version.
// // Uncomment to add a new fresh userList and courseList
// courseList = new CourseList();
// userList = new UserList();
//
// // Uncomment here to add a new TA or Admin
// // This will add false users, for checking users in the arraylist. (NECESSARY)
// new Student("falseStudent", "False Student", Long.valueOf("123456789"), userList);
// new TeachingAssistant("falseTA", "False TA", Long.valueOf("123456789"), userList);
// new Admin("falseAdmin", "False Admin", userList);
//
// // Add testStudent
// new Student("akuStudent", "Aku Student", Long.valueOf("123456789"), userList);
//
// // Add testTa with a mock calendar and schedule
// 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");
// new TeachingAssistant("anotherTA", "Another TA", Long.valueOf("1"), userList);
//
// // Add testAdmin
// new Admin("akuAdmin", "Aku Admin", userList);
//
// // Add false course
// new Course("False Course", "FALSECOURSE", "False Professor", 0, courseList);
//
// // Add mock courses
// new Course("Mata Kuliah", "MATKUL420", "Aku Dosen", 4, courseList);
// new Course("Software Engineering", "SOFTENG2021", "Aku Professor", 3, courseList);
// new Course("Fisika", "PHYS101", "Aku Dosen Fisika", 4, courseList);
//
// // Save the course
// ListIO.saveList(courseList, userList);
//
// // ---------------- END OF "HARDCODED" VERSION
// --------Uncomment this for the "hardcoded" version.
// reset();
while (true) {
......@@ -111,7 +38,6 @@ public class Main {
break;
}
// TODO: Maybe it is better to move this whole if block into a different class
String[] splited = {" ", " "};
splited = q.split("\\s+"); // Split by space
String searchUsername;
......@@ -224,4 +150,42 @@ public class Main {
System.out.println();
}
// Function to reset the list to its factory new state
public static void reset() {
CourseList courseList = new CourseList();
UserList userList = new UserList();
// Uncomment here to add a new TA or Admin
// This will add false users, for checking users in the arraylist. (NECESSARY)
new Student("falseStudent", "False Student", Long.valueOf("123456789"), userList);
new TeachingAssistant("falseTA", "False TA", Long.valueOf("123456789"), userList);
new Admin("falseAdmin", "False Admin", userList);
// Add testStudent
new Student("akuStudent", "Aku Student", Long.valueOf("123456789"), userList);
// Add testTa with a mock calendar and schedule
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");
new TeachingAssistant("anotherTA", "Another TA", Long.valueOf("1"), userList);
// Add testAdmin
new Admin("akuAdmin", "Aku Admin", userList);
// Add false course
new Course("False Course", "FALSECOURSE", "False Professor", 0, courseList);
// Add mock courses
new Course("Mata Kuliah", "MATKUL420", "Aku Dosen", 4, courseList);
new Course("Software Engineering", "SOFTENG2021", "Aku Professor", 3, courseList);
new Course("Fisika", "PHYS101", "Aku Dosen Fisika", 4, courseList);
// Save the course
ListIO.saveList(courseList, userList);
}
}
......@@ -27,7 +27,7 @@ public class Appointment implements Serializable {
this.status = "Pending";
}
public String generateId(){
public String generateId() {
int leftLimit = 65; // letter 'A'
int rightLimit = 90; // letter 'Z'
int targetStringLength = 5;
......@@ -43,11 +43,12 @@ public class Appointment implements Serializable {
}
public String toString() {
return "ID: " + id + "\nDate: " + date + "\nStudent: " + student.getUsername() + "\nTA: " + ta.getUsername() + "\nStart Time : " + startTime + "\nEnd Time: " + endTime + "\nDescription: " + description + "\nStatus: " + status;
//return date + student + " " + ta + " " + startTime + " - " + endTime + " " + description + status;
return "ID: " + id + "\nDate: " + date + "\nStudent: " + student.getUsername() +
"\nTA: " + ta.getUsername() + "\nStart Time : " + startTime + "\nEnd Time: " + endTime +
"\nDescription: " + description + "\nStatus: " + status;
}
public String getId (){
public String getId() {
return id;
}
......@@ -59,8 +60,8 @@ public class Appointment implements Serializable {
return status;
}
public void setStatus (String newstatus){
this.status = newstatus;
public void setStatus(String newStatus) {
this.status = newStatus;
}
}
package softeng.g4.appointment;
public class Reminder {
}
package softeng.g4.appointment;
public class RequestAppointment {
}
package softeng.g4.calendar;
import softeng.g4.user.TeachingAssistant;
import softeng.g4.user.UserList;
import java.io.Serializable;
public class SeeCalendarCheckPermission implements SeeCalendarStrategy, Serializable {
@Override
public void seeCalendar(String username, UserList userList) {
TeachingAssistant searchedTA = userList.searchTA(username);
if (searchedTA.getCalendar().isAdminPermission()) {
searchedTA.getCalendar().showCalendar();
} else {
System.out.println("You don't have permission to see " + username + "'s calendar!");
}
}
}
package softeng.g4.calendar;
import softeng.g4.user.UserList;
public interface SeeCalendarStrategy {
void seeCalendar(String username, UserList userList);
}
package softeng.g4.calendar;
import softeng.g4.user.TeachingAssistant;
import softeng.g4.user.UserList;
import java.io.Serializable;
public class SeeCalendarWithoutPermission implements SeeCalendarStrategy, Serializable {
@Override
public void seeCalendar(String username, UserList userList) {
TeachingAssistant ta = userList.searchTA(username);
ta.getCalendar().showCalendar();
}
}
......@@ -3,7 +3,6 @@ package softeng.g4.cli;
import softeng.g4.appointment.Appointment;
import softeng.g4.course.CourseList;
import softeng.g4.user.Student;
import softeng.g4.user.TeachingAssistant;
import softeng.g4.user.UserList;
import java.util.Scanner;
......@@ -39,12 +38,12 @@ public class StudentCLI extends CLI {
String endTime = sc.nextLine();
System.out.println("(Optional) Add description for your appointment: ");
String description = sc.nextLine();
currentStudent.setAppointment(day, ta, startTime, endTime, description, userList);
currentStudent.addAppointment(day, ta, startTime, endTime, description, userList);
System.out.println("Successfully added a new appointment!");
break;
case "APPOINTMENTS":
for (Appointment appointment : currentStudent.appointmentList) {
for (Appointment appointment : currentStudent.getAppointmentList()) {
System.out.println(appointment);
System.out.println();
}
......
......@@ -32,7 +32,7 @@ public class TACLI extends CLI {
break;
case "CALENDAR":
currentTA.seeCalendar();
currentTA.seeCalendar(currentTA.getUsername(), userList);
break;
case "COURSELIST":
......@@ -40,7 +40,6 @@ public class TACLI extends CLI {
break;
case "ADD SCHEDULE":
//TODO: Move this to a different function
System.out.println("Choose day (Monday, Tuesday, etc.): ");
String day = sc.nextLine().toLowerCase();
System.out.println("Schedule name: ");
......@@ -53,7 +52,7 @@ public class TACLI extends CLI {
System.out.println("Sucessfully added a new schedule!");
case "APPOINTMENTS":
for (Appointment appointment : currentTA.appointmentList) {
for (Appointment appointment : currentTA.getAppointmentList()) {
System.out.println(appointment);
System.out.println();
}
......
......@@ -9,12 +9,10 @@ import java.util.ArrayList;
public class Course implements Serializable {
private String name;
private String courseId;
private ArrayList<Student> students = new ArrayList<Student>();
private ArrayList<TeachingAssistant> teachingAssistants = new ArrayList<TeachingAssistant>();
private String professor;
private int sks;
private int coordinatorRoleIndex;
private boolean adminPermission;
// Constructor
public Course(String name, String courseId, String professor, int sks, CourseList courseList) {
......@@ -22,7 +20,6 @@ public class Course implements Serializable {
this.courseId = courseId;
this.professor = professor;
this.sks = sks;
this.adminPermission = false;
courseList.courseList.add(this);
}
......
package softeng.g4.user;
import softeng.g4.appointment.Appointment;
import softeng.g4.calendar.SeeCalendarCheckPermission;
import softeng.g4.course.Course;
import softeng.g4.course.CourseList;
public class Admin extends User {
private String username;
private String fullname;
private String role;
public Admin(String username, String fullname, UserList userList) {
this.username = username;
this.fullname = fullname;
this.role = "A";
this.setUsername(username);
this.setFullname(fullname);
this.setRole("A");
this.setSeeCalendarStrategy(new SeeCalendarCheckPermission());
// Register new Admin to the userList
userList.userList.add(this);
......@@ -42,41 +40,16 @@ public class Admin extends User {
System.out.println();
// Uncomment to include the printing of the false users
// DEBUG: include the printing of the false users
// for (User user: userList.userList) {
// System.out.println(user);
// }
}
// TODO: Iteration 3 - Admin can see the appointment, if appointment has the permission true
public void seeAppointment(Appointment ap) {
}
// Ask permission to the Teaching Assistant
public void askPermission(String username, UserList userList) {
TeachingAssistant searchedTA = userList.searchTA(username);
searchedTA.setAskPermissionAdmin(this);
System.out.println("You have successfully asked " + searchedTA.getUsername() + " to see their calendar!");
}
// TODO: Iteration 3 - Admin can see the TA's calendar, if calendar has the permission true
public void seeCalendar(String username, UserList userList) {
TeachingAssistant searchedTA = userList.searchTA(username);
if (searchedTA.getCalendar().isAdminPermission()) {
searchedTA.getCalendar().showCalendar();
} else {
System.out.println("You don't have permission to see " + username + "'s calendar!");
}
}
public String toString() {
return username + " - " + fullname;
}
public String getRole() {
return role;
}
public String getUsername() {
return username;
}
}
package softeng.g4.user;
import softeng.g4.calendar.Calendar;
import softeng.g4.calendar.Schedule;
import softeng.g4.appointment.Appointment;
import java.util.ArrayList;
import softeng.g4.calendar.SeeCalendarWithoutPermission;
public class Student extends UserStudent {
private String username;
private String fullname;
private String role;
private long npm;
public ArrayList<Appointment> appointmentList = new ArrayList<Appointment>();
// Constructor
public Student(String username, String fullname, long npm, UserList userList) {
this.username = username;
this.fullname = fullname;
this.role = "S";
this.npm = npm;
this.setUsername(username);
this.setFullname(fullname);
this.setRole("S");
this.setNPM(npm);
this.setSeeCalendarStrategy(new SeeCalendarWithoutPermission());
// Register new TA to the userList
userList.userList.add(this);
}
// TODO: Adds appointment with the corresponding TeachingAssistant
public void setAppointment(String date, String TAusername, String startTime, String endTime, String description, UserList userList) {
TeachingAssistant ta = userList.searchTA(TAusername);
if(ta.getUsername().equals("falseTA")){
// Adds appointment with the corresponding TeachingAssistant
public void addAppointment(String date, String TAUsername,
String startTime, String endTime, String description, UserList userList) {
TeachingAssistant ta = userList.searchTA(TAUsername);
if (ta.getUsername().equals("falseTA")) {
System.out.println("TA not found");
} else {
Appointment newAppointment = new Appointment(date, this, ta, startTime, endTime, description);
//this.calendar.addAppointment(day, newAppointment);
this.appointmentList.add(newAppointment);
ta.appointmentList.add(newAppointment);
this.getAppointmentList().add(newAppointment);
ta.getAppointmentList().add(newAppointment);
}
}
// See this Student's calendar
// temporary calendar
// TODO: Change this method such that it returns the TA's calendar by searching for their username
public void seeCalendar(String TAusername, UserList userList) {
TeachingAssistant ta = userList.searchTA(TAusername);
ta.seeCalendar();
}
public String getRole() {
return role;
}
public String getUsername() {
return username;
}
public String toString() {
return username + " - " + fullname;
}
}
package softeng.g4.user;
import softeng.g4.appointment.Appointment;
import softeng.g4.calendar.SeeCalendarWithoutPermission;
import softeng.g4.course.CourseList;
import softeng.g4.calendar.Calendar;
import softeng.g4.calendar.Schedule;
import java.util.ArrayList;
public class TeachingAssistant extends UserStudent {
private String username;
private String fullname;
private String role;
private long npm;
private Admin askPermissionAdmin = null;
private Calendar calendar = new Calendar();
public ArrayList<Appointment> appointmentList = new ArrayList<Appointment>();
// Constructor
public TeachingAssistant(String username, String fullname, long npm, UserList userList) {
this.username = username;
this.fullname = fullname;
this.role = "TA";
this.npm = npm;
this.setUsername(username);
this.setFullname(fullname);
this.setRole("TA");
this.setNPM(npm);
this.setSeeCalendarStrategy(new SeeCalendarWithoutPermission());
// Register new TA to the userList
userList.userList.add(this);
......@@ -38,22 +33,17 @@ public class TeachingAssistant extends UserStudent {
this.calendar.addSchedule(day, newSchedule);
}
// See this TA's calendar
public void seeCalendar() {
this.calendar.showCalendar();
}
// TODO: Implement these 2 methods such that they can change the status of the appointment to accepted or rejected
// Accept appointment
public void acceptAppointment(String id, UserList userList) {
boolean found = false;
for (Appointment appointment : appointmentList) {
for (Appointment appointment : this.getAppointmentList()) {
if (appointment.getId().equals(id)) {
found = true;
appointment.setStatus("Accepted");
Student student = userList.searchStudent(appointment.getStudent().getUsername());
for (Appointment studentappointment : student.appointmentList) {
if (studentappointment.getId().equals(id)) {
studentappointment.setStatus("Accepted");
for (Appointment studentAppointment : student.getAppointmentList()) {
if (studentAppointment.getId().equals(id)) {
studentAppointment.setStatus("Accepted");
}
}
}
......@@ -65,16 +55,17 @@ public class TeachingAssistant extends UserStudent {
}
}
// Reject appointment
public void rejectAppointment(String id, UserList userList) {
boolean found = false;
for (Appointment appointment : appointmentList) {
for (Appointment appointment : this.getAppointmentList()) {
if (appointment.getId().equals(id)) {
found = true;
appointment.setStatus("Rejected");
Student student = userList.searchStudent(appointment.getStudent().getUsername());
for (Appointment studentappointment : student.appointmentList) {
if (studentappointment.getId().equals(id)) {
studentappointment.setStatus("Rejected");
for (Appointment studentAppointment : student.getAppointmentList()) {
if (studentAppointment.getId().equals(id)) {
studentAppointment.setStatus("Rejected");
}
}
}
......@@ -87,6 +78,7 @@ public class TeachingAssistant extends UserStudent {
}
// Everytime this method is invoked, askPermissionAdmin will be updated to null
// Which means, TA will only be notified once after logging in.
public void getAskPermissionAdmin() {
if (this.askPermissionAdmin != null) {
System.out.println();
......@@ -106,18 +98,6 @@ public class TeachingAssistant extends UserStudent {
this.calendar.switchPermission();
}
public String toString() {
return username + " - " + fullname;
}
public String getRole() {
return role;
}
public String getUsername() {
return username;
}
public Calendar getCalendar() {
return calendar;
}
......
package softeng.g4.user;
import softeng.g4.calendar.SeeCalendarStrategy;
import java.io.Serializable;
public abstract class User implements Serializable {
private String fullname;
private String username;
private String role;
private SeeCalendarStrategy seeCalendarStrategy;
public void seeCalendar(String username, UserList userList) {
seeCalendarStrategy.seeCalendar(username, userList);
}
public String getRole() {
return role;
......@@ -13,4 +21,28 @@ public abstract class User implements Serializable {
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public void setRole(String role) {
this.role = role;
}
public String getFullname() {
return fullname;
}
public void setFullname(String fullname) {