0% found this document useful (0 votes)
29 views6 pages

Password Services

PasswordServices

Uploaded by

mohammedsaiaf6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views6 pages

Password Services

PasswordServices

Uploaded by

mohammedsaiaf6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Code:

package com.mycompany.passwordservices;

import java.util.Random;

import java.util.Scanner;

public class PasswordServices {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int choice;

System.out.println("Welcome to our passwoord services project");

do {

System.out.println("\nTo generate passwords, please enter 1.");

System.out.println("To check the strength of your password, please enter 2.");

System.out.println("To generate passwords and check their strengths, please enter


3.");

System.out.println("To exit the program. please enter 0");

choice = input.nextInt();

switch (choice) {

case 1:

System.out.println("Enter the password length");

int length = input.nextInt();

if (length <= 0 || length != (int) length) {

System.out.println("Length should be a positive integer");


} else {

String[] passwords = generatePasswords(length);

printPasswords(passwords);

break;

case 2:

System.out.println("Enter your password");

String password = input.next();

int score = checkStrength(password);

printStrength(score);

break;

case 3:

System.out.println("Enter the password length");

int len = input.nextInt();

if (len <= 0 || len != (int) len) {

System.out.println("Length should be a positive integer");

} else {

String[] generatedPasswords = generatePasswords(len);

int[] scores = new int[generatedPasswords.length];

for (int i = 0; i < generatedPasswords.length; i++) {

scores[i] = checkStrength(generatedPasswords[i]);

printPasswords(generatedPasswords, scores);

break;

case 0:

System.out.println("Message: Program ended");


break;

default:

System.out.println("Error: Invalid entry");

} while (choice != 0);

input.close();

public static String[] generatePasswords(double len) {

Random random = new Random();

String characters =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$
%^&*()_+";

String[] passwords = new String[3];

for (int i = 0; i < 3; i++) {

String[] createPassword = new String[(int)len];

for (int j = 0; j < len; j++) {

int index = random.nextInt(characters.length());

createPassword[j] = Character.toString(characters.charAt(index));

passwords[i] = String.join("", createPassword) ;

return passwords;

public static void printPasswords(String[] passwords) {

System.out.println("Here are a few options:");

for (int i = 0; i < passwords.length; i++) {

System.out.println(passwords[i]);
}

public static void printPasswords(String[] passwords, int[] scores) {

System.out.println("Here are a few options:");

for (int i = 0; i < passwords.length; i++) {

System.out.print(passwords[i] + " ");

printStrength(scores[i]);

public static int checkStrength(String s) {

int score = 0;

char[] c = s.toCharArray();

// check the password contain the capetal letter

for (int i = 0; i < c.length; i++){

if (Character.isUpperCase(c[i])){

score++;

break;

// check the password contain the Small letter

for (int i = 0; i < c.length; i++){

if (Character.isLowerCase(c[i])){

score++;

break;

}
// check the password contain the number

for (int i = 0; i < c.length; i++){

if (Character.isDigit(c[i])){

score++;

break;

// check the password contain the specific Chars

String specificChars = "!@#$%^&*()_+";

for (int i = 0; i < c.length; i++){

if (specificChars.indexOf(c[i]) != -1){

score++;

break;

// check the password length bigger than or equal 8

if (s.length() >= 8) {

score++;

return score;

public static void printStrength(int score) {

switch (score) {

case 1:

System.out.println("This is a weak password, you should find a new one !");

break;

case 2:
System.out.println("This is a weak password, you should find a new one !");

break;

case 3:

System.out.println("This is a medium password, try making it better");

break;

case 4:

System.out.println("This is a good password, but you can still do better");

break;

case 5:

System.out.println("This is a very good password!");

break;

default:

System.out.println("password is not correct");

break;

You might also like