ralitsadzhukeva

2. Shopping List

Jun 16th, 2023
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 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. products.add(command[1]);
  23. break;
  24. case "Unnecessary":
  25. if (products.contains(command[1])) {
  26. products.remove(command[1]);
  27. }
  28. break;
  29. case "Correct":
  30. String oldItem = command[1];
  31. String newItem = command[2];
  32. if (products.contains(oldItem)){
  33. int place = products.indexOf(oldItem);
  34. products.set(place,newItem);
  35. }
  36. break;
  37. case "Rearrange":
  38. if (products.contains(command[1])){
  39. products.remove(command[1]);
  40. products.add(command[1]);
  41. }
  42. break;
  43. }
  44. input=scanner.nextLine();
  45. }
  46. System.out.println(String.join(", ", products));
  47. }
  48. }
  49.  
Advertisement
Add Comment
Please, Sign In to add comment