Fakultas Ilmu Komputer UI

Commit feb6a27a authored by saddamonpc's avatar saddamonpc
Browse files

Add progress to log in

parent df804047
...@@ -2,10 +2,7 @@ package softeng.g4; ...@@ -2,10 +2,7 @@ package softeng.g4;
import softeng.g4.course.Course; import softeng.g4.course.Course;
import softeng.g4.course.CourseList; import softeng.g4.course.CourseList;
import softeng.g4.user.Admin; import softeng.g4.user.*;
import softeng.g4.user.TeachingAssistant;
import softeng.g4.user.User;
import softeng.g4.user.UserList;
import java.io.*; import java.io.*;
import java.util.*; import java.util.*;
...@@ -23,7 +20,6 @@ public class Main { ...@@ -23,7 +20,6 @@ public class Main {
* TODOs for "LOG IN" USE CASE * TODOs for "LOG IN" USE CASE
* TODO: Implement the methods in the Student class * TODO: Implement the methods in the Student class
* TODO: Create a class / method(s) for handling storage. Maybe something like ListIO? * 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: 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) * TODO: Implement a method to search for a user and log in (must implement the previous TODO to do this)
*/ */
...@@ -32,7 +28,6 @@ public class Main { ...@@ -32,7 +28,6 @@ public class Main {
* TODOs for "APPOINTMENT" USE CASE * TODOs for "APPOINTMENT" USE CASE
* TODO: Implement the methods in the Appointment class * TODO: Implement the methods in the Appointment class
* TODO: Implement the methods in the RequestAppointment class * TODO: Implement the methods in the RequestAppointment class
* TODO: Implement the methods in the Reminder class
*/ */
public static void main(String[] args) { public static void main(String[] args) {
...@@ -40,23 +35,37 @@ public class Main { ...@@ -40,23 +35,37 @@ public class Main {
String q = ""; String q = "";
String currentLogin = ""; String currentLogin = "";
// Open all persistent storage // Uncomment all of this for the "hardcoded" version.
CourseList courseList = openCourseList();
UserList userList = openUserList();
// Uncomment to do testing // Uncomment to add a new fresh userList and courseList
TeachingAssistant testTa = (TeachingAssistant) userList.userList.get(0); CourseList courseList = new CourseList();
Admin testAdmin = (Admin) userList.userList.get(1); UserList userList = new UserList();
// Uncomment here to add a new TA or Admin // Uncomment here to add a new TA or Admin
// TeachingAssistant testTa = Student falseStudent =
// new TeachingAssistant("akuTA", "Aku Asdos", Long.valueOf("123456789"), userList); new Student("falseStudent", "False Student", Long.valueOf("123456789"), userList);
// testTa.setSchedule("monday", "Free", "12:00", "17:00"); TeachingAssistant falseTeachingAssistant =
// testTa.setSchedule("tuesday", "Free", "08:00", "11:00"); new TeachingAssistant("falseTA", "False TA", Long.valueOf("123456789"), userList);
// testTa.setSchedule("tuesday", "Free", "12:00", "15:00"); Admin falseAdmin =
// testTa.setSchedule("saturday", "Busy", "07:00", "21:00"); new Admin("falseAdmin", "False Admin", userList);
// testTa.setSchedule("sunday", "Free", "12:00", "17:00"); TeachingAssistant testTa =
// Admin testAdmin = new Admin("akuAdmin", "Aku Admin", userList); 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);
Admin testAdmin = new Admin("akuAdmin", "Aku Admin", userList);
saveList(courseList, userList);
// // Open all persistent storage
// CourseList courseList = openCourseList();
// UserList userList = openUserList();
//
// // Uncomment to do testing
// TeachingAssistant testTa = (TeachingAssistant) userList.userList.get(0);
// Admin testAdmin = (Admin) userList.userList.get(1);
// // Store userList test // // Store userList test
// try { // try {
...@@ -82,10 +91,20 @@ public class Main { ...@@ -82,10 +91,20 @@ public class Main {
} }
// TODO: Maybe it is better to move this whole if block into a different class // TODO: Maybe it is better to move this whole if block into a different class
checkLogin(q); String[] splited = {"", ""};
currentLogin = q; splited = q.split("\\s+");
String searchUsername = splited[1];
String role = splited[0];
currentLogin = role;
if (currentLogin.equals("T")) { if (currentLogin.equals("T")) {
// Find teaching assistant in userList. If not exist, immediately log out
TeachingAssistant currentTA = userList.searchTA(searchUsername);
if (currentTA.getUsername().equals("falseTA")) {
q = "LOG OUT";
System.out.println("Wrong username!");
}
while (!q.equals("LOG OUT")) { while (!q.equals("LOG OUT")) {
saveList(courseList, userList); saveList(courseList, userList);
System.out.print("Input: "); System.out.print("Input: ");
...@@ -96,11 +115,11 @@ public class Main { ...@@ -96,11 +115,11 @@ public class Main {
break; break;
case "CALENDAR": case "CALENDAR":
testTa.seeCalendar(); currentTA.seeCalendar();
break; break;
case "COURSELIST": case "COURSELIST":
testTa.seeCourseList(courseList); currentTA.seeCourseList(courseList);
break; break;
case "ADD SCHEDULE": case "ADD SCHEDULE":
...@@ -113,7 +132,7 @@ public class Main { ...@@ -113,7 +132,7 @@ public class Main {
String startTime = sc.nextLine(); String startTime = sc.nextLine();
System.out.println("Add end time 'XX:XX'. Example, '09:40': "); System.out.println("Add end time 'XX:XX'. Example, '09:40': ");
String endTime = sc.nextLine(); String endTime = sc.nextLine();
testTa.setSchedule(day, name, startTime, endTime); currentTA.setSchedule(day, name, startTime, endTime);
System.out.println("Sucessfully added a new schedule!"); System.out.println("Sucessfully added a new schedule!");
case "LOG OUT": case "LOG OUT":
...@@ -125,6 +144,12 @@ public class Main { ...@@ -125,6 +144,12 @@ public class Main {
} }
} }
} else if (currentLogin.equals("A")) { } else if (currentLogin.equals("A")) {
// Find Admin in userList. If not exist, immediately log out
Admin currentAdmin = userList.searchAdmin(searchUsername);
if (currentAdmin.getUsername().equals("falseAdmin")) {
q = "LOG OUT";
System.out.println("Wrong username!");
}
while (!q.equals("LOG OUT")) { while (!q.equals("LOG OUT")) {
saveList(courseList, userList); saveList(courseList, userList);
System.out.print("Input: "); System.out.print("Input: ");
...@@ -135,11 +160,11 @@ public class Main { ...@@ -135,11 +160,11 @@ public class Main {
break; break;
case "COURSELIST": case "COURSELIST":
testAdmin.seeCourseList(courseList); currentAdmin.seeCourseList(courseList);
break; break;
case "USERLIST": case "USERLIST":
testAdmin.getUserList(userList); currentAdmin.getUserList(userList);
break; break;
case "ADD COURSE": case "ADD COURSE":
...@@ -181,13 +206,9 @@ public class Main { ...@@ -181,13 +206,9 @@ public class Main {
System.out.println(); System.out.println();
} }
public static void checkLogin(String q) { // public static Student checkLoginStudent(String q, String role, UserList userList) {
switch (q) { //
case "S" -> introStudent(); // }
case "T" -> introTA();
case "A" -> introAdmin();
}
}
public static void introStudent() { public static void introStudent() {
System.out.println("Available commands:"); System.out.println("Available commands:");
...@@ -203,7 +224,6 @@ public class Main { ...@@ -203,7 +224,6 @@ public class Main {
System.out.println("CALENDAR \t\t\t See your calendar."); System.out.println("CALENDAR \t\t\t See your calendar.");
System.out.println("COURSELIST \t\t\t See all courses."); System.out.println("COURSELIST \t\t\t See all courses.");
System.out.println("ADD SCHEDULE \t\t Add new schedule."); System.out.println("ADD SCHEDULE \t\t Add new schedule.");
// TAQuery();
} }
public static void introAdmin() { public static void introAdmin() {
...@@ -219,8 +239,7 @@ public class Main { ...@@ -219,8 +239,7 @@ public class Main {
// Open courselist.txt for persistent storage of list of courses // Open courselist.txt for persistent storage of list of courses
public static CourseList openCourseList() { public static CourseList openCourseList() {
try try {
{
// Reading the object from a file // Reading the object from a file
FileInputStream file = new FileInputStream("src/softeng/g4/courselist.txt"); FileInputStream file = new FileInputStream("src/softeng/g4/courselist.txt");
ObjectInputStream in = new ObjectInputStream(file); ObjectInputStream in = new ObjectInputStream(file);
...@@ -232,15 +251,9 @@ public class Main { ...@@ -232,15 +251,9 @@ public class Main {
file.close(); file.close();
return courseList; return courseList;
} } catch (IOException ex) {
catch(IOException ex)
{
System.out.println("IOException is caught"); System.out.println("IOException is caught");
} } catch (ClassNotFoundException ex) {
catch(ClassNotFoundException ex)
{
System.out.println("ClassNotFoundException is caught"); System.out.println("ClassNotFoundException is caught");
} }
...@@ -249,8 +262,7 @@ public class Main { ...@@ -249,8 +262,7 @@ public class Main {
// Open userlist.txt for persistent storage of list of users // Open userlist.txt for persistent storage of list of users
public static UserList openUserList() { public static UserList openUserList() {
try try {
{
// Reading the object from a file // Reading the object from a file
FileInputStream file = new FileInputStream("src/softeng/g4/userlist.txt"); FileInputStream file = new FileInputStream("src/softeng/g4/userlist.txt");
ObjectInputStream in = new ObjectInputStream(file); ObjectInputStream in = new ObjectInputStream(file);
...@@ -262,15 +274,9 @@ public class Main { ...@@ -262,15 +274,9 @@ public class Main {
file.close(); file.close();
return userList; return userList;
} } catch (IOException ex) {
catch(IOException ex)
{
System.out.println("IOException is caught"); System.out.println("IOException is caught");
} } catch (ClassNotFoundException ex) {
catch(ClassNotFoundException ex)
{
System.out.println("ClassNotFoundException is caught"); System.out.println("ClassNotFoundException is caught");
} }
......
...@@ -45,4 +45,12 @@ public class Admin extends User { ...@@ -45,4 +45,12 @@ public class Admin extends User {
public String toString() { public String toString() {
return username; return username;
} }
public String getRole() {
return role;
}
public String getUsername() {
return username;
}
} }
package softeng.g4.user; package softeng.g4.user;
public class Student extends UserStudent { public class Student extends UserStudent {
private String username;
private String fullname;
private String role;
private long npm;
// Constructor
public Student(String username, String fullname, long npm, UserList userList) {
this.username = username;
this.fullname = fullname;
this.role = "S";
this.npm = npm;
// Register new TA to the userList
userList.userList.add(this);
}
public void addAppointment() {} public void addAppointment() {}
public void addReminder() {} public void addReminder() {}
public void addDescription() {} public void addDescription() {}
public void seeCalendar(long npm) {} public void seeCalendar(long npm) {}
public String getRole() {
return role;
}
public String getUsername() {
return username;
}
} }
...@@ -44,4 +44,12 @@ public class TeachingAssistant extends UserStudent { ...@@ -44,4 +44,12 @@ public class TeachingAssistant extends UserStudent {
public String toString() { public String toString() {
return username; return username;
} }
public String getRole() {
return role;
}
public String getUsername() {
return username;
}
} }
...@@ -4,6 +4,13 @@ import java.io.Serializable; ...@@ -4,6 +4,13 @@ import java.io.Serializable;
public abstract class User implements Serializable { public abstract class User implements Serializable {
private String username; private String username;
private String fullname;
private String role; private String role;
public String getRole() {
return role;
}
public String getUsername() {
return username;
}
} }
...@@ -7,4 +7,39 @@ import java.util.ArrayList; ...@@ -7,4 +7,39 @@ import java.util.ArrayList;
public class UserList implements Serializable { public class UserList implements Serializable {
public ArrayList<User> userList = new ArrayList<User>(); public ArrayList<User> userList = new ArrayList<User>();
// Returns student
public Student searchStudent(String username) {
for (User user : userList) {
try {
Student currentUser = (Student) user;
if (currentUser.getUsername().equals(username) && currentUser.getRole().equals("S")) return currentUser;
} catch (Exception ex) { }
}
// Return a student called falseStudent, so user will have to re-login with the correct username
return (Student) userList.get(0);
}
public TeachingAssistant searchTA(String username) {
for (User user : userList) {
try {
TeachingAssistant currentUser = (TeachingAssistant) user;
if (currentUser.getUsername().equals(username) && currentUser.getRole().equals("TA")) return currentUser;
} catch (Exception ex) { }
}
// Return a student called falseStudent, so user will have to re-login with the correct username
return (TeachingAssistant) userList.get(1);
}
public Admin searchAdmin(String username) {
for (User user : userList) {
try {
Admin currentUser = (Admin) user;
if (currentUser.getUsername().equals(username) && currentUser.getRole().equals("A")) return currentUser;
} catch (Exception ex) { }
}
// Return a student called falseStudent, so user will have to re-login with the correct username
return (Admin) userList.get(2);
}
} }
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