plamenavylcheva

FriendListMaintance

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