From 9ac119d05465e59316c029836cdfd4f24265194a Mon Sep 17 00:00:00 2001 From: Hammy Date: Sun, 15 Nov 2020 09:15:40 +0000 Subject: [PATCH] Learned about Sets --- Java Sets/src/ProductCatalogue.java | 20 +++++++++++++ .../src/WeightAwareProductCatalogue.java | 28 +++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 Java Sets/src/ProductCatalogue.java create mode 100644 Java Sets/src/WeightAwareProductCatalogue.java diff --git a/Java Sets/src/ProductCatalogue.java b/Java Sets/src/ProductCatalogue.java new file mode 100644 index 0000000..9e55948 --- /dev/null +++ b/Java Sets/src/ProductCatalogue.java @@ -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 { + private final Set products = new HashSet<>(); + + public void addSupplier(final Supplier supplier) { + + products.addAll(supplier.getProducts()); + } + + @Override + public Iterator iterator() { + return products.iterator(); + } +} diff --git a/Java Sets/src/WeightAwareProductCatalogue.java b/Java Sets/src/WeightAwareProductCatalogue.java new file mode 100644 index 0000000..79bc8ed --- /dev/null +++ b/Java Sets/src/WeightAwareProductCatalogue.java @@ -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 { + private final NavigableSet products = + new TreeSet<>(Product.BY_WEIGHT); + + public void addSupplier(final Supplier supplier) { + + products.addAll(supplier.getProducts()); + } + + public Set findLighterProducts(final Product product) { + + return products.headSet(product, false); + } + + @Override + public Iterator iterator() { + + return products.iterator(); + } +}