OpKaTa

Untitled

Feb 9th, 2020
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class PaswwordValidator {
  4. public static void main(String[] args) {
  5. Scanner scan = new Scanner(System.in);
  6. String password = scan.nextLine();
  7. Boolean isCorectpass = getValidpass(password);
  8. if (isCorectpass) {
  9. System.out.println("Password is valid");
  10. }
  11. }
  12.  
  13. public static Boolean getValidpass(String password) {
  14. boolean isCoerect1 = getPaswwordLenght(password);
  15. boolean isCoerect2 = getCheckLettersOrDigits(password);
  16. boolean isCoerect3 = getCheckAtLeastTwoDigit(password);
  17. if (isCoerect1 && isCoerect2 && isCoerect3) {
  18. return true;
  19. }
  20. return false;
  21. }
  22. // Проверка дали има минимум 2 числа
  23. public static boolean getCheckAtLeastTwoDigit(String password) {
  24. int counter = 0;
  25. for (int i = 0; i < password.length(); i++) {
  26. char symbol = password.charAt(i);
  27. int symbolInDigit = (int) symbol;
  28.  
  29. if (symbolInDigit >= 48 && symbolInDigit <= 57) {
  30. counter++;
  31. }
  32. if (password.length() - 1 == i) {
  33. if (counter < 2) {
  34. System.out.println("Password must have at least 2 digits");
  35. return false;
  36. }
  37. }
  38. }
  39. return true;
  40. }
  41. //Проверка дали има само букви и цифри
  42. public static boolean getCheckLettersOrDigits(String password) {
  43. for (int i = 0; i < password.length(); i++) {
  44. char symbol = password.charAt(i);
  45. if (!Character.isLetterOrDigit(symbol)) {
  46. System.out.println("Password must consist only of letters and digits");
  47. return false;
  48. }
  49. }
  50. return true;
  51.  
  52. }
  53. // Проверка дали дължината на паролата е от 6 до 10 симовола включително
  54. public static boolean getPaswwordLenght(String password) {
  55. if (password.length() >= 6 && password.length() <= 10) {
  56. return true;
  57. } else {
  58. System.out.println("Password must be between 6 and 10 characters");
  59. return false;
  60. }
  61. }
  62.  
  63. }
Advertisement
Add Comment
Please, Sign In to add comment