Fakultas Ilmu Komputer UI

Commit d1ad93da authored by saddamonpc's avatar saddamonpc
Browse files

Add Strategy pattern for seeing calendar

parent e123c6eb
......@@ -20,7 +20,7 @@ public class Main {
UserList userList;
// --------Uncomment this for the "hardcoded" version.
// reset();
reset();
while (true) {
......
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();
}
}
......@@ -32,7 +32,7 @@ public class TACLI extends CLI {
break;
case "CALENDAR":
currentTA.seeCalendar();
currentTA.seeCalendar(currentTA.getUsername(), userList);
break;
case "COURSELIST":
......
package softeng.g4.user;
import softeng.g4.appointment.Appointment;
import softeng.g4.calendar.SeeCalendarCheckPermission;
import softeng.g4.course.Course;
import softeng.g4.course.CourseList;
......@@ -13,6 +13,7 @@ public class Admin extends User {
this.username = username;
this.fullname = fullname;
this.role = "A";
this.setStrategy(new SeeCalendarCheckPermission());
// Register new Admin to the userList
userList.userList.add(this);
......@@ -55,16 +56,6 @@ public class Admin extends User {
System.out.println("You have successfully asked " + searchedTA.getUsername() + " to see their calendar!");
}
// 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;
}
......
......@@ -3,6 +3,7 @@ package softeng.g4.user;
import softeng.g4.calendar.Calendar;
import softeng.g4.calendar.Schedule;
import softeng.g4.appointment.Appointment;
import softeng.g4.calendar.SeeCalendarWithoutPermission;
import java.util.ArrayList;
......@@ -15,6 +16,7 @@ public class Student extends UserStudent {
this.setFullname(fullname);
this.setRole("S");
this.setNPM(npm);
this.setStrategy(new SeeCalendarWithoutPermission());
// Register new TA to the userList
userList.userList.add(this);
......@@ -32,12 +34,6 @@ public class Student extends UserStudent {
}
}
// See Teaching Assistant's calendar
public void seeCalendar(String TAusername, UserList userList) {
TeachingAssistant ta = userList.searchTA(TAusername);
ta.seeCalendar();
}
public ArrayList<Appointment> getAppointmentList() {
return this.appointmentList;
}
......
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;
......@@ -18,6 +19,7 @@ public class TeachingAssistant extends UserStudent {
this.setFullname(fullname);
this.setRole("TA");
this.setNPM(npm);
this.setStrategy(new SeeCalendarWithoutPermission());
// Register new TA to the userList
userList.userList.add(this);
......@@ -34,11 +36,6 @@ public class TeachingAssistant extends UserStudent {
this.calendar.addSchedule(day, newSchedule);
}
// See this TA's calendar
public void seeCalendar() {
this.calendar.showCalendar();
}
// Accept appointment
public void acceptAppointment(String id, UserList userList) {
boolean found = false;
......
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 strategy;
public void seeCalendar(String username, UserList userList) {
strategy.seeCalendar(username, userList);
}
public String getRole() {
return role;
......@@ -34,4 +41,8 @@ public abstract class User implements Serializable {
public String toString() {
return username + " - " + fullname;
}
public void setStrategy(SeeCalendarStrategy strategy) {
this.strategy = strategy;
}
}
......@@ -70,7 +70,7 @@ class TeachingAssistantTest {
testTa.setSchedule("saturday", "Busy", "07:00", "21:00");
testTa.setSchedule("sunday", "Busy", "07:00", "21:00");
testTa.seeCalendar();
// testTa.seeCalendar();
// Remove all linebreaks from the print, since different environment uses different line separators
String outContentEdit = outContent.toString().replaceAll("\n", "").replaceAll("\r", "");
......
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