lifesaver800

Friendlist Maintenance

Feb 26th, 2020
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.68 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.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         String[] names = scanner.nextLine().split(", ");
  10.         ArrayList<String> friends = new ArrayList<>(Arrays.asList(names));
  11.         int blacklistCount = 0;
  12.         int lostNamesCount = 0;
  13.  
  14.  
  15.         String command = scanner.nextLine();
  16.         while (!"Report".equals(command)) {
  17.             String[] tokens = command.split(" ");
  18.             String type = tokens[0];
  19.  
  20.             switch (type) {
  21.                 case "Blacklist": {
  22.                     String name = tokens[1];
  23.                     if (friends.contains(name)) {
  24.                         int indexName = friends.indexOf(name);
  25.                         friends.set(indexName, "Blacklisted");
  26.                         System.out.println(String.format("%s was blacklisted.", name));
  27.                         blacklistCount++;
  28.                     } else {
  29.                         System.out.println(String.format("%s was not found.", name));
  30.                     }
  31.                     break;
  32.                 }
  33.                 case "Error": {
  34.                     int index = Integer.parseInt(tokens[1]);
  35.                     String currName = friends.get(index);
  36.                     if (!currName.equals("Blacklisted") && !currName.equals("Lost")) {
  37.                         friends.set(index, "Lost");
  38.                         lostNamesCount++;
  39.                         System.out.println(String.format("%s was lost due to an error.", currName));
  40.                     }
  41.                     break;
  42.                 }
  43.                 case "Change": {
  44.                     int index = Integer.parseInt(tokens[1]);
  45.                     String newName = tokens[2];
  46.                     if (isValidIndex(friends, index)) {
  47.                         String currName = friends.get(index);
  48.                         friends.set(index, newName);
  49.                         System.out.println(String.format("%s changed his username to %s."
  50.                                 , currName, newName));
  51.                     }
  52.                     break;
  53.                 }
  54.             }
  55.  
  56.             command = scanner.nextLine();
  57.         }
  58.         System.out.println(String.format("Blacklisted names: %d", blacklistCount));
  59.         System.out.println(String.format("Lost names: %d", lostNamesCount));
  60.         for (String friend : friends) {
  61.             System.out.print(friend + " ");
  62.         }
  63.  
  64.     }
  65.  
  66.     private static boolean isValidIndex(ArrayList<String> friends, int index) {
  67.         return 0 <= index && index < friends.size();
  68.     }
  69. }
Add Comment
Please, Sign In to add comment