Exemplo n.º 1
0
  /**
   * Prompt student with list of courses and add each on based on selection to their list of courses
   *
   * @param allCourses
   * @param aStudent
   */
  public static void menu(LinkedList<Course> courseList, User aUser, LinkedList<User> userList) {
    int selection = -1;
    final int MAX_COURSES = 7;
    boolean more;

    // send user if admin to add course
    Student aStudent = new Student();
    if (aUser instanceof Student) {
      aStudent = (Student) aUser;
      String menuPrompt =
          "Welcome to the GMU IT Bookstore!"
              + "\nPlease enter the number that corresponds to one of your classes";
      for (int i = 0; i < courseList.size(); i++) {
        Course aCourse = courseList.get(i);
        menuPrompt += "\n" + (i + 1) + ") " + aCourse.getCourseName();
      }

      do {
        do {
          try {
            selection = Integer.parseInt(JOptionPane.showInputDialog(menuPrompt));

            if (selection > courseList.size() || selection <= 0) {
              throw new IllegalArgumentException();
            }
          } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(
                null, "Invalid entry, please enter number associated with course");
            selection = -1;
          } catch (IllegalArgumentException e) {
            JOptionPane.showMessageDialog(null, "Invalid Selection. Try again.");
            selection = -1;
          }

        } while (selection > courseList.size() || selection <= 0);

        // add a course to student list of courses if its not already added to student's courses
        if (aStudent.getCourseList().contains(courseList.get(selection - 1))) {
          JOptionPane.showMessageDialog(null, "You have already ordered this book");
        } else if (aStudent.getCourseList().size() >= MAX_COURSES) {
          JOptionPane.showMessageDialog(
              null,
              "You have already registered " + MAX_COURSES + " courses, the max number allowed");
          break;
        } else {
          if (courseList.get(selection - 1).getTextStock() < 1) {
            JOptionPane.showMessageDialog(
                null, "This book is backordered and will take extra processing time");
          }
          Course aCourse = (courseList.get(selection - 1));
          aStudent.addCourse(aCourse);
          JOptionPane.showMessageDialog(
              null, courseList.get(selection - 1).getCourseText() + " Has been added to your cart");
        }

        // prompt student if they would like to continue entering courses
        int reply =
            JOptionPane.showConfirmDialog(
                null, "Do you have another class to enter?", "title", JOptionPane.YES_NO_OPTION);
        if (reply == 1) {
          more = false;
        } else {
          more = true;
        }
      } while (more && (aStudent.getCourseList().size() <= MAX_COURSES));

      // Give confirmation of order
      JOptionPane.showMessageDialog(null, "Your order has been entered\n");
    } else {
      // Admin tempAdmin;
      addCourse(courseList);
    }

    // return user to login screen
    login(userList, courseList);
  }