I have an ArrayList
of products already initialized. The Product
constructor is:
public Product(String number, String type, double rentalDeposit, double rentalPrice, double lateFee, double buyPrice, int maxDuration)
The type
is determined by an enumeration:
protected enum productType {Basket, BabySeat, Helmet, Headlight, Bell};
I pass in the type
using the toString
method for the enumeration. I need to iterate through the ArrayList<Product>
(given by shop.getInventory()
) that I have and count how many of each type
there are, i.e. how many are of type
Basket
, BabySeat
, Helmet
, etc.
The Product
class has a getType()
method that returns a string.
for (Product.productType product : Product.productType.values()) {
int occurences = Collections.frequency(shop.getInventory(), product.toString());
}
I have tried using Collections.frequency
, but it keeps returning 0
and I'm not sure why.
Is there another way to iterate through and find this amount without using a ton of if
statements?