Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1.  
  2.  
  3. package MidExamPrep.second;
  4.  
  5. import java.sql.SQLOutput;
  6. import java.util.ArrayList;
  7. import java.util.Arrays;
  8. import java.util.List;
  9. import java.util.Scanner;
  10. import java.util.stream.Collectors;
  11.  
  12. public class FriendlistMaintenance {
  13. public static void main(String[] args) {
  14. Scanner scanner = new Scanner(System.in);
  15. String[] some = scanner.nextLine().split(", ");
  16. ArrayList<String> friendList = new ArrayList<>(Arrays.asList(some));
  17.  
  18. int blackListedNames = 0;
  19. int lostNames = 0;
  20.  
  21. String input = scanner.nextLine();
  22. while (!input.equals("Report")) {
  23. String[] tokens = input.split(" ");
  24. String command = tokens[0];
  25. switch (command) {
  26. case "Blacklist":
  27. String blacklist = tokens[1];
  28. if (friendList.contains(blacklist)) {
  29. int getIndex = friendList.indexOf(blacklist);
  30. friendList.set(getIndex, "Blacklisted");
  31. System.out.println(String.format("%s was blacklisted.", blacklist));
  32. blackListedNames++;
  33.  
  34.  
  35. } else {
  36. System.out.println(String.format("%s was not found.", blacklist));
  37. }
  38.  
  39. break;
  40. case "Error": {
  41. int index = Integer.parseInt(tokens[1]);
  42. String names = friendList.get(index);
  43. if (!friendList.get(index).equals("Blacklisted") && !friendList.equals("Lost")) {
  44. lostNames++;
  45. System.out.println(String.format("%s was lost due to an error.", names));
  46. friendList.set(index, "Lost");
  47.  
  48. }
  49. }
  50. break;
  51. case "Change": {
  52. int index = Integer.parseInt(tokens[1]);
  53. String newName = tokens[2];
  54. String newIndex = friendList.get(index);
  55. String oldName = friendList.get(index);
  56. if (isValidIndex(friendList, index)) {
  57. friendList.set(index, newName);
  58. System.out.println(String.format("%s changed his username to %s."
  59. , oldName, newName));
  60.  
  61.  
  62. }
  63. break;
  64. }
  65. }
  66. input = scanner.nextLine();
  67.  
  68. }
  69.  
  70.  
  71. System.out.println(String.format("Blacklisted names: %d", blackListedNames));
  72. System.out.println(String.format("Lost names: %d", lostNames));
  73. for (String friend : friendList) {
  74. System.out.print(friend + " ");
  75. }
  76. }
  77.  
  78. private static boolean isValidIndex(ArrayList<String> friends, int index) {
  79. return 0 <= index && index < friends.size();
  80. }
  81.  
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement