Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Uprajnenie;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.*;
- import java.util.function.Predicate;
- import java.util.stream.Collectors;
- public class PredicateParty {
- public static void main(String[] args) throws IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- String line = reader.readLine();
- List<String> guests = Arrays.stream(line.split("\\s+")).collect(Collectors.toList());
- while (!"Party!".equals(line = reader.readLine())) {
- String[] commandTokens = line.split("\\s+");
- String command = commandTokens[0];
- String predicateType = commandTokens[1];
- String predicateArgument = commandTokens[2];
- if (command.equals("Remove")) {
- guests.removeIf(getPredicate(predicateType, predicateArgument));
- } else if (command.equals("Double")) {
- List<String> temp = new ArrayList<>();
- for (String guest : guests) {
- if (getPredicate(predicateType, predicateArgument).test(guest)) {
- temp.add(guest);
- }
- temp.add(guest);
- }
- guests = temp;
- }
- }
- if (guests.isEmpty()) {
- System.out.println("Nobody is going to the party!");
- } else {
- Collections.sort(guests);
- System.out.println(String.join(", ", guests) + " are going to the party!");
- }
- }
- private static Predicate<String> getPredicate(String type, String parameter) {
- switch (type) {
- case "StartsWith":
- return text -> text.startsWith(parameter);
- case "EndsWith":
- return text -> text.endsWith(parameter);
- case "Length":
- return text -> text.length() == Integer.parseInt(parameter);
- default:
- return text -> false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement