In: Computer Science
I have attempted to implement some changes to my source code and was unable to succefully implement there are (two) changes. I am in the learning process so comments help in the learning curve so if possible leave comments.
PLEASE DONT CHANGE ANY OF THE SOURCE CODE - unless needed to implement the changes.
* Change the program so that the user can choose to either check if a password is valid or have the program randomly generate a password that is valid.
* Add the functionality to generate a valid password, display this password and end the program.
SOURCE CODE PROVIDED BELOW
import java.util.Scanner;
public class PasswordChecker {
public static void main(String[] args) {
Scanner keyboard = new
Scanner(System.in);
String password;
int uppers = 0;
int lowers = 0;
int numbers = 0;
int special = 0;
int length = 0;
System.out.println("Enter your
password: ");
System.out.println("Rules:
");
System.out.println("* 9 to 30
characters in length");
System.out.println("* contain at
least one uppercase letter (A - Z)");
System.out.println("* contain at
least one lowercase letter (a - z)");
System.out.println("* contain at
least one number digit (0 - 9)");
System.out.println("* contain at
least one special character (# @ $ % + = )");
password =
keyboard.nextLine();
length = password.length();
if ((length < 9) || (length >
30)) {
System.out.println("Please re-enter password, keep in mind more
than 9 char and less than 30");
}
else
{
for(int i = 0; i
< length; i++) {
if(Character.isUpperCase(password.charAt(i)))
uppers++;
else if
(Character.isLowerCase(password.charAt(i)))
lowers++;
else if
(Character.isDigit(password.charAt(i)))
numbers++;
else if
(specialCompare(password.charAt(i)))
special++;
}
}
if((uppers == 0) || (lowers == 0)
|| (numbers == 0) || (special == 0))
System.out.println("Password is missing one of the
requirements");
else
System.out.println("Good password");
}//End main
static boolean specialCompare(char a) {
switch(a) {
case '@':
case '#':
case '$':
case '%':
case '+':
case '=':
return
true;
}
return false;
}//End specialCompare
}//End class
//All the code in bold is the code added by me
import java.util.*;
public class PasswordChecker
{
public static void main (String[]args)
{
Scanner keyboard = new Scanner
(System.in);
String password;
int uppers = 0;
int lowers = 0;
int numbers = 0;
int special = 0;
int length = 0;
int choice = 0;
System.out.println ("press 1
to enter your own password.");
System.out.println ("press 2 to get an auto generated
password.");
choice = keyboard.nextInt ();
if (choice == 1)
{
System.out.println
("Enter your password: ");
System.out.println ("Rules: ");
System.out.println ("* 9 to 30 characters in length");
System.out.println ("* contain at least one uppercase letter (A -
Z)");
System.out.println ("* contain at least one lowercase letter (a -
z)");
System.out.println ("* contain at least one number digit (0 -
9)");
System.out.println ("* contain at least one special character (# @
$ % + = )");
password = keyboard.nextLine ();
length = password.length ();
if ((length < 9) || (length > 30)) System.out.println("Please
re-enter password, keep in mind more than 9 char and less than
30");
else
{
for (int i = 0; i < length;
i++)
{
if (Character.isUpperCase (password.charAt (i))) uppers++;
else if (Character.isLowerCase (password.charAt (i))) lowers++;
else if (Character.isDigit (password.charAt (i))) numbers++;
else if (specialCompare (password.charAt (i))) special++;
}
}
if ((uppers == 0) || (lowers == 0) ||
(numbers == 0) || (special == 0)) System.out.println ("Password is
missing one of the requirements");
else System.out.println ("Good password");
} //End is x==1
else if (choice == 2)
{
//If x==2 we will auto generate pass word
password = "";
int x = 0;
int count = 0;
length = getRandomInRange (9,
30); // here we randomly decide length of our password
for (int i = 0; i < length; i++) // In this loop we generate
each and every character of password randomly
{
if (count < 4 &&
(length - i - 1) == (4 - count))
{
//this if ensures if each of number, special character, lowercase and uppercase is taken atleast once
if (uppers == 0)
{
password += getUpperCase
();
count++;
uppers++;
}
else if (lowers == 0)
{
password += getLowerCase
();
count++;
lowers++;
}
else if (numbers == 0)
{
password += getNumber
();
count++;
numbers++;
}
else
{
password +=
getSpecialCharacter ();
count++;
special++;
}
continue;
}
x = getRandomInRange (1, 4);
//Below if else ladder
decides which of the four available cases to use as our next
character in password.
if (x == 1) //for uppercase
{
password += getUpperCase
();
if (uppers == 0) count++;
uppers++;
}
else if (x == 2) //for lowercase
{
password += getLowerCase
();
if (lowers == 0) count++;
lowers++;
}
else if (x == 3) //for number
{
password += getNumber
();
if (numbers == 0) count++;
numbers++;
}
else //for special character
{
password +=
getSpecialCharacter ();
if (special == 0) count++;
special++;
}
}
System.out.println(password);
} // End if x == 2
else System.out.println (choice + " is an invalid choice.
Exiting.....");
} //End Main
public static char getUpperCase () //this function will
get a random uppercase character
{
return (char) getRandomInRange (65, 90);
}
public static char getLowerCase () //this function will
get a random lowercase character
{
return (char) getRandomInRange (97, 122);
}
public static int getNumber () //this function will get
a random number between 0 - 9
{
return getRandomInRange (0, 9);
}
public static char getSpecialCharacter () //this
function will get a random special character
{
int x = getRandomInRange (1,
6);
switch (x)
{
case 1:
return '@';
case 2:
return '#';
case 3:
return '$';
case 4:
return '%';
case 5:
return '+';
case 6:
return '=';
}
return '@';
}
public static int getRandomInRange (int min, int max)
//this function will generate a random in the given range.
{
Random r = new Random
();
return r.nextInt ((max - min) + 1) + min;
}
static boolean specialCompare (char a)
{
switch (a)
{
case '@':
case '#':
case '$':
case '%':
case '+':
case '=':
return true;
}
return false;
}
//End specialCompare
}
OUTPUT:-