Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.List;
- import java.util.Scanner;
- import java.util.stream.Collectors;
- public class Main {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- List<String> coffees = Arrays.asList(scan.nextLine().split(" ")).stream().collect(Collectors.toList());
- int n = Integer.parseInt(scan.nextLine());
- while (n> 0) {
- String[] command = scan.nextLine().split(" ");
- switch (command[0]) {
- case "Include":
- coffees.addLast(command[1]);
- break;
- case "Remove":
- int count = Integer.parseInt(command[2]);
- while(count>0) {
- if (command[1].equals("first") && count <= coffees.size()) {
- coffees.removeFirst();
- } else if (command[1].equals("last") && count <= coffees.size()) {
- coffees.removeLast();
- }
- count--;
- }
- break;
- case "Prefer":
- int index1 = Integer.parseInt(command[1]);
- int index2 = Integer.parseInt(command[2]);
- if(index1 == coffees.size() || index2 == coffees.size()) {
- break;
- }
- if (index1 >= 0 && index1 < coffees.size() && index2 >= 0 && index2 < coffees.size()) {
- // Swap elements correctly
- String temp = coffees.get(index1);
- coffees.set(index1, coffees.get(index2));
- coffees.set(index2, temp);
- }
- break;
- case "Reverse":
- Collections.reverse(coffees);
- break;
- }
- n--;
- }
- System.out.println("Coffees:");
- for (String coffee : coffees) {
- System.out.print(coffee + " ");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement