I have Kitchen
and each Product
s in my Kitchen
has an expiring date,
This is with x
days to initialize after creation
x = for wine 3 years
for cheese 10 days
for eggs 2 days
The inquiry should be from the Kitchen
- which product in the kitchen expires first?
Please anyone ready to help me implement this or give me a guideline? I am just 6 weeks into the world of Java programming, challenging myself.
The most appropriate data structure is a
PriorityQueue
with aComparator
class that ordersProduct
objects by expiry date.If you wanted a more generic class, then a
TreeSet
(with the same comparator class) would do the job. (But there is a snag. Your comparator needs a tie-breaker so that two differentProduct
objects with the same expiry date are not treated as equal. If you don't do this, then one of theProduct
objects will be treated as a duplicate, and not added to the set.)