-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b80ea46
commit ecdf33a
Showing
1 changed file
with
57 additions
and
0 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
JAVA ADVANCED/12. Exercise Functional Programming/P11_ThePartyReservationFilterModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import java.util.*; | ||
import java.util.function.Predicate; | ||
import java.util.stream.Collectors; | ||
|
||
public class P11_ThePartyReservationFilterModule { | ||
|
||
public static void main(String[] args) { | ||
Scanner scanner = new Scanner(System.in); | ||
|
||
List<String> guests = Arrays.stream(scanner.nextLine().split("\\s+")) | ||
.collect(Collectors.toList()); | ||
|
||
Predicate<String> predicate; | ||
|
||
List<String> commands = new ArrayList<>(); | ||
|
||
String line; | ||
while (!"Print".equals(line = scanner.nextLine())) { | ||
String[] tokens = line.split(";"); | ||
|
||
String command = tokens[0]; | ||
String filterType = tokens[1]; | ||
String filterParameters = tokens[2]; | ||
|
||
if (command.equals("Add filter")) { | ||
commands.add(line); | ||
} else if (command.equals("Remove filter")) { | ||
String commandToRemove = "Add filter;" + filterType + ";" + filterParameters; | ||
commands.removeIf(s -> s.equals(commandToRemove)); | ||
} | ||
} | ||
|
||
for (String command : commands) { | ||
String filterType = command.split(";")[1]; | ||
String filterParameters = command.split(";")[2]; | ||
|
||
switch (filterType) { | ||
case "Starts with": | ||
predicate = str -> str.startsWith(filterParameters); | ||
break; | ||
case "Ends with": | ||
predicate = str -> str.endsWith(filterParameters); | ||
break; | ||
case "Length": | ||
predicate = str -> str.length() == Integer.parseInt(filterParameters); | ||
break; | ||
default: //Contains | ||
predicate = str -> str.contains(filterParameters); | ||
break; | ||
} | ||
|
||
guests.removeIf(predicate); | ||
} | ||
|
||
System.out.println(guests.stream().collect(Collectors.joining(" ")).toString().replaceAll("([\\[\\]])", "")); | ||
} | ||
} |