Advertisement
apostolakis

FriendListMaintenance

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