Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package PodgotovkaZaIzpit;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- public class FriendListMaintenance {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String[] friends = scanner.nextLine().split(", ");
- List<String> friendList = new ArrayList<>();
- for (String friend : friends) {
- friendList.add(friend);
- }
- int blacklistedCount = 0;
- int lostCount = 0;
- String input = scanner.nextLine();
- while (!input.equals("Report")) {
- String[] command = input.split(" ");
- String action = command[0];
- switch (action) {
- case "Blacklist":
- String nameToBlacklist = command[1];
- if (friendList.contains(nameToBlacklist)) {
- friendList.set(friendList.indexOf(nameToBlacklist), "Blacklisted");
- System.out.println(nameToBlacklist + " was blacklisted.");
- blacklistedCount++;
- } else {
- System.out.println(nameToBlacklist + " was not found.");
- }
- break;
- case "Error":
- int index = Integer.parseInt(command[1]);
- if (isValidIndex(index, friendList) && !friendList.get(index).equals("Blacklisted") && !friendList.get(index).equals("Lost")) {
- String lostName = friendList.get(index);
- friendList.set(index, "Lost");
- System.out.println(lostName + " was lost due to an error.");
- lostCount++;
- }
- break;
- case "Change":
- int changeIndex = Integer.parseInt(command[1]);
- if (isValidIndex(changeIndex, friendList)) {
- String newName = command[2];
- String currentName = friendList.get(changeIndex);
- friendList.set(changeIndex, newName);
- System.out.println(currentName + " changed his username to " + newName + ".");
- }
- break;
- }
- input = scanner.nextLine();
- }
- System.out.println("Blacklisted names: " + blacklistedCount);
- System.out.println("Lost names: " + lostCount);
- for (String friend : friendList) {
- System.out.print(friend + " ");
- }
- }
- private static boolean isValidIndex(int index, List<String> friendList) {
- return index >= 0 && index < friendList.size();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement