I have made a collection of items. For example:
- Bread
- Cheese
- Coke
- Coke
- Cheese
- Crisps
I want it to print out a condensed version of the list. Expected result:
- 1 x Bread
- 2 x Cheese
- 2 x Coke
- 1 x Crisps
I have been approaching it with a for loop nested within a for loop. But I can't seem to quite get it to work.
The item class holds two variables; name and price of item.
I have been successful in getting loops to count the total, output the total value and output each item as a string. But I just can't quite get it to do this.
I have tried writing the following pseudo code to help me but I'm still stuck.
for each item in list
check item does not equal item currently being checked
if item match
then add one to item (quantity?) and delete duplicate element.
else continue search.
All I can think of is that I need to use a while loop nested inside my for loop and potentially add a new field somewhere that counts the quantity.