Advertisement
Didart

Password Validator

Dec 9th, 2021
467
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.09 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4.  
  5. public class PasswordValidator {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         StringBuilder password = new StringBuilder(scanner.nextLine());
  10.         String input = scanner.nextLine();
  11.  
  12.         while (!input.equals("Complete")) {
  13.             String[] data = input.split(" ");
  14.             String command = data[0];
  15.  
  16.             switch (command) {
  17.                 case "Make":
  18.                     String caps = data[1];
  19.                     int index = Integer.parseInt(data[2]);
  20.  
  21.                     if (caps.equals("Upper")) {
  22.                         char charToReplace = password.charAt(index);
  23.                         charToReplace = Character.toUpperCase(charToReplace);
  24.                         password.setCharAt(index, charToReplace);
  25.                         System.out.println(password);
  26.                     } else if (caps.equals("Lower")) {
  27.                         char charToReplace = password.charAt(index);
  28.                         charToReplace = Character.toLowerCase(charToReplace);
  29.                         password.setCharAt(index, charToReplace);
  30.                         System.out.println(password);
  31.                     }
  32.                     break;
  33.                 case "Insert":
  34.                     int indexToInsert = Integer.parseInt(data[1]);
  35.                     String symbolToInsert = data[2];
  36.                     char charInsert = symbolToInsert.charAt(0);
  37.                     int lastIndex = password.length() - 1;
  38.                     if (indexToInsert < 0 || indexToInsert > lastIndex) {
  39.                         break;
  40.  
  41.                     } else {
  42.                         password.insert(indexToInsert, charInsert);
  43.                         System.out.println(password);
  44.                     }
  45.                     break;
  46.                 case "Replace":
  47.                     char toReplace = data[1].charAt(0);
  48.                     String replace = data[1];
  49.                     int value = Integer.parseInt(data[2]);
  50.                     int asciiSymbol = toReplace;
  51.                     int sum = asciiSymbol + value;
  52.                     char newReplace = (char) sum;
  53.  
  54.                     if (password.toString().contains(replace)) {
  55.                         while (password.toString().contains(replace)) {
  56.                             String text = password.toString().replace(replace, String.valueOf(newReplace));
  57.                             password.replace(0, password.length(), text);
  58.                             System.out.println(password);
  59.                         }
  60.                     } else {
  61.                         break;
  62.                     }
  63.                     break;
  64.                 case "Validation":
  65.                     String regex1 = "\\w+";
  66.                     Pattern pattern1 = Pattern.compile(regex1);
  67.                     Matcher matcher1 = pattern1.matcher(password);
  68.  
  69.                 //    String regex2 = "\\d+";
  70.                     Pattern pattern2 = Pattern.compile(regex1);
  71.                     Matcher matcher2 = pattern2.matcher(password);
  72.  
  73.                     if (password.length() < 8) {
  74.                         System.out.println("Password must be at least 8 characters long!");
  75.                     }  if (!matcher1.find()) {
  76.                     System.out.println("Password must consist only of letters, digits and _!");
  77.  
  78.                 }  if (password.toString().chars().noneMatch(Character::isUpperCase)) {
  79.                     System.out.println("Password must consist at least one uppercase letter!");
  80.                 }  if (password.toString().chars().noneMatch(Character::isLowerCase)) {
  81.                     System.out.println("Password must consist at least one lowercase letter!");
  82.                 }  if (!matcher2.find()) {
  83.                     System.out.println("Password must consist at least one digit!");
  84.                 }
  85.                     break;
  86.  
  87.                 default:
  88.                     break;
  89.             }
  90.             input = scanner.nextLine();
  91.         }
  92.     }
  93. }
  94.  
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement