Changing password logic - old password, new password, retype password

I have a MainFrame class which has a Change Password button. The action for the button is:

// Change password

aListener =new ActionListener(){

publicvoid actionPerformed(ActionEvent e){

ChangeMyPassword cmp =new ChangeMyPassword(

MainFrame.this);

cmp.dispose();

}

};

changePasswordButton.addActionListener(aListener);

When the user press theChange Password button, it pass control to the ChangeMyPassword class which as a submit button and a cancel button. When the user press the submit button, it should validate theold password (i.e. compare it to the password in the text file), validate thenew password match theretype password:

// submit button

ActionListener aListener =new ActionListener(){

publicvoid actionPerformed(ActionEvent e){

// Retrieve the password.

char[] pw = oldPasswordField.getPassword();

oldPassword =new String(pw).trim();

pw = newPasswordField.getPassword();

newPassword =new String(pw).trim();

pw = retypePasswordField.getPassword();

retypePassword =new String(pw).trim();

// Hide, but don't dispose of, this window ...

// we need to give the client code a chance to

// retrieve the user's typed response via

// the getPassword() method first.

ChangeMyPassword.this.setVisible(false);

}

};

submitButton.addActionListener(aListener);

I have a Student class which compare theold password with the password in the text file. The student class constructor takes the student id to identify which student text file it should call.

The problem is that the ChangeMyPassword does not know about student id (because I am getting these from the text field in MainFrame class). So how can I call the validatePassword method in Student class?

publicboolean validatePassword(String pw){

if (pw ==null)returnfalse;

if (pw.equals(password))returntrue;

elsereturnfalse;

}

[3333 byte] By [SDNJavaa] at [2007-11-27 7:05:17]
# 1
Why dont you just simply pass the value of the current student id to the ChangeMyPassword class constructor?
DarumAa at 2007-7-12 18:56:38 > top of Java-index,Java Essentials,Java Programming...
# 2

I don抰 understand why it does not update the text file with the new password.

The submit button has the action in ChangeMyPassword class:

// submit button

ActionListener aListener = new ActionListener() {

public void actionPerformed(ActionEvent e) {

// Retrieve the password.

char[] pw = oldPasswordField.getPassword();

oldPassword = new String(pw).trim();

pw = newPasswordField.getPassword();

newPassword = new String(pw).trim();

pw = retypePasswordField.getPassword();

retypePassword = new String(pw).trim();

Student theStudent = new Student(id);

if(theStudent.validatePassword(oldPassword)){

if(validateChangePassword()){

// write the new password to file

theStudent.setPassword(getNewPassword());

// Let the user know that the

// password succeeded.

JOptionPane.showMessageDialog(null,

"Password change successfully.",

"Password Changed",

JOptionPane.INFORMATION_MESSAGE);

}else{

JOptionPane.showMessageDialog(null,

"You have used an invalid user name or password; please try again.",

"Authentication failed",

JOptionPane.WARNING_MESSAGE);

}

}else{

JOptionPane.showMessageDialog(null,

"You have used an invalid user name or password; please try again.",

"Authentication failed",

JOptionPane.WARNING_MESSAGE);

}

// Hide, but don't dispose of, this window ...

// we need to give the client code a chance to

// retrieve the user's typed response via

// the getPassword() method first.

ChangeMyPassword.this.setVisible(false);

}

};

submitButton.addActionListener(aListener);

Validate password method in Student class:

public boolean validatePassword(String pw) {

if (pw == null) return false;

if (pw.equals(password)) return true;

else return false;

}

Validate change password method in ChangeMyPassword class:

public boolean validateChangePassword(){

if(newPassword.equals(retypePassword))return true;

else return false;

}

When the user press the Change Password button the action performed in the MainFrame class is:

// Change password

aListener = new ActionListener() {

public void actionPerformed(ActionEvent e) {

String id = ssnField.getText();

ChangeMyPassword cmp = new ChangeMyPassword(MainFrame.this, id);

boolean success = currentUser.persist();

cmp.dispose();

}

};

changePasswordButton.addActionListener(aListener);

Now remember that I set the password that should be written in ChangeMyPassword class as you can see above. Now that I called the persist method, it should write all the details back to the text. For some reason it not doing that and I don抰 understand why.

The persist method:

public boolean persist() {

FileOutputStream fos = null;

PrintWriter pw = null;

try {

// Attempt to create the ssn.dat file. Note that

// it will overwrite one if it already exists.

fos = new FileOutputStream("C://Files//" + getSsn() + ".dat");

pw = new PrintWriter(fos);

// First, we output the header record as a tab-delimited

// record.

pw.println(getSsn() + "\t" + getName() + "\t" +

getMajor() + "\t" + getDegree() + "\t" + getPassword());

// Then, we output one record for every Section that

// the Student is enrolled in.

for (int i = 0; i < attends.size(); i++) {

Section s = (Section) attends.elementAt(i);

pw.println(s.getFullSectionNo());

}

pw.close();

fos.close(); //? needed?

}

catch (IOException e) {

// Signal that an error has occurred.

return false;

}

// All is well!

return true;

}

SDNJavaa at 2007-7-12 18:56:38 > top of Java-index,Java Essentials,Java Programming...
# 3
Can you take out all the extra stuff and just post a working subset of code so it can be tested. I can not see any errors by looking at the code. Will need to debug it to see why it is not behaving as expected.
DarumAa at 2007-7-12 18:56:38 > top of Java-index,Java Essentials,Java Programming...