desislava_topuzakova

04. Password Validator

Oct 8th, 2021
1,366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.36 KB | None | 0 0
  1. package Methods_Exercise;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class PasswordValidator_04 {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         String password = scanner.nextLine();
  9.         //проверка за дължина -> [6; 10]
  10.         boolean isPasswordValidLength = isValidLength(password); //true -> валидна; false -> не е валидна
  11.         if (!isPasswordValidLength) {
  12.             System.out.println("Password must be between 6 and 10 characters");
  13.         }
  14.         //проверка за съдържанието -> само букви и цифри
  15.         boolean isValidPasswordContent = isValidContent(password); //true (валидна), false (невалидна)
  16.         if (!isValidPasswordContent) {
  17.             System.out.println("Password must consist only of letters and digits");
  18.         }
  19.  
  20.         //проверка за брой на цифирите -> минимум 2
  21.         boolean isValidPasswordCountDigits = isValidCountDigits(password);  //true (валидна), false (невалидна)
  22.         if (!isValidPasswordCountDigits) {
  23.             System.out.println("Password must have at least 2 digits");
  24.         }
  25.  
  26.         //валидна: isPasswordValidLength = true и isValidPasswordContent = true и isValidPasswordCountDigits = true
  27.         if (isPasswordValidLength && isValidPasswordContent && isValidPasswordCountDigits) {
  28.             System.out.println("Password is valid");
  29.         }
  30.  
  31.     }
  32.  
  33.     //метод за валидиране на дължината -> true (валидна); false (невалидна)
  34.     private static boolean isValidLength(String password) {
  35.         //валидна -> дължина >= 6 и дължина <= 10
  36.         /*if (password.length() >= 6 && password.length() <= 10) {
  37.             return true;
  38.         } else {
  39.             return false;
  40.         }*/
  41.         return password.length() >= 6 && password.length() <= 10;
  42.     }
  43.  
  44.     //метод за валидиране на съдържанието -> true (валидна), false (невалидна)
  45.     private static boolean isValidContent(String password) {
  46.         //всички символи да са буква или цифра -> "T@st1234"
  47.         //обхождаме всички символи
  48.         //ако даден символ не е буква или цифра -> false
  49.         for (char symbol : password.toCharArray()) {
  50.             if (!Character.isLetterOrDigit(symbol)) {
  51.                 return false;
  52.             }
  53.         }
  54.         return true;
  55.     }
  56.  
  57.     //метод за валидиране на брой на цифрите -> валидна парола: брой >= 2
  58.     private static boolean isValidCountDigits(String password) {
  59.         //true -> count >= 2
  60.         //false -> count < 2
  61.         int count = 0; //брой на цифрите
  62.         //обхождам всеки един символ -> проверка дали е цифра -> увеличавам броя
  63.        for (char symbol : password.toCharArray()) {
  64.             if (Character.isDigit(symbol)) {
  65.                 count++;
  66.             }
  67.         }
  68.         //знам броя на цифрите
  69.         /*if (count >= 2) {
  70.             return true;
  71.         } else {
  72.             return false;
  73.         }*/
  74.         return count >= 2;
  75.     }
  76.  
  77. }
Advertisement
Add Comment
Please, Sign In to add comment