Advertisement
ralitsadzhukeva

2. Shopping List

Jun 16th, 2023
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. package MidExamPreparation_01;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.List;
  6. import java.util.Scanner;
  7. import java.util.stream.Collectors;
  8.  
  9. public class ShoppingList_02 {
  10. public static void main(String[] args) {
  11. Scanner scanner = new Scanner(System.in);
  12.  
  13. List<String> products = Arrays.stream(scanner.nextLine()
  14. .split("!"))
  15. .collect(Collectors.toList());
  16. String input = scanner.nextLine();
  17.  
  18. while (!input.equals("Go Shopping!")){
  19. String[] command = input.split(" ");
  20. switch (command[0]){
  21. case "Urgent":
  22. if (products.contains(command[1])){
  23. break;
  24. }
  25. else {
  26. products.add(command[1]);
  27. }
  28. break;
  29. case "Unnecessary":
  30. if (products.contains(command[1])){
  31. products.remove(command[1]);
  32. }
  33. else {
  34. break;
  35. }
  36. break;
  37. case "Correct":
  38. String oldItem = command[1];
  39. String newItem = command[2];
  40. if (products.contains(oldItem)){
  41. int place = products.indexOf(oldItem);
  42. products.set(place,newItem);
  43. }
  44. break;
  45. case "Rearrange":
  46. if (products.contains(command[1])){
  47. products.remove(command[1]);
  48. products.add(command[1]);
  49. }
  50. break;
  51. }
  52. input=scanner.nextLine();
  53. }
  54. System.out.println(String.join(", ", products));
  55. }
  56. }
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement