I have a rather simple exercise but I'm limited by my knowledge and the requirements to use design patterns (+ unit tests). The goal of a project is to create a console app that will allow you to save(add), print(show all), remove(remove by cryteria) and filter(show by criteria) messages from the collection.
private String title;
private String author;
private String content;
private String creationDate;
I was able to create "add" function and "show all". My problem is with the filtering. I must create an option to filter saved objects based on the criteria given by the user (all possible combinations like: filter by title and creationDate, filter by title etc.). I thought about giving the user an option to choose it from the menu by using switch and methods like this:
private final List<Message> storage = new ArrayList<Message>();
public List<Message> getAll() {
final ArrayList<Message> messages = new ArrayList<>();
messages.addAll(storage);
return messages;
}
List<Message> find(String author) {
return simpleStorage.getAll().stream()
.filter(item -> item.getAuthor() == author)
.collect(toList());
}
but I think it's not a good practice to copy lots of similar code. Also, I might find myself in the future that such solution would be tiresome and even impossible (each new parameter would add new combinations). Is there a better way to do it? Like choosing a criteria "one by one" so the users could create a combinations by themselves? I've had a tip that predicates could help me with this but I don't know where to go with this.
Aucun commentaire:
Enregistrer un commentaire