Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package MidExam_8;
- import java.util.Arrays;
- import java.util.List;
- import java.util.Scanner;
- import java.util.stream.Collectors;
- public class Exercise_02 {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- List<String> names = Arrays.stream(scanner.nextLine()
- .split(", "))
- .collect(Collectors.toList());
- String command = scanner.nextLine();
- int blacklisted = 0;
- int lostNames = 0;
- while (!command.equals("Report")) {
- String[] commandParts = command.split(" ");
- String commandName = commandParts[0];
- switch (commandName) {
- case "Blacklist":
- String name = commandParts[1];
- // int indexOfName = names.indexOf(name);
- if (names.contains(name)) {
- int indexOfName = names.indexOf(name);
- blacklisted++;
- names.set(indexOfName, "Blacklisted");
- System.out.printf("%s was blacklisted.%n", name);
- } else {
- System.out.printf("%s was not found.%n", name);
- }
- break;
- case "Error":
- int index = Integer.parseInt(commandParts[1]);
- if (index >= 0 && index < names.size()) {
- String nameAtTheIndex = names.get(index);
- if (!nameAtTheIndex.equals("Lost") && !nameAtTheIndex.equals("Blacklisted")) {
- names.set(index, "Lost");
- lostNames++;
- System.out.printf("%s was lost due to an error.%n", nameAtTheIndex);
- }
- }
- break;
- case "Change":
- int currentIndex = Integer.parseInt(commandParts[1]);
- if (currentIndex >= 0 && currentIndex < names.size()) {
- String newName = commandParts[2];
- String writtenName = names.get(currentIndex);
- names.set(currentIndex, newName);
- System.out.printf("%s changed his username to %s.%n", writtenName, newName);
- break;
- }
- }
- command = scanner.nextLine();
- }
- System.out.printf("Blacklisted names: %d%n", blacklisted);
- System.out.printf("Lost names: %d%n", lostNames);
- System.out.println(String.join(" ", names));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment