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;
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 softeng.g4.user.*;
import java.io.*;
import java.util.*;
......@@ -23,7 +20,6 @@ public class Main {
* 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)
*/
......@@ -32,7 +28,6 @@ public class Main {
* 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) {
......@@ -40,23 +35,37 @@ public class Main {
String q = "";
String currentLogin = "";
// Open all persistent storage
CourseList courseList = openCourseList();
UserList userList = openUserList();
// Uncomment all of this for the "hardcoded" version.
// Uncomment to do testing
TeachingAssistant testTa = (TeachingAssistant) userList.userList.get(0);
Admin testAdmin = (Admin) userList.userList.get(1);
// Uncomment to add a new fresh userList and courseList
CourseList courseList = new CourseList();
UserList userList = new UserList();
// 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);
Student falseStudent =
new Student("falseStudent", "False Student", Long.valueOf("123456789"), userList);
TeachingAssistant falseTeachingAssistant =
new TeachingAssistant("falseTA", "False TA", Long.valueOf("123456789"), userList);
Admin falseAdmin =
new Admin("falseAdmin", "False Admin", userList);
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);
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
// try {
......@@ -82,10 +91,20 @@ public class Main {
}
// TODO: Maybe it is better to move this whole if block into a different class
checkLogin(q);
currentLogin = q;
String[] splited = {"", ""};
splited = q.split("\\s+");
String searchUsername = splited[1];
String role = splited[0];
currentLogin = role;
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")) {
saveList(courseList, userList);
System.out.print("Input: ");
......@@ -96,11 +115,11 @@ public class Main {
break;
case "CALENDAR":
testTa.seeCalendar();
currentTA.seeCalendar();
break;
case "COURSELIST":
testTa.seeCourseList(courseList);
currentTA.seeCourseList(courseList);
break;
case "ADD SCHEDULE":
......@@ -113,7 +132,7 @@ 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);
currentTA.setSchedule(day, name, startTime, endTime);
System.out.println("Sucessfully added a new schedule!");
case "LOG OUT":
......@@ -125,6 +144,12 @@ public class Main {
}
}
} 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")) {
saveList(courseList, userList);
System.out.print("Input: ");
......@@ -135,11 +160,11 @@ public class Main {
break;
case "COURSELIST":
testAdmin.seeCourseList(courseList);
currentAdmin.seeCourseList(courseList);
break;
case "USERLIST":
testAdmin.getUserList(userList);
currentAdmin.getUserList(userList);
break;
case "ADD COURSE":
......@@ -181,13 +206,9 @@ public class Main {
System.out.println();
}
public static void checkLogin(String q) {
switch (q) {
case "S" -> introStudent();
case "T" -> introTA();
case "A" -> introAdmin();
}
}
// public static Student checkLoginStudent(String q, String role, UserList userList) {
//
// }
public static void introStudent() {
System.out.println("Available commands:");
......@@ -203,7 +224,6 @@ public class Main {
System.out.println("CALENDAR \t\t\t See your calendar.");
System.out.println("COURSELIST \t\t\t See all courses.");
System.out.println("ADD SCHEDULE \t\t Add new schedule.");
// TAQuery();
}
public static void introAdmin() {
......@@ -219,8 +239,7 @@ public class Main {
// Open courselist.txt for persistent storage of list of courses
public static CourseList openCourseList() {
try
{
try {
// Reading the object from a file
FileInputStream file = new FileInputStream("src/softeng/g4/courselist.txt");
ObjectInputStream in = new ObjectInputStream(file);
......@@ -232,15 +251,9 @@ public class Main {
file.close();
return courseList;
}
catch(IOException ex)
{
} catch (IOException ex) {
System.out.println("IOException is caught");
}
catch(ClassNotFoundException ex)
{
} catch (ClassNotFoundException ex) {
System.out.println("ClassNotFoundException is caught");
}
......@@ -249,8 +262,7 @@ public class Main {
// Open userlist.txt for persistent storage of list of users
public static UserList openUserList() {
try
{
try {
// Reading the object from a file
FileInputStream file = new FileInputStream("src/softeng/g4/userlist.txt");
ObjectInputStream in = new ObjectInputStream(file);
......@@ -262,15 +274,9 @@ public class Main {
file.close();
return userList;
}
catch(IOException ex)
{
} catch (IOException ex) {
System.out.println("IOException is caught");
}
catch(ClassNotFoundException ex)
{
} catch (ClassNotFoundException ex) {
System.out.println("ClassNotFoundException is caught");
}
......
......@@ -45,4 +45,12 @@ public class Admin extends User {
public String toString() {
return username;
}
public String getRole() {
return role;
}
public String getUsername() {
return username;
}
}
package softeng.g4.user;
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 addReminder() {}
public void addDescription() {}
public void seeCalendar(long npm) {}
public String getRole() {
return role;
}
public String getUsername() {
return username;
}
}
......@@ -44,4 +44,12 @@ public class TeachingAssistant extends UserStudent {
public String toString() {
return username;
}
public String getRole() {
return role;
}
public String getUsername() {
return username;
}
}
......@@ -4,6 +4,13 @@ import java.io.Serializable;
public abstract class User implements Serializable {
private String username;
private String fullname;
private String role;
public String getRole() {
return role;
}
public String getUsername() {
return username;
}
}
......@@ -7,4 +7,39 @@ import java.util.ArrayList;
public class UserList implements Serializable {
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);
}
}
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