Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.List;
- import java.util.Scanner;
- import java.util.function.BiPredicate;
- import java.util.stream.Collectors;
- public class Main {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- List<String> names = Arrays.stream(scan.nextLine().split("\\s+")).collect(Collectors.toList());
- String input = scan.nextLine();
- while (!"Party!".equals(input)) {
- BiPredicate<String, String> predicate = null;
- String[] tokens = input.split("\\s+");
- String command = tokens[1];
- switch (command) {
- case "StartsWith":
- predicate = (n, s) -> n.startsWith(s);
- break;
- case "EndsWith":
- predicate = (n, e) -> n.endsWith(e);
- break;
- case "Length":
- predicate = (n, l) -> {
- int len = Integer.parseInt(l);
- return n.length() == len;
- };
- break;
- }
- String removeOrDouble = tokens[0];
- if ("Double".equals(removeOrDouble)) {
- int startSize = names.size();
- for (int i = 0; i < startSize; i++) {
- if (predicate.test(names.get(i), tokens[2])) {
- names.add(names.get(i));
- }
- }
- } else if ("Remove".equals(removeOrDouble)) {
- for (int i = 0; i < names.size(); i++) {
- if (predicate.test(names.get(i), tokens[2])) {
- names.remove(i);
- i--;
- }
- }
- }
- input = scan.nextLine();
- }
- names = names.stream().sorted((n1, n2) -> n1.compareTo(n2)).collect(Collectors.toList());
- if (names.size() == 0) {
- System.out.println("Nobody is going to the party!");
- } else {
- System.out.printf("%s are going to the party!",names.toString().replaceAll("\\[|]", ""));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment