Advertisement
Ligh7_of_H3av3n

Problem 2 - Friend List Maintenance

Feb 18th, 2024
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.72 KB | None | 0 0
  1. package PodgotovkaZaIzpit;
  2.  
  3.  
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import java.util.Scanner;
  7.  
  8. public class FriendListMaintenance {
  9.     public static void main(String[] args) {
  10.         Scanner scanner = new Scanner(System.in);
  11.  
  12.  
  13.         String[] friends = scanner.nextLine().split(", ");
  14.         List<String> friendList = new ArrayList<>();
  15.         for (String friend : friends) {
  16.             friendList.add(friend);
  17.         }
  18.  
  19.         int blacklistedCount = 0;
  20.         int lostCount = 0;
  21.  
  22.         String input = scanner.nextLine();
  23.         while (!input.equals("Report")) {
  24.             String[] command = input.split(" ");
  25.             String action = command[0];
  26.  
  27.             switch (action) {
  28.                 case "Blacklist":
  29.                     String nameToBlacklist = command[1];
  30.                     if (friendList.contains(nameToBlacklist)) {
  31.                         friendList.set(friendList.indexOf(nameToBlacklist), "Blacklisted");
  32.                         System.out.println(nameToBlacklist + " was blacklisted.");
  33.                         blacklistedCount++;
  34.                     } else {
  35.                         System.out.println(nameToBlacklist + " was not found.");
  36.                     }
  37.                     break;
  38.                 case "Error":
  39.                     int index = Integer.parseInt(command[1]);
  40.                     if (isValidIndex(index, friendList) && !friendList.get(index).equals("Blacklisted") && !friendList.get(index).equals("Lost")) {
  41.                         String lostName = friendList.get(index);
  42.                         friendList.set(index, "Lost");
  43.                         System.out.println(lostName + " was lost due to an error.");
  44.                         lostCount++;
  45.                     }
  46.                     break;
  47.                 case "Change":
  48.                     int changeIndex = Integer.parseInt(command[1]);
  49.                     if (isValidIndex(changeIndex, friendList)) {
  50.                         String newName = command[2];
  51.                         String currentName = friendList.get(changeIndex);
  52.                         friendList.set(changeIndex, newName);
  53.                         System.out.println(currentName + " changed his username to " + newName + ".");
  54.                     }
  55.                     break;
  56.             }
  57.  
  58.             input = scanner.nextLine();
  59.         }
  60.  
  61.         System.out.println("Blacklisted names: " + blacklistedCount);
  62.         System.out.println("Lost names: " + lostCount);
  63.         for (String friend : friendList) {
  64.             System.out.print(friend + " ");
  65.         }
  66.     }
  67.  
  68.     private static boolean isValidIndex(int index, List<String> friendList) {
  69.         return index >= 0 && index < friendList.size();
  70.     }
  71. }
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement