Advertisement
Deiancom

02. Friendlist Maintenance

Nov 11th, 2019
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.29 KB | None | 0 0
  1. package JavaFundamentals.ExamsPreparation;
  2.  
  3. import java.util.*;
  4. import java.util.stream.Collectors;
  5.  
  6. public class FriendListMaintenance02 {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.         List<String> names = Arrays.stream(scanner.nextLine().split(",\\s+")).collect(Collectors.toList());
  10.         int blackListedNames = 0;
  11.         int lostNames = 0;
  12.         while (true) {
  13.             String input = scanner.nextLine();
  14.             if (input.equals("Report")) {
  15.                 break;
  16.             }
  17.             String[] command = input.split("\\s+");
  18.             switch (command[0]) {
  19.                 case "Blacklist":
  20.                     if (names.contains(command[1])) {
  21.                         blackListedNames++;
  22.                         System.out.printf("%s was blacklisted.%n", command[1]);
  23.                         names.set(names.indexOf(command[1]), "Blacklisted");
  24.                     } else {
  25.                         System.out.printf("%s was not found.", command[1]);
  26.                     }
  27.                     break;
  28.                 case "Error": {
  29.                     int index = Integer.parseInt(command[1]);
  30.                     if (!names.get(index).equals("Blacklisted") && !names.get(index).equals("Lost")) {
  31.                         String name = names.get(index);
  32.                         lostNames++;
  33.                         System.out.printf("%s was lost due to an error.%n", names.get(index));
  34.                         names.set(names.indexOf(name), "Lost");
  35.                     }
  36.                     break;
  37.                 }
  38.                 case "Change": {
  39.                     int index = Integer.parseInt(command[1]);
  40.                     if (index >= 0 && index < names.size()) {
  41.                         String name = names.get(index);
  42.                         names.set(names.indexOf(name), command[2]);
  43.                         System.out.printf("%s changed his username to %s.%n", name, command[2]);
  44.                     }
  45.                     break;
  46.                 }
  47.             }
  48.         }
  49.         System.out.printf("Blacklisted names: %d%n", blackListedNames);
  50.         System.out.printf("Lost names: %d%n", lostNames);
  51.         for (String name : names) {
  52.             System.out.print(name + " ");
  53.         }
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement