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> friends = new ArrayList<>(Arrays.asList(names));
- int blacklistCount = 0;
- int lostNamesCount = 0;
- String command = scanner.nextLine();
- while (!"Report".equals(command)) {
- String[] tokens = command.split(" ");
- String type = tokens[0];
- switch (type) {
- case "Blacklist": {
- String name = tokens[1];
- if (friends.contains(name)) {
- int indexName = friends.indexOf(name);
- friends.set(indexName, "Blacklisted");
- System.out.println(String.format("%s was blacklisted.", name));
- blacklistCount++;
- } else {
- System.out.println(String.format("%s was not found.", name));
- }
- break;
- }
- case "Error": {
- int index = Integer.parseInt(tokens[1]);
- String currName = friends.get(index);
- if (!currName.equals("Blacklisted") && !currName.equals("Lost")) {
- friends.set(index, "Lost");
- lostNamesCount++;
- System.out.println(String.format("%s was lost due to an error.", currName));
- }
- break;
- }
- case "Change": {
- int index = Integer.parseInt(tokens[1]);
- String newName = tokens[2];
- if (isValidIndex(friends, index)) {
- String currName = friends.get(index);
- friends.set(index, newName);
- System.out.println(String.format("%s changed his username to %s."
- , currName, newName));
- }
- break;
- }
- }
- command = scanner.nextLine();
- }
- System.out.println(String.format("Blacklisted names: %d", blacklistCount));
- System.out.println(String.format("Lost names: %d", lostNamesCount));
- for (String friend : friends) {
- System.out.print(friend + " ");
- }
- }
- private static boolean isValidIndex(ArrayList<String> friends, int index) {
- return 0 <= index && index < friends.size();
- }
- }
Add Comment
Please, Sign In to add comment