Reputation: 29
My program for university allows me to search for parts in an arraylist i created. My goal now is to apply a filter to search for specific types of parts such as 'cpu'.
This is what i'm trying to do, i input a word, the word is sent to a loop which checks if any element in the list has that word; if it does, then it will add it to a new list and print it. However this is just printing an empty loop. I'm trying to get it working for just the type of item, then i'll do the price.
private void filter(){
System.out.print("Enter type of part to view ('all' for no filtering): ");
String filterPart = In.nextLine();
System.out.print("Enter minimum price ('-1' for no filtering): ");
double minPrice = In.nextDouble();
catalogue.LoopSearch(filterPart);
}
And here is the LoopSearch Method
public Part LoopSearch(String filterPart){
List<Part> list2 = new ArrayList<Part>();
for (Part part : parts) {
if (part.hasName(filterPart)) {
list2.add(part);
}
System.out.print(list2);
}
return null;
}
lastly, this is the hasName method
public boolean hasName(String filterPart) {
return filterPart.equals(this.name);
}
Upvotes: 0
Views: 948
Reputation:
The following code can be used to filter the elements:
List<Part> newParts = parts.stream()
.filter(p -> p.hasName(filterPart))
.collect(Collectors.toList());
Upvotes: 1