Learned about Sets
parent
b7f92e3ced
commit
9ac119d054
@ -0,0 +1,20 @@
|
||||
import common.Product;
|
||||
import common.Supplier;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
|
||||
public class ProductCatalogue implements Iterable<Product> {
|
||||
private final Set<Product> products = new HashSet<>();
|
||||
|
||||
public void addSupplier(final Supplier supplier) {
|
||||
|
||||
products.addAll(supplier.getProducts());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<Product> iterator() {
|
||||
return products.iterator();
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
import common.Product;
|
||||
import common.Supplier;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.NavigableSet;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
|
||||
public class WeightAwareProductCatalogue implements Iterable<Product> {
|
||||
private final NavigableSet<Product> products =
|
||||
new TreeSet<>(Product.BY_WEIGHT);
|
||||
|
||||
public void addSupplier(final Supplier supplier) {
|
||||
|
||||
products.addAll(supplier.getProducts());
|
||||
}
|
||||
|
||||
public Set<Product> findLighterProducts(final Product product) {
|
||||
|
||||
return products.headSet(product, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<Product> iterator() {
|
||||
|
||||
return products.iterator();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue