C# Intermediate: OOP, Interfaces & Events

Lesson 7 of 7

Final Project: Event-Driven Inventory System

Combine interfaces, generics, and events into one small system.

Requirements

  1. Define an IItem interface with at least a Name and a Value property.
  2. Create a generic Inventory<T> where T : IItem class that can Add(T item) and Remove(T item), backed by a List<T>.
  3. Add an event Action<T> OnItemAdded (or a custom delegate) on Inventory<T> that fires whenever an item is added.
  4. Subscribe to OnItemAdded from outside the class to print a message like "Added: {item.Name} ({item.Value} gold)".
  5. Wrap any invalid operation (e.g. removing an item that isn't in the inventory) in a custom exception and handle it with try/catch where the inventory is used.
  6. Create at least two classes implementing IItem (e.g. Weapon, Potion) and add instances of both to the same Inventory<IItem>.

Stretch goals

  • Add a TotalValue property using LINQ's Sum.
  • Add an OnItemRemoved event too.
  • Make Inventory<T> implement IEnumerable<T> so it can be used directly in a foreach loop.

Submit your repository link below when you are done, an instructor will review it before you can mark this lesson complete. Good luck! 🚀

This lesson ends in a project. Build it on your own machine, there's nowhere to submit it here, but the brief above is everything you need.