Question
I have to write a program that works with a user's password.
The program will prompt the user for a possible password. The program should check to be sure this
is a 'good' password. Specifically, a good password is one that will have to be
must be between 6 and 10 characters long
must contain at least one letter
must contain at least one digit
Continue to prompt the user for a password until these criteria are met. Once the user enters a 'good' password, prompt them to enter it again until the two passwords match.
Please note the way you'll write this program, the user will be able to see the characters that they type. While you wouldn't want to do this in practice, it's ok for this project here.
Be sure your program demonstrates good programming style. please include appropriate comments, identifier names, and indenting. Be sure to submit all source and project workspace files used in your solution....
Explanation / Answer
//***************************************************************************************************** import javax.swing.JOptionPane; public class PasswordTester { public static void main(String[] args) { String password = JOptionPane.showInputDialog("Enter Password: "); PassWord myPass = new PassWord(password); myPass.checkPassword(); } } //***************************************************************************************************** import javax.swing.JOptionPane; public class PassWord { private String password; private int count; /** * Creates a password entered by the user * @param aPassword the password */ public PassWord(String aPassword) { this.password = aPassword; } /** * Verifies whether the password meets the correct criteria of * Between 6 and 10 characters long * Containing at least one letter * Containing at least one digit */ public void checkPassword() { int countUpperCase = 0; //counts the number of uppercase letters in the password for (char c : password.toCharArray()) { if (Character.isLetter(c) && Character.isUpperCase(c)) { countUpperCase++; } } //loops until the correct password is keyed in while ((password.length() < 6) || (password.length() > 10) || (countUpperCase > 1) || (checkDigit(password) > 1) || (checkDigit(password) < 1)) { JOptionPane.showMessageDialog(null, " Not a good password "); password = JOptionPane.showInputDialog("Enter Password: "); } JOptionPane.showMessageDialog(null, "Password is 'GOOD' ReEnter again"); //call the matchPasswords method matchPasswords(password); } /** * Checks the if the password has more or no numbers * @param passString the password * @return the number of digits in the password */ private int checkDigit(String passString) { // Create an array to store the values for the counts int[] array = new int[10]; // Use a nested for loop to count the occurences for (int i = 0; i