Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package MidExamPrep.second;
- import java.sql.SQLOutput;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- import java.util.Scanner;
- import java.util.stream.Collectors;
- public class FriendlistMaintenance {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String[] some = scanner.nextLine().split(", ");
- ArrayList<String> friendList = new ArrayList<>(Arrays.asList(some));
- int blackListedNames = 0;
- int lostNames = 0;
- String input = scanner.nextLine();
- while (!input.equals("Report")) {
- String[] tokens = input.split(" ");
- String command = tokens[0];
- switch (command) {
- case "Blacklist":
- String blacklist = tokens[1];
- if (friendList.contains(blacklist)) {
- int getIndex = friendList.indexOf(blacklist);
- friendList.set(getIndex, "Blacklisted");
- System.out.println(String.format("%s was blacklisted.", blacklist));
- blackListedNames++;
- } else {
- System.out.println(String.format("%s was not found.", blacklist));
- }
- break;
- case "Error": {
- int index = Integer.parseInt(tokens[1]);
- String names = friendList.get(index);
- if (!friendList.get(index).equals("Blacklisted") && !friendList.equals("Lost")) {
- lostNames++;
- System.out.println(String.format("%s was lost due to an error.", names));
- friendList.set(index, "Lost");
- }
- }
- break;
- case "Change": {
- int index = Integer.parseInt(tokens[1]);
- String newName = tokens[2];
- String newIndex = friendList.get(index);
- String oldName = friendList.get(index);
- if (isValidIndex(friendList, index)) {
- friendList.set(index, newName);
- System.out.println(String.format("%s changed his username to %s."
- , oldName, newName));
- }
- break;
- }
- }
- input = scanner.nextLine();
- }
- System.out.println(String.format("Blacklisted names: %d", blackListedNames));
- System.out.println(String.format("Lost names: %d", lostNames));
- for (String friend : friendList) {
- System.out.print(friend + " ");
- }
- }
- private static boolean isValidIndex(ArrayList<String> friends, int index) {
- return 0 <= index && index < friends.size();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement