Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.util.function.Predicate;
- import java.util.stream.Collectors;
- public class functional1_Exercises_10Predicate_Party {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- List<String> names = Arrays.stream(scanner.nextLine().split("\\s+"))
- .collect(Collectors.toList());
- String command = scanner.nextLine();
- while (!command.equals("Party!")) {
- String[] tokens = command.split("\\s+");
- if (tokens[0].equals("Remove")) {
- List<String> namesDouble = names.stream()
- .filter(predicateRemove(names, tokens[1], tokens[2]))
- .collect(Collectors.toList());
- names.clear();
- names.addAll(namesDouble);
- } else {
- List<String> namesDouble = names.stream()
- .filter(predicateAdd(names, tokens[1], tokens[2]))
- .collect(Collectors.toList());
- names.addAll(namesDouble);
- }
- command = scanner.nextLine();
- }
- if (names.isEmpty()) {
- System.out.println("Nobody is going to the party!");
- } else {
- Collections.sort(names);
- System.out.print(names.toString().replaceAll("[\\[\\]]", ""));
- System.out.println(" are going to the party!");
- }
- }
- private static Predicate<String> predicateRemove(List<String> names, String type, String data) {
- Predicate<String> predicate = null;
- if (type.equals("StartsWith")) {
- predicate = f -> !f.startsWith(data);
- } else if (type.equals("EndsWith")) {
- predicate = e -> !e.endsWith(data);
- }
- if (type.equals("Length")) {
- predicate = e -> e.length() != Integer.parseInt(data);
- }
- return predicate;
- }
- private static Predicate<String> predicateAdd(List<String> names, String type, String data) {
- Predicate<String> predicate = null;
- if (type.equals("StartsWith")) {
- predicate = f -> f.startsWith(data);
- } else if (type.equals("EndsWith")) {
- predicate = e -> e.endsWith(data);
- } else if (type.equals("Length")) {
- predicate = e -> e.length() == Integer.parseInt(data);
- }
- return predicate;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement