Advertisement
veronikaaa86

02. Shopping List

Oct 18th, 2021
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. package examPreparation;
  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 P02ShoppingList {
  9. public static void main(String[] args) {
  10. Scanner scanner = new Scanner(System.in);
  11.  
  12. List<String> productList = Arrays.stream(scanner.nextLine().split("!")).collect(Collectors.toList());
  13.  
  14. String inputLine = scanner.nextLine();
  15. while (!inputLine.equals("Go Shopping!")) {
  16. String[] commandArr = inputLine.split(" ");
  17. String command = commandArr[0];
  18.  
  19. switch (command) {
  20. case "Urgent":
  21. String itemToAdd = commandArr[1];
  22.  
  23. if (!productList.contains(itemToAdd)) {
  24. productList.add(0, itemToAdd);
  25. }
  26. break;
  27. case "Unnecessary":
  28. String itemToRemove = commandArr[1];
  29.  
  30. productList.remove(itemToRemove);
  31. break;
  32. case "Correct":
  33. String oldItem = commandArr[1];
  34. String newItem = commandArr[2];
  35.  
  36. // for (int i = 0; i < productList.size(); i++) {
  37. // if (productList.get(i).equals(oldItem)) {
  38. // productList.set(i, newItem);
  39. // }
  40. // }
  41.  
  42. if (productList.contains(oldItem)) {
  43. int index = productList.indexOf(oldItem);
  44. productList.set(index, newItem);
  45. }
  46. break;
  47. case "Rearrange":
  48. String itemToRearrange = commandArr[1];
  49. if (productList.contains(itemToRearrange)) {
  50. productList.remove(itemToRearrange);
  51. productList.add(itemToRearrange);
  52. }
  53. break;
  54. }
  55.  
  56. inputLine = scanner.nextLine();
  57. }
  58.  
  59. System.out.println(String.join(", ", productList));
  60. }
  61. }
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement