Advertisement
IvaAnd

ex0229_ShopplingList

Jul 1st, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.73 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Scanner;
  4.  
  5. public class ex0229_ShopplingList {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         String[] input = scanner.nextLine().split("\\!");
  10.         List<String> products = new ArrayList<>();
  11.  
  12.         for (int i = 0; i < input.length; i++) {
  13.             products.add(input[i]);
  14.         }
  15.         String command = scanner.nextLine();
  16.         while (!command.equals("Go Shopping!")) {
  17.             String[] tokens = command.split("\\s+");
  18.             String action = tokens[0];
  19.             String item = tokens[1];
  20.  
  21.  
  22.             if (action.equals("Urgent")) {
  23.                 if (products.contains(item)) {
  24.                     command = scanner.nextLine();
  25.                     continue;
  26.                 }
  27.                 products.add(0, item);
  28.             }
  29.             if (products.contains(item)) {
  30.                 if (action.equals("Unnecessary")) {
  31.                     products.remove(item);
  32.                 } else if (action.equals("Correct")) {
  33.                     String newItem = tokens[2];
  34.                     int indexOfItem = products.indexOf(item);
  35.                     products.set(indexOfItem, newItem);
  36.                 } else if (action.equals("Rearrange")) {
  37.                     products.remove(item);
  38.                     products.add(item);
  39.                 }
  40.             }
  41.  
  42.             command = scanner.nextLine();
  43.         }
  44.         for (int i = 0; i < products.size(); i++) {
  45.             if (i == products.size() - 1) {
  46.                 System.out.print(products.get(i));
  47.             } else {
  48.                 System.out.print(products.get(i) + ", ");
  49.             }
  50.         }
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement