Final Project: Event-Driven Inventory System
Combine interfaces, generics, and events into one small system.
Requirements
- Define an
IIteminterface with at least aNameand aValueproperty. - Create a generic
Inventory<T> where T : IItemclass that canAdd(T item)andRemove(T item), backed by aList<T>. - Add an
event Action<T> OnItemAdded(or a custom delegate) onInventory<T>that fires whenever an item is added. - Subscribe to
OnItemAddedfrom outside the class to print a message like"Added: {item.Name} ({item.Value} gold)". - Wrap any invalid operation (e.g. removing an item that isn't in the inventory) in a custom exception and handle it with
try/catchwhere the inventory is used. - Create at least two classes implementing
IItem(e.g.Weapon,Potion) and add instances of both to the sameInventory<IItem>.
Stretch goals
- Add a
TotalValueproperty using LINQ'sSum. - Add an
OnItemRemovedevent too. - Make
Inventory<T>implementIEnumerable<T>so it can be used directly in aforeachloop.
Submit your repository link below when you are done, an instructor will review it before you can mark this lesson complete. Good luck! 🚀