Advertisement
ivanmitkoff

02_Friendlist_Maintenance_ProgramFundMidExam_2Nov2019_Group2

Nov 2nd, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.58 KB | None | 0 0
  1. import java.util.List;
  2. import java.util.Arrays;
  3. import java.util.Scanner;
  4. import java.util.stream.Collectors;
  5.  
  6. public class FriendlistMaintenance {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         List<String> friends = Arrays.stream(scanner.nextLine().split(", "))
  11.                 .collect(Collectors.toList());
  12.  
  13.         String command = scanner.nextLine();
  14.  
  15.         int blacklistedCount = 0;
  16.         int lostNamesCount = 0;
  17.  
  18.         while(!command.equals("Report")) {
  19.             String[] tokens = command.split(" ");
  20.             String subCommand = tokens[0];
  21.             int index = 0;
  22.  
  23.             switch (subCommand) {
  24.  
  25.                 case "Blacklist":
  26.                     String name = tokens[1];
  27.  
  28.                         if (friends.contains(name)) {
  29.                             System.out.printf("%s was blacklisted.%n", name);
  30.                             friends.set(friends.indexOf(name), "Blacklisted");
  31.                             blacklistedCount++;
  32.                             break;
  33.                         } else {
  34.                             System.out.printf("%s was not found.%n", name);
  35.                         }
  36.                     break;
  37.                 case "Error":
  38.                     index = Integer.parseInt(tokens[1]);
  39.  
  40.                     if (checkIfIndexExists(friends, index)) {
  41.                         if (!(friends.get(index)).equals("Blacklisted") && !(friends.get(index)).equals("Lost")) {
  42.                             System.out.printf("%s was lost due to an error.%n", friends.get(index));
  43.                             friends.set(index, "Lost");
  44.                             lostNamesCount++;
  45.                         }
  46.                     }
  47.                     break;
  48.  
  49.                 case "Change":
  50.                     index = Integer.parseInt(tokens[1]);
  51.                     String newName = tokens[2];
  52.  
  53.                     if (checkIfIndexExists(friends, index)) {
  54.                         System.out.printf("%s changed his username to %s.%n", friends.get(index), newName);
  55.                         friends.set(index, newName);
  56.                     }
  57.                     break;
  58.             }
  59.             command = scanner.nextLine();
  60.         }
  61.         System.out.printf("Blacklisted names: %d%n", blacklistedCount);
  62.         System.out.printf("Lost names: %d%n", lostNamesCount);
  63.         System.out.print(String.join(" ", friends));
  64.     }
  65.  
  66.     private static boolean checkIfIndexExists(List<String> friends, int index) {
  67.         return index >= 0 && index < friends.size();
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement