Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package FinalExam;
- import java.util.*;
- public class Task1 {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- StringBuilder inputPassword = new StringBuilder(scanner.nextLine());
- String[] commands = scanner.nextLine().split("\\s+");
- while (!commands[0].equals("Complete")) {
- switch (commands[0]) {
- case "Make":
- String upperOrLower = commands[1];
- int index = Integer.parseInt(commands[2]);
- if (upperOrLower.equals("Upper")) {
- if (index >= 0 && index <= inputPassword.length()) {
- char tempUpper = inputPassword.charAt(index);
- char toMake = Character.toUpperCase(tempUpper);
- inputPassword.setCharAt(index, toMake);
- System.out.println(inputPassword);
- }
- } else {
- if (upperOrLower.equals("Lower")) {
- char tempLower = inputPassword.charAt(index);
- if (index >= 0 && index <= inputPassword.length()) {
- char toMake = Character.toLowerCase(tempLower);
- inputPassword.setCharAt(index, toMake);
- System.out.println(inputPassword);
- }
- }
- }
- break;
- case "Insert":
- int indexToInsert = Integer.parseInt(commands[1]);
- String charToInsert = commands[2];
- if (indexToInsert >= 0 && indexToInsert <= inputPassword.length()) {
- inputPassword.insert(indexToInsert, charToInsert);
- System.out.println(inputPassword);
- }
- break;
- case "Replace":
- String charToReplace = commands[1];
- int value = Integer.parseInt(commands[2]);
- if (inputPassword.indexOf(charToReplace) > -1) {
- int charCode = charToReplace.charAt(0);
- int newValue = charCode + value;
- String newSymbol = String.valueOf((char) newValue);
- if (inputPassword.toString().contains(charToReplace)) {
- String tempString = inputPassword.toString().replace(charToReplace, newSymbol);
- inputPassword = new StringBuilder(tempString);
- }
- System.out.println(inputPassword);
- }
- break;
- case "Validation":
- if (inputPassword.length() < 8) {
- System.out.println("Password must be at least 8 characters long!");
- }
- boolean isLetterOrDigit = true;
- for (int i = 0; i < inputPassword.length(); i++) {
- char tempChar = inputPassword.charAt(i);
- if (!Character.isLetterOrDigit(tempChar) && tempChar != '_') {
- isLetterOrDigit = false;
- break;
- }
- }
- if (!isLetterOrDigit) {
- System.out.println("Password must consist only of letters, digits and _!");
- }
- isLetterOrDigit = true;
- for (int i = 0; i < inputPassword.length(); i++) {
- char tempChar = inputPassword.charAt(i);
- if (Character.isUpperCase(tempChar)) {
- isLetterOrDigit = false;
- break;
- }
- }
- if (isLetterOrDigit) {
- System.out.println("Password must consist at least one uppercase letter!");
- }
- isLetterOrDigit = true;
- for (int i = 0; i < inputPassword.length(); i++) {
- char tempChar = inputPassword.charAt(i);
- if (Character.isLowerCase(tempChar)) {
- isLetterOrDigit = false;
- break;
- }
- }
- if (isLetterOrDigit) {
- System.out.println("Password must consist at least one lowercase letter!");
- }
- isLetterOrDigit = true;
- for (int i = 0; i < inputPassword.length(); i++) {
- char tempChar = inputPassword.charAt(i);
- if (Character.isDigit(tempChar)) {
- isLetterOrDigit = false;
- break;
- }
- }
- if (isLetterOrDigit) {
- System.out.println("Password must consist at least one digit!");
- }
- break;
- }
- commands = scanner.nextLine().split("\\s+");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement