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