Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package MidExam.O4;
- import java.util.Arrays;
- import java.util.List;
- import java.util.Scanner;
- import java.util.stream.Collectors;
- public class O2ShoppingList {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- List<String> test = Arrays.stream(scanner.nextLine().split("!")).
- map(String::toString).collect(Collectors.toList());
- String command = scanner.nextLine();
- while (!command.equals("Go Shopping!")) {
- String type = command.split("\\s+")[0];
- String product1 = command.split("\\s+")[1];
- switch (type) {
- case "Urgent":
- if (test.contains(product1)) {
- break;
- } else {
- test.add(0, product1);
- }
- break;
- case "Unnecessary":
- for (int i = 0; i < test.size(); i++) {
- if (test.get(i).equals(product1)) {
- test.remove(i);
- i--;
- }
- }
- break;
- case "Correct":
- String product2 = command.split("\\s+")[2];
- for (int i = 0; i < test.size(); i++) {
- if (test.get(i).equals(product1)) {
- test.set(i, product2);
- }
- }
- break;
- case "Rearrange":
- if (test.contains(product1)) {
- test.remove(product1);
- test.add(product1);
- }
- break;
- }
- command = scanner.nextLine();
- }
- for (int i = 0; i < test.size(); i++) {
- if (i == test.size() - 1) {
- System.out.print(test.get(i));
- break;
- }
- System.out.print(test.get(i) + ", ");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment