Class Backend


  • public class Backend
    extends Object
    This class represents that backend that SIS (frontend) interfaces with. It creates the course and user databases from the input files. It is resposible for taking requests from SIS and invoking the appropriate operations on the databases.

    A course file is a comma separated text file in the format (one per line):

    {CourseId},{Course_Name},{CourseLevel}

    A professor file is a comma separated text file in the format (one per line):

    {ProfessorUsername},{CourseId_1},{CourseId_2},...

    A student file is a comma separated text file in the format (one per line):

    {StudentUsername},{CourseId_1},{CourseId_2},...

    • Field Detail

      • courseDB

        private CourseDB courseDB
        the database of courses
      • userDB

        private UserDB userDB
        the database of users
    • Constructor Detail

      • Backend

        public Backend​(String courseFile,
                       String professorFile,
                       String studentFile)
                throws FileNotFoundException
        Creates the backend by initializing the course and user databases.
        Parameters:
        courseFile - name of the course file
        professorFile - name of the professor file
        studentFile - name of the student file
        Throws:
        FileNotFoundException - if any of the files cannot be found
    • Method Detail

      • initializeCourseDB

        private void initializeCourseDB​(String courseFile)
                                 throws FileNotFoundException
        A utility method for initializing the course database.
        Parameters:
        courseFile - name of the course file
        Throws:
        FileNotFoundException - if the courses file cannot be found
      • initializeUserDB

        private void initializeUserDB​(String professorFile,
                                      String studentFile)
                               throws FileNotFoundException
        A utility method for initializing the user database.
        Parameters:
        professorFile - name of the professor file
        studentFile - name of the student file
        Throws:
        FileNotFoundException - if the
      • addCourses

        private void addCourses​(User user,
                                String[] courseIds)
        A utility "utility" method. Used by initializeUserDB when adding a professor or student to a collection of courses.
        Parameters:
        user - the user
        courseIds - a collection of course ids
      • courseExists

        public boolean courseExists​(int id)
        Check whether a course exists or not.
        Parameters:
        id - the course id
        Returns:
        whether the course exists or not
      • enrollStudent

        public boolean enrollStudent​(String username,
                                     int courseId)
        Enroll a student in a course. This requires they are added to both the course and the student's courses.
        Parameters:
        username - the username of the student
        courseId - the course if of the student
        Returns:
        whether the student was added or not (false if already enrolled)
        Precondition
        the user exists, the user is a student, the course exists
      • getAllCourses

        public Collection<Course> getAllCourses()
        Get all the courses in the system.
        Returns:
        all the courses
      • getAllUsers

        public Collection<User> getAllUsers()
        Get all users in the system.
        Returns:
        all the users
      • isStudent

        public boolean isStudent​(String username)
        Check whether a username belongs to a student.
        Parameters:
        username - the username
        Returns:
        whether the username belongs to a student or not
        Precondition
        the username exists in the database
      • getCourse

        public Course getCourse​(int id)
        Get a course by id.
        Parameters:
        id - the course id
        Returns:
        the course associated with the id
        Precondition
        the course exists
      • unenrollStudent

        public boolean unenrollStudent​(String username,
                                       int courseId)
        Unenroll a student in a course. This requires they are removed from both the course and the student's courses.
        Parameters:
        username - the username of student to unenroll
        courseId - the id of the course
        Returns:
        true if the student was unerolled, false if the student was not enrolled in the course
        Precondition
        the user exists, the user is a student, the course exists
      • userExists

        public boolean userExists​(String username)
        Check if a username exists in the system.
        Parameters:
        username - the username to check
        Returns:
        whether the username is in the system or not
      • getCoursesUser

        public Collection<Course> getCoursesUser​(String username)
        Get the courses for a particular user.
        Parameters:
        username - the username
        Returns:
        the collection of courses for a user
        Precondition
        the username exists