-4

I was just creating this specific but I was a little confused on documenting this. Am just stuck on explaining what the last couple of lines do :

class MyVerifier extends InputVerifier {

public boolean verify(JComponent input) {

  if (input==id) {
    return validId();

}

 else if (input==name) {
     return validName();

 }

 return false;
}

    public boolean validId() {
      boolean status;
      String theID = id.getText();
      Pattern pattern = Pattern.compile("\\d{8}");
      Matcher matcher = pattern.matcher(theID);
      if (matcher.matches()) {
          status = true;
      }
      else {
          status = false;
      }
       return status;
    }
    public boolean validName() {
       boolean status;
       String theName = name.getText();
       Pattern pattern = Pattern.compile("[A-za-z0-9 ]+");
       Matcher matcher = pattern.matcher(theName);
       if (matcher.matches()) {
           status = true;
       }
       else {
           status = false;
       }
       return status;
    }
}

COULD YOU EXPLAIN THESE SPECIFIC LINES HERE ONE BY ONE ?

/**
 * @param  o    the object corresponding to the user's selection
 */
@Override
public void tell(Object o) { -- Where has this come from ?
    deptCode.setText(o.toString());
}

@Override
public void actionPerformed(ActionEvent e) {
    if (e.getSource() == submit) {
        MyVerifier test = new MyVerifier();

        if (Staff.getStaff(id.getText()) == null && test.verify(id) &&
                test.verify(name)) {
            System.out.println("YAY");-- What is this doing
        }
        else if (!(Staff.getStaff(id.getText()) == null)) {
            String errorMessage = "ID EXISTS: " + Staff.getStaff(id.getText()).toString(); -- What is this doing

            JOptionPane.showMessageDialog(theFrame, errorMessage, "Error",
                JOptionPane.WARNING_MESSAGE);-- What is this doing
        }
        else {
            System.out.println("Woops.");
        }
    }

    else if (e.getSource() == clear) {
        id.setText(null);
        deptCode.setText(null);
        name.setText(null);
    }
}

public static void main(String[] args) {
    Registration test = new Registration();
}
}
musical_coder
  • 3,886
  • 3
  • 15
  • 18
Eddy
  • 11
  • 1
  • 8

2 Answers2

0

Now that you understand what you're trying to accomplish with this program, start from a clean slate (using your first attempt as an example if necessary). It's often easier to start over than to fix a program.

Daniel
  • 855
  • 10
  • 21
0

It appears that your public void tell(Object o) method is setting a String with the value of the object passed. Because you haven't shown us what your using it for, though, it's impossible for us to know for sure. On the other hand, your other problems are fairly clear:

System.out.println("YAY");

It appears that Staff.getStaff(id.getText) is checking either a String or a text file for a list of names and id's. This statement prints "YAY" only if there hasn't previously been created a staff member with the provided id and name. But since you also haven't shown us where those variables are, this is only my best guess.

JOptionPane.showMessageDialog(theFrame, errorMessage, "Error", JOptionPane.WARNING_MESSAGE);

This displays a JOptionPane warning message if there is already a staff member with the given id or name. Obviously, you cannot create an account that someone else has, so this JOptionPane displays an error message if this is, indeed, the case.

Omicron
  • 54
  • 1
  • 7