I'm trying to figure out how to handle filtering project. I'm capable of filtering my collection by one field at a time (thanks to the guava Immutable.of) but I'm forced to create a way to filter my collection once again and again, until the user tells (for example, in switch) that he's done and wants to go back to the main menu.
private String filterOption() {
System.out.println("What fields should I filter by? :\n" +
"title\n" +
"author\n" +
"content\n" +
"date\n");
String filteringOption = scanner.nextLine();
return filteringOption;
}
private String filterValue() {
System.out.println("What value you want to use for filtering? :\n");
String userInput = scanner.nextLine();
return userInput;
}
public List<Message> filterMessages() {
String filteringOption = filterOption();
String userInput = filterValue();
Map<String, BiPredicate<String, Message>> criteria = ImmutableMap.of(
"title", (userTitle, message) -> userInput.equals(message.getTitle()),
"author", (userAuthor, message) -> userInput.equals(message.getAuthor()),
"content", (userContent, message) -> userInput.equals(message.getContent()),
"date", (userDate, message) -> userInput.equals(message.getCreationDate())
);
BiPredicate<String, Message> predicate = criteria.get(filteringOption);
filteredMessages = messageStorage.getAll().stream() //getAll() is to get entire collection
.filter(m -> predicate.test(userInput, m))
.collect(Collectors.toList());
return filteredMessages;
}
Could someone show me a way to will allow user to use the values they've got from the filterMessages() to once again filter them using the same method? In short - filter what was once filtered until you are happy or there's nothing in the returned list.
Aucun commentaire:
Enregistrer un commentaire