Advertisement
TUNVAN

Untitled

Apr 2nd, 2023
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.29 KB | None | 0 0
  1. package FinalExam;
  2.  
  3. import java.util.*;
  4.  
  5. public class Task1 {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         StringBuilder inputPassword = new StringBuilder(scanner.nextLine());
  10.  
  11.         String[] commands = scanner.nextLine().split("\\s+");
  12.  
  13.         while (!commands[0].equals("Complete")) {
  14.             switch (commands[0]) {
  15.                 case "Make":
  16.                     String upperOrLower = commands[1];
  17.                     int index = Integer.parseInt(commands[2]);
  18.  
  19.                     if (upperOrLower.equals("Upper")) {
  20.                         if (index >= 0 && index <= inputPassword.length()) {
  21.                             char tempUpper = inputPassword.charAt(index);
  22.                             char toMake = Character.toUpperCase(tempUpper);
  23.                             inputPassword.setCharAt(index, toMake);
  24.                             System.out.println(inputPassword);
  25.                         }
  26.                     } else {
  27.                         if (upperOrLower.equals("Lower")) {
  28.                             char tempLower = inputPassword.charAt(index);
  29.                             if (index >= 0 && index <= inputPassword.length()) {
  30.                                 char toMake = Character.toLowerCase(tempLower);
  31.                                 inputPassword.setCharAt(index, toMake);
  32.                                 System.out.println(inputPassword);
  33.                             }
  34.                         }
  35.                     }
  36.                     break;
  37.                 case "Insert":
  38.                     int indexToInsert = Integer.parseInt(commands[1]);
  39.                     String charToInsert = commands[2];
  40.                     if (indexToInsert >= 0 && indexToInsert <= inputPassword.length()) {
  41.                         inputPassword.insert(indexToInsert, charToInsert);
  42.                         System.out.println(inputPassword);
  43.                     }
  44.                     break;
  45.                 case "Replace":
  46.                     String charToReplace = commands[1];
  47.                     int value = Integer.parseInt(commands[2]);
  48.                     if (inputPassword.indexOf(charToReplace) > -1) {
  49.                         int charCode = charToReplace.charAt(0);
  50.                         int newValue = charCode + value;
  51.                         String newSymbol = String.valueOf((char) newValue);
  52.                         if (inputPassword.toString().contains(charToReplace)) {
  53.                             String tempString = inputPassword.toString().replace(charToReplace, newSymbol);
  54.                             inputPassword = new StringBuilder(tempString);
  55.                         }
  56.                         System.out.println(inputPassword);
  57.                     }
  58.                     break;
  59.                 case "Validation":
  60.                     if (inputPassword.length() < 8) {
  61.                         System.out.println("Password must be at least 8 characters long!");
  62.                     }
  63.                     boolean isLetterOrDigit = true;
  64.                     for (int i = 0; i < inputPassword.length(); i++) {
  65.                         char tempChar = inputPassword.charAt(i);
  66.                         if (!Character.isLetterOrDigit(tempChar) && tempChar != '_') {
  67.                             isLetterOrDigit = false;
  68.                             break;
  69.                         }
  70.                     }
  71.                     if (!isLetterOrDigit) {
  72.                         System.out.println("Password must consist only of letters, digits and _!");
  73.                     }
  74.  
  75.                     isLetterOrDigit = true;
  76.                     for (int i = 0; i < inputPassword.length(); i++) {
  77.                         char tempChar = inputPassword.charAt(i);
  78.                         if (Character.isUpperCase(tempChar)) {
  79.                             isLetterOrDigit = false;
  80.                             break;
  81.                         }
  82.                     }
  83.                     if (isLetterOrDigit) {
  84.                         System.out.println("Password must consist at least one uppercase letter!");
  85.                     }
  86.  
  87.                     isLetterOrDigit = true;
  88.  
  89.                     for (int i = 0; i < inputPassword.length(); i++) {
  90.                         char tempChar = inputPassword.charAt(i);
  91.                         if (Character.isLowerCase(tempChar)) {
  92.                             isLetterOrDigit = false;
  93.                             break;
  94.                         }
  95.                     }
  96.                     if (isLetterOrDigit) {
  97.                         System.out.println("Password must consist at least one lowercase letter!");
  98.                     }
  99.  
  100.                     isLetterOrDigit = true;
  101.  
  102.                     for (int i = 0; i < inputPassword.length(); i++) {
  103.                         char tempChar = inputPassword.charAt(i);
  104.                         if (Character.isDigit(tempChar)) {
  105.                             isLetterOrDigit = false;
  106.                             break;
  107.                         }
  108.                     }
  109.                     if (isLetterOrDigit) {
  110.                         System.out.println("Password must consist at least one digit!");
  111.                     }
  112.                     break;
  113.             }
  114.             commands = scanner.nextLine().split("\\s+");
  115.         }
  116.     }
  117. }
  118.  
Tags: Java
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement