desislava_topuzakova

Untitled

Nov 1st, 2021
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. package MidExam;
  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 PhoneShop {
  9. public static void main(String[] args) {
  10. Scanner scanner = new Scanner(System.in);
  11.  
  12. List<String> input = Arrays.stream(scanner.nextLine().split(", ")).collect(Collectors.toList());
  13.  
  14. String command = scanner.nextLine();
  15.  
  16. while (!command.equals("End")){
  17. String phone = command.split(" - ")[1];
  18. switch (command.split(" - ")[0]){
  19. case "Add":
  20. //String addPhone = command.split(" - ")[1];
  21. if (!input.contains(phone)){
  22. input.add(phone);
  23. }
  24. break;
  25. case "Remove":
  26. if (input.contains(phone)) {
  27. input.remove(phone);
  28. }
  29. break;
  30. case "Last":
  31. if (input.contains(phone)){
  32. input.remove(phone);
  33. input.add(phone);
  34. }
  35. break;
  36. default:
  37. String phh = command.split(" ")[3];
  38. String oldPhone = phh.split(":")[0];
  39. String newPhone = phh.split(":")[1];
  40.  
  41. int index = 0;
  42. if (input.contains(oldPhone)){
  43. for (int i = 0; i < input.size(); i++){
  44. if (input.get(i).equals(oldPhone)){
  45. index = i;
  46. }
  47. }
  48. input.add(index + 1, newPhone);
  49. }
  50. //System.out.println(input.toString().replaceAll("[\\[\\],]", "").replaceAll(" ", ", "));
  51. break;
  52.  
  53. }
  54.  
  55. command = scanner.nextLine();
  56. }
  57. System.out.println(input.toString().replaceAll("[\\[\\],]", "").replaceAll(" ", ", "));
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment