Class User

  • All Implemented Interfaces:
    Comparable<User>
    Direct Known Subclasses:
    Professor, Student

    public class User
    extends Object
    implements Comparable<User>
    Represents a user who is either a professor or student. All users have a unique username and store the courses they are currently teaching (professor) or enrolled in (student), in a tree set that is organized based on their preference (see subclasses).
    • Field Detail

      • username

        private String username
        The username (unique)
      • courses

        private TreeSet<Course> courses
        The courses the professor is teaching, or student is enrolled in
    • Constructor Detail

      • User

        public User​(String username,
                    User.UserType type,
                    Comparator<Course> comp)
        Create a new user.
        Parameters:
        username - the username (must be unique)
        type - the user type (professor or student)
        comp - the comparator that the tree set, courses, is to used order the courses
    • Method Detail

      • getUsername

        public String getUsername()
        Get the username.
        Returns:
        username
      • getType

        public User.UserType getType()
        Get the user type.
        Returns:
        user type
      • addCourse

        public boolean addCourse​(Course course)
        Add a course for this user. For a professor it means they add it to the courses they are teaching. If they are a student, they are enrolling in the course.
        Parameters:
        course - the course to add.
        Returns:
        whether the course was added or not (would be false, if the course was not in the course
      • removeCourse

        public boolean removeCourse​(Course course)
        Remove of a course for this user. In both cases for a professor or student the meaning is the course no longer exists in their collection of courses.
        Parameters:
        course - the course to remove
        Returns:
        true if the student was removed from the course, false if the student was not in the course
      • getCourses

        public Collection<Course> getCourses()
        Returns the courses the user is currently teaching or enrolled in.
        Returns:
        the courses
      • equals

        public boolean equals​(Object other)
        Two users are equal if they have the same username.
        Overrides:
        equals in class Object
        Parameters:
        other - the other user to compare to
        Returns:
        whether they are equal or not
      • hashCode

        public int hashCode()
        The hash code of a user is the hash code of the username.
        Overrides:
        hashCode in class Object
        Returns:
        hash code
      • toString

        public String toString()
        Returns a string representation of the user in the format:

        User{username=USERNAME, type=TYPE, courses=COURSE_LIST}

        Here, COURSE_LIST is a list of courses, with brackets surrounding the comma separated entries, which are the course names in ascending alphabetical order for students, and first by course level and second by ascending alphabetical course name for professors.
        Overrides:
        toString in class Object
        Returns:
        the formatted string
      • compareTo

        public int compareTo​(User other)
        Users are naturally ordered alphabetically by username.
        Specified by:
        compareTo in interface Comparable<User>
        Parameters:
        other - the user to compare to
        Returns:
        a value less than 0 if this username is less than other's username, 0 if the usernames are the same, a value greater than 0 if this username is greaterh than other's username.