Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Scanner;
- public class FriendListMaintenance {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String[] names = scanner.nextLine().split(", ");
- ArrayList<String> userNames = new ArrayList<>(Arrays.asList(names));
- int blacklistedCounter = 0;
- int lostCounter = 0;
- String input = scanner.nextLine();
- while (!input.equals("Report")) {
- String[] tokens = input.split(" ");
- String command = tokens[0];
- switch (command) {
- case "Blacklist":
- String nameToContain = tokens[1];
- for (int i = 0; i < userNames.size(); i++) {
- if (userNames.get(i).equals(nameToContain)) {
- System.out.printf("%s was blacklisted.%n", nameToContain);
- userNames.set(i, "Blacklisted");
- blacklistedCounter++;
- break;
- }
- }
- for (String userName : userNames) {
- if (!userName.equals(nameToContain)) {
- break;
- } else {
- System.out.printf("%s was not found.%n", nameToContain);
- }
- }
- break;
- case "Error": {
- int index = Integer.parseInt(tokens[1]);
- if (index < 0 || index >= userNames.size()) {
- break;
- }
- if (!userNames.get(index).equals("Blacklisted") && !userNames.get(index).equals("Lost")) {
- System.out.printf("%s was lost due to an error.%n", userNames.get(index));
- userNames.set(index, "Lost");
- lostCounter++;
- }
- break;
- }
- case "Change":
- int index = Integer.parseInt(tokens[1]);
- String newName = tokens[2];
- if (index < 0 || index >= userNames.size()) {
- break;
- } else {
- for (int i = 0; i < userNames.size(); i++) {
- if (i == index) {
- System.out.printf("%s changed his username to %s.%n", userNames.get(i), newName);
- break;
- }
- }
- userNames.set(index, newName);
- }
- break;
- }
- input = scanner.nextLine();
- }
- System.out.println(String.format("Blacklisted names: %d", blacklistedCounter));
- System.out.println(String.format("Lost names: %d", lostCounter));
- System.out.println(String.join(" ", userNames));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement