Wednesday, April 10, 2013

Password Check program

This is a password check program. This program applies the following rule to the strong password: It must contain digits, lower case and upper case characters, symbols, and eight characters long. In this program, you will learn to create graphic user interface program with AWT controls, use the Character class to determine whether a character is a digit, lower case, or upper case, and write your own method to check whether a character is a symbol.

PasswordCheckProgram source code:

import java.awt.*;
import java.awt.event.*;
class  Login extends Frame implements TextListener{
            Panel panel;
            Label lblpassword;
            Label lblmess;
            TextField txtpassword;
           

            Login(){                     
1                      setSize(600,150);
2                      setResizable(false);
3                      setTitle("Password Check Program");
4                      lblpassword=new Label("Password:");
5                      lblmess=new Label("A strong password must contain digits, \nlower and upper letters, symbols,and eight characters long.");
6                      txtpassword=new TextField(20);
7                      txtpassword.setEchoChar('*');
8                      txtpassword.addTextListener(this);
9                      panel=new Panel();
10                    panel.add(lblpassword);
11                    panel.add(txtpassword);
12                    panel.add(lblmess);
13                    panel.setLayout(new FlowLayout());                   
14                    add(panel, BorderLayout.CENTER);
15                    addWindowListener(new WindowAdapter(){
                                    public void windowClosing(WindowEvent e){
                                                System.exit(0);
                                    }
                        });
16                    setLocationRelativeTo(null);
17                    setVisible(true);                   
18                    txtpassword.requestFocus();                     
                       
            }



19 public void textValueChanged(TextEvent e){
                        String mess=checkStrength(txtpassword.getText().toCharArray());
                        showMess(mess,lblpassword);
}

20 public void showMess(String mess, Label lbl){
                        lbl.setText(mess);
                        lbl.setForeground(Color.RED);                
            }

21 public String checkStrength(char[] passw){
                        String mess=null;
                        if(containsLowercase(passw) && containsUppercase(passw) && containsDigit(passw) &&
containsSymbol(passw) ){
                                    if(passw.length>=8)
                       
                                                mess="Strong";
                                    else    
                                                mess="Medium";
                        }
                        else
                                    mess="Weak";
                       
                        return mess;
}

22 public boolean containsLowercase(char [] passw){
                        boolean hasLower=false;
                        for(char c:passw){
                                    if(Character.isLowerCase(c)) {
                                                hasLower=true;
                                                break;
                                    }
                        }

                        return hasLower;
            }


23 public boolean containsUppercase(char [] passw){
                        boolean hasUpper=false;
                        for(char c:passw){
                                    if(Character.isUpperCase(c)) {
                                                hasUpper=true;
                                                break;
                                    }
                        }

                        return hasUpper;
            }
24 public boolean containsDigit(char [] passw){
                        boolean hasDigit=false;
                        for(char c:passw){
                                    if(Character.isDigit(c)) {
                                                hasDigit=true;
                                                break;
                                    }
                        }

                        return hasDigit;
            }

25 public boolean containsSymbol(char [] passw){

                        char[] symbols={'!','@','#','$','%','^','&','*','(',')','_','-','+','=','{','}',’[’,’]’};
                        String passwText=new String(passw);
                        boolean hasSymbol=false;
                        for(char c:symbols){
                                    if(passwText.indexOf(c)!=-1) {
                                                hasSymbol=true;
                                                break;
                                    }
                        }

                        return hasSymbol;
            }
}


26 public class PasswordCheckProgram{
 
            public static void main(String args[]){
                        new Login();
  
            }


}




1 Specify the size of the program window.
2 Disable window resizing.
3 Set the title of the program (Password Check Program).
4 Create a Label lblpassword object to show "Password:" text when the program firstly loads.
5 Create a Label lblmess object to show red-color message (Strong, Medium, Weak).
6 Create a TextFeld txtpassword object to enable password input.
7 Specify the character to display on the password text box.
8 Register the password text box with the text event.
9-12 Create a Panel panel object and add all above objects in to it.
13 Specify the layout of controls to display.
14 Add panel object to the program window.
15 The program window can close when the its close button is clicked.
16 Display the program window at the center of the screen.
17 Make use the program window is visible.
18 Focus on the password box when the program loads.
19 The textValueChanged method is rewritten to enable the password box to perform action when its value changes. Its action is to check whether the input password is strong.
20 The showMess method display the text "Strong", "Weak", or  "Medium" in red color. It makes change to the text displaying on the lblshow label.
21 The checkStrenght method invokes the containsLowercase, containsUppercase,containsDigit, and containsSymbol methods to determine whether password input is strong.
22 The containsLowercase method checks whether the password contains an lower case character. The isLowerCase( char c) method of the Character is used to perform this operation.
23 The containsUppercase method checks whether the password contains an upper case character. The isUpperCase( char c) method of the Character is used to perform this operation.
24 The containsDigit method checks whether the password contains a digit character. The isDigit( char c) method of the Character is used to perform this operation.
25 The containsSymbol method checks whether the password contains a symbol. The code starts by creating a list of symbols then the for loop is used to check whether any symbol in the list is found in the password text.

No comments:

Post a Comment