|
| 1 | +import java.util.Objects; |
| 2 | +import java.util.Scanner; |
| 3 | + |
| 4 | +public class Generator { |
| 5 | + Alphabet alphabet; |
| 6 | + public static Scanner keyboard; |
| 7 | + |
| 8 | + public Generator(Scanner scanner) { |
| 9 | + keyboard = scanner; |
| 10 | + } |
| 11 | + |
| 12 | + public Generator(boolean IncludeUpper, boolean IncludeLower, boolean IncludeNum, boolean IncludeSym) { |
| 13 | + alphabet = new Alphabet(IncludeUpper, IncludeLower, IncludeNum, IncludeSym); |
| 14 | + } |
| 15 | + |
| 16 | + public void mainLoop() { |
| 17 | + System.out.println("Welcome to Ziz Password Services :)"); |
| 18 | + printMenu(); |
| 19 | + |
| 20 | + String userOption = "-1"; |
| 21 | + |
| 22 | + while (!userOption.equals("4")) { |
| 23 | + |
| 24 | + userOption = keyboard.next(); |
| 25 | + |
| 26 | + switch (userOption) { |
| 27 | + case "1" -> { |
| 28 | + requestPassword(); |
| 29 | + printMenu(); |
| 30 | + } |
| 31 | + case "2" -> { |
| 32 | + checkPassword(); |
| 33 | + printMenu(); |
| 34 | + } |
| 35 | + case "3" -> { |
| 36 | + printUsefulInfo(); |
| 37 | + printMenu(); |
| 38 | + } |
| 39 | + case "4" -> printQuitMessage(); |
| 40 | + default -> { |
| 41 | + System.out.println(); |
| 42 | + System.out.println("Kindly select one of the available commands"); |
| 43 | + printMenu(); |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + private Password GeneratePassword(int length) { |
| 50 | + final StringBuilder pass = new StringBuilder(""); |
| 51 | + |
| 52 | + final int alphabetLength = alphabet.getAlphabet().length(); |
| 53 | + |
| 54 | + int max = alphabetLength - 1; |
| 55 | + int min = 0; |
| 56 | + int range = max - min + 1; |
| 57 | + |
| 58 | + for (int i = 0; i < length; i++) { |
| 59 | + int index = (int) (Math.random() * range) + min; |
| 60 | + pass.append(alphabet.getAlphabet().charAt(index)); |
| 61 | + } |
| 62 | + |
| 63 | + return new Password(pass.toString()); |
| 64 | + } |
| 65 | + |
| 66 | + private void printUsefulInfo() { |
| 67 | + System.out.println(); |
| 68 | + System.out.println("Use a minimum password length of 8 or more characters if permitted"); |
| 69 | + System.out.println("Include lowercase and uppercase alphabetic characters, numbers and symbols if permitted"); |
| 70 | + System.out.println("Generate passwords randomly where feasible"); |
| 71 | + System.out.println("Avoid using the same password twice (e.g., across multiple user accounts and/or software systems)"); |
| 72 | + System.out.println("Avoid character repetition, keyboard patterns, dictionary words, letter or number sequences," + |
| 73 | + "\nusernames, relative or pet names, romantic links (current or past) " + |
| 74 | + "and biographical information (e.g., ID numbers, ancestors' names or dates)."); |
| 75 | + System.out.println("Avoid using information that the user's colleagues and/or " + |
| 76 | + "acquaintances might know to be associated with the user"); |
| 77 | + System.out.println("Do not use passwords which consist wholly of any simple combination of the aforementioned weak components"); |
| 78 | + } |
| 79 | + |
| 80 | + private void requestPassword() { |
| 81 | + boolean IncludeUpper = false; |
| 82 | + boolean IncludeLower = false; |
| 83 | + boolean IncludeNum = false; |
| 84 | + boolean IncludeSym = false; |
| 85 | + |
| 86 | + boolean correctParams = false; |
| 87 | + |
| 88 | + System.out.println(); |
| 89 | + System.out.println("Hello, welcome to the Password Generator :) answer" |
| 90 | + + " the following questions by Yes or No \n"); |
| 91 | + |
| 92 | + do { |
| 93 | + System.out.println("Do you want Lowercase letters \"abcd...\" to be used? "); |
| 94 | + String input = keyboard.nextLine(); |
| 95 | + |
| 96 | + if (isInclude(input)) IncludeLower = true; |
| 97 | + |
| 98 | + System.out.println("Do you want Uppercase letters \"ABCD...\" to be used? "); |
| 99 | + input = keyboard.nextLine(); |
| 100 | + |
| 101 | + if (isInclude(input)) IncludeUpper = true; |
| 102 | + |
| 103 | + System.out.println("Do you want Numbers \"1234...\" to be used? "); |
| 104 | + input = keyboard.nextLine(); |
| 105 | + |
| 106 | + if (isInclude(input)) IncludeNum = true; |
| 107 | + |
| 108 | + System.out.println("Do you want Symbols \"!@#$...\" to be used? "); |
| 109 | + input = keyboard.nextLine(); |
| 110 | + |
| 111 | + if (isInclude(input)) IncludeSym = true; |
| 112 | + |
| 113 | + //No Pool Selected |
| 114 | + if (!IncludeUpper && !IncludeLower && !IncludeNum && !IncludeSym) { |
| 115 | + System.out.println("You have selected no characters to generate your " + |
| 116 | + "password at least one of your answers should be Yes"); |
| 117 | + correctParams = true; |
| 118 | + } |
| 119 | + |
| 120 | + System.out.println("Great! Now enter the length of the password"); |
| 121 | + int length = keyboard.nextInt(); |
| 122 | + |
| 123 | + final Generator generator = new Generator(IncludeUpper, IncludeLower, IncludeNum, IncludeSym); |
| 124 | + final Password password = generator.GeneratePassword(length); |
| 125 | + |
| 126 | + System.err.println("Your generated password -> " + password); |
| 127 | + |
| 128 | + } while (correctParams); |
| 129 | + } |
| 130 | + |
| 131 | + private boolean isInclude(String Input) { |
| 132 | + if (Input.equalsIgnoreCase("yes")) { |
| 133 | + return true; |
| 134 | + } else { |
| 135 | + if (!Input.equalsIgnoreCase("no")) { |
| 136 | + PasswordRequestError(); |
| 137 | + } |
| 138 | + return false; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + private void PasswordRequestError() { |
| 143 | + System.out.println("You have entered something incorrect let's go over it again \n"); |
| 144 | + } |
| 145 | + |
| 146 | + private void checkPassword() { |
| 147 | + String input; |
| 148 | + final Scanner in = new Scanner(System.in); |
| 149 | + |
| 150 | + System.out.print("\nEnter your password:"); |
| 151 | + input = in.nextLine(); |
| 152 | + |
| 153 | + final Password p = new Password(input); |
| 154 | + |
| 155 | + System.out.println(p.calculateScore()); |
| 156 | + |
| 157 | + in.close(); |
| 158 | + } |
| 159 | + |
| 160 | + private void printMenu() { |
| 161 | + System.out.println(); |
| 162 | + System.out.println("Enter 1 - Password Generator"); |
| 163 | + System.out.println("Enter 2 - Password Strength Check"); |
| 164 | + System.out.println("Enter 3 - Useful Information"); |
| 165 | + System.out.println("Enter 4 - Quit"); |
| 166 | + System.out.print("Choice:"); |
| 167 | + } |
| 168 | + |
| 169 | + private void printQuitMessage() { |
| 170 | + System.out.println("Closing the program bye bye!"); |
| 171 | + } |
| 172 | +} |
0 commit comments