abstract class· schoolcourseenrolmentsystem
User.java
User holds the profile fields that Students, Instructors and Administrators share, and forces each subclass to implement its own login and logout.
public abstract class User<T>
- Generic
- User<T>: each subclass passes itself as T
- Subclasses
- Student, Instructor, Administrator
- Nested enum
- Role { STUDENT, INSTRUCTOR, ADMIN }
- Validation
- None in setters, done by Helpers first
Responsibilities#
- One structure for every system user
- Common fields: name, ID, password, email, phone, role, address
- Abstract login and logout so each role authenticates against its own list
- Less duplication across the three subclasses
Fields & enums#
enum RoleL8
STUDENTINSTRUCTORADMINConstructors#
Empty object, used for the throwaway instance passed to Helpers.login.
User(String name, String id, String password, String email, String phoneNumber, Role role, String address)ParameterisedL21Sets every field through its setter, so any future setter validation applies automatically.
2 methods
Contract#
Every subclass must implement these two. Helpers.login<T> calls them through a throwaway instance.
Authenticate against a list of the subclass's own type.
- Returns the matching user, or null when authentication fails
Return a confirmation message for the user who is logging out.
Getters & setters#
Plain getters and setters for every field. Validation is kept out of them on purpose; the Helpers validators run before a setter is called.
14 accessors
String getName()L33void setName(String name)L37String getId()L41void setId(String ID)L45String getPassword()L49void setPassword(String password)L53String getEmail()L57void setEmail(String email)L60String getPhoneNumber()L64void setPhoneNumber(String phoneNumber)L68String getAddress()L72void setAddress(String address)L75Role getRole()L79void setRole(Role role)L83
Known issues#
Source
User.java91 lines · from the project report
1package schoolcourseenrolmentsystem;
3import java.util.List;
5public abstract class User<T> {
6 // Attributes
7 // enum is short for enumeration
8 public enum Role {
9 STUDENT,
10 INSTRUCTOR,
11 ADMIN;
12 }
14 private String name, ID, password, email, phoneNumber;
15 private Role role;
16 private String address;
18 public User() { }
20 // Constructor
21 public User(String name, String id, String password, String email, String phoneNumber, Role role, String address) {
22 setName(name);
23 setId(id);
24 setPassword(password);
25 setEmail(email);
26 setPhoneNumber(phoneNumber);
27 setRole(role);
28 setAddress(address);
30 }
31 //======================= Setters and Getters =======================
32 // we do validation in prior to calling this method to avoid bloating it
33 public String getName() {
34 return name;
35 }
37 public void setName(String name) {
38 this.name = name;
39 }
41 public String getId() {
42 return ID;
43 }
45 public void setId(String ID) {
46 this.ID = ID;
47 }
49 public String getPassword() {
50 return password;
51 }
53 public void setPassword(String password) {
54 this.password = password;
55 }
57 public String getEmail() {
58 return email;
59 }
60 public void setEmail(String email) {
61 this.email = email;
62 }
64 public String getPhoneNumber() {
65 return phoneNumber;
66 }
68 public void setPhoneNumber(String phoneNumber) {
69 // research on how to ensure that phone numbers follow the KSA pattern
70 this.phoneNumber = phoneNumber;
71 }
72 public String getAddress() {
73 return address;
74 }
75 public void setAddress(String address) {
76 this.address = address;
77 }
79 public Role getRole() {
80 return role;
81 }
83 public void setRole(Role role) {
84 this.role = role;
85 }
86 //======================= Methods =======================
87 // Abstract Methods
88 public abstract T login(List<T> list, String id, String password);
90 public abstract String logout(T user);
91}