示例#1
0
  /**
   * @param userList
   * @return Student Register new student by prompting them for user info
   */
  public static Student registerStudentAccount(LinkedList<User> userList) {
    String username = "";
    String password = "";
    String first = "";
    String last = "";
    String GNum = "";
    String phoneNum = "";
    String email = "";
    String address = "";
    User aStudent = null;

    // prompt for username until available username is entered
    do {
      username = JOptionPane.showInputDialog("Please enter desired username");
      aStudent = validateUsername(username, userList);
      if (aStudent != null) {
        JOptionPane.showMessageDialog(null, "This username is already in use!\nPlease try again");
      }
    } while (aStudent != null);

    // create student object
    aStudent = new Student(username);

    // prompt for password until valid entry is given
    do {
      password = JOptionPane.showInputDialog("Please enter desired password");
      if (!aStudent.setPassword(password)) {
        JOptionPane.showMessageDialog(
            null,
            "Password does not meet requirements. Minimum 8 characters\nTry Again.",
            "Error",
            JOptionPane.ERROR_MESSAGE);
      }
    } while (!aStudent.setPassword(password));

    // prompt for first name until valid entry is made
    do {
      first = JOptionPane.showInputDialog("Please enter your first name");
      if (!aStudent.setFirstName(first)) {
        JOptionPane.showMessageDialog(null, "Invalid entry \nPlease try again.");
      }
    } while (!aStudent.setFirstName(first));

    // prompt for last name until valid entry is made
    do {
      last = (JOptionPane.showInputDialog("Please enter your last name"));
      if (!aStudent.setLastName(last)) {
        JOptionPane.showMessageDialog(null, "Invalid entry \nPlease try again");
      }
    } while (!aStudent.setLastName(last));

    // prompt for G-Number until valid entry is made
    do {
      GNum = (JOptionPane.showInputDialog("Please enter your G-number"));
      if (!aStudent.setgNumber(GNum)) {
        JOptionPane.showMessageDialog(
            null,
            "Invalid entry! Please write your GNumber in this format 00XXXXXX \nPlease try again",
            "Error",
            JOptionPane.ERROR_MESSAGE);
      }
    } while (!aStudent.setgNumber(GNum));

    // prompt for phone number until valid entry is made
    do {
      phoneNum = (JOptionPane.showInputDialog("Please enter your phone number"));
      if (!aStudent.setPhoneNumber(phoneNum)) {
        JOptionPane.showMessageDialog(
            null,
            "Invalid entry. Please write your phone number in XXXXXXXXXX format \nPlease try again",
            "Error",
            JOptionPane.ERROR_MESSAGE);
      }
    } while (!aStudent.setPhoneNumber(phoneNum));

    // prompt for email until valid entry is made
    do {
      email = (JOptionPane.showInputDialog("Please enter your Email address"));
      if (!aStudent.setEmail(email)) {
        JOptionPane.showMessageDialog(
            null,
            "Invalid entry, correct format: [email protected] \nPlease try again",
            "Error",
            JOptionPane.ERROR_MESSAGE);
      }
    } while (!aStudent.setEmail(email));

    // prompt for address until valid entry is made
    Student nStudent = (Student) aStudent;

    do {
      address = (JOptionPane.showInputDialog("Please enter your shipping address"));
      if (!nStudent.setShippingAddress(address)) {
        JOptionPane.showMessageDialog(null, "Invalid entry \nPlease try again");
      }
    } while (!nStudent.setShippingAddress(address));

    JOptionPane.showMessageDialog(null, "Your account has been created");
    try {
      PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("accounts.txt", true)));

      pw.println(
          "\r\n-"
              + aStudent.getFirstName()
              + ","
              + aStudent.getLastName()
              + ","
              + aStudent.getgNumber()
              + ","
              + aStudent.getPassword()
              + ","
              + aStudent.getPhoneNumber()
              + ","
              + aStudent.getEmail()
              + ","
              + aStudent.getUsername()
              + ","
              + nStudent.getShippingAddress());
      pw.close();

    } catch (IOException e) {
      e.printStackTrace();
    }

    userList.add(aStudent);
    return nStudent;
  }
示例#2
0
  /** @param userList Populate system with list of students accounts from text file */
  public static void populateStudentAccounts(LinkedList<User> userList) {
    Scanner inputStream = null;
    // open text file of accounts
    try {
      inputStream = new Scanner(new FileInputStream("accounts.txt"));
      // System.out.println("accounts file read");
    } catch (FileNotFoundException e) {
      JOptionPane.showMessageDialog(null, "The file \"accounts.txt\" could not be found");
      JOptionPane.showMessageDialog(null, "The system will now exit");
      System.exit(0);
    }

    // Pull line of text to generate a student
    while (inputStream.hasNextLine()) {
      String s1 = inputStream.nextLine();
      // locate first name
      int fNsameStart = (s1.indexOf("-") + 1);
      int fNameEnd = (s1.indexOf(","));
      String fName = s1.substring(fNsameStart, fNameEnd);
      // locate Last name
      int lNameStart = (s1.indexOf(",", fNameEnd) + 1);
      int lNameEnd = (s1.indexOf(",", lNameStart));
      String lName = s1.substring(lNameStart, lNameEnd);
      // locate gNumber
      int gNumberStart = (s1.indexOf(",", lNameEnd) + 1);
      int gNumberEnd = (s1.indexOf(",", gNumberStart));
      String gNumber = s1.substring(gNumberStart, gNumberEnd);
      // locate password
      int passwordStart = (s1.indexOf(",", gNumberEnd) + 1);
      int passwordEnd = (s1.indexOf(",", passwordStart));
      String password = s1.substring(passwordStart, passwordEnd);
      // locate phone number
      int phoneNumberStart = (s1.indexOf(",", passwordEnd + 1));
      int phoneNumberEnd = (s1.indexOf(",", phoneNumberStart));
      String phoneNumber = s1.substring(phoneNumberStart, phoneNumberEnd);
      // locate email
      int emailStart = (s1.indexOf(",", phoneNumberEnd) + 1);
      int emailEnd = (s1.indexOf(",", emailStart));
      String email = s1.substring(emailStart, emailEnd);
      // locate username
      int usernameStart = (s1.indexOf(",", emailEnd) + 1);
      int usernameEnd = (s1.indexOf(",", usernameStart));
      String username = s1.substring(usernameStart, usernameEnd);
      // locate address
      int addressStart = (s1.indexOf(",", usernameEnd) + 1);
      String address = s1.substring(addressStart);

      // create student object and populate info
      User aStudent = new Student(username);
      aStudent.setFirstName(fName);
      aStudent.setLastName(lName);
      aStudent.setgNumber(gNumber);
      aStudent.setPassword(password);
      aStudent.setPhoneNumber(phoneNumber);
      aStudent.setEmail(email);
      if (aStudent instanceof Student) {
        ((Student) aStudent).setShippingAddress(address);
      }

      // add Student to list
      userList.add(aStudent);
      System.out.println(userList.size());
    }
  }