Learn C# · lesson 57 of 61 · about 12 minutes
Project: Inventory Tracker
Track a shop's stock with an enum for categories and a generic store for any kind of item.
What you'll learn
- Model categories with an enum
- Write a generic store keyed by name
- Summarise stock per category
Everything the hero carries
Before the dragon, the hero lays out everything they're carrying: the things in the bag, the gear bought at the forge, and the boss's treasure. Each thing becomes an Item with a Kind, Gear, Supplies or Treasure (the enum, the record and the list carried are given). You write the inventory: a generic Store<T> like the shop's in this lesson, keeping each thing once by its name; then a line for every kind, such as Supplies: bread, key, rope, torch or Gear: nothing; and last a look for the key, The key opens the lair's gate.
The plan
The shop sells food, drinks and toys. The tracker will:
1. Use enum Category { Food, Drink, Toy }
2. Store products as record Product(string Name, Category Category, int Qty)
3. Keep them in a generic Store<T> that adds and finds items by name
4. Report how many items there are in each category
A generic store
var store = new Store<Product>(p => p.Name);
store.Add(new Product("tea", Category.Drink, 12));
store.Add(new Product("ball", Category.Toy, 3));
Console.WriteLine(store.Find("tea")?.Qty);
Console.WriteLine(store.Find("cake") is null);
class Store<T>(Func<T, string> keyOf)
{
private readonly Dictionary<string, T> items = [];
public IEnumerable<T> All => items.Values;
public void Add(T item) => items[keyOf(item)] = item;
public T? Find(string key) => items.TryGetValue(key, out T? item) ? item : default;
}
enum Category { Food, Drink, Toy }
record Product(string Name, Category Category, int Qty);Output
12
TrueTotals per category
Loop over Enum.GetValues<Category>() so every category appears, even empty ones, and sum the quantities of the products in it with LINQ's Where and Sum.
The report
var products = new List<Product>
{
new("tea", Category.Drink, 12),
new("ball", Category.Toy, 3),
new("juice", Category.Drink, 5),
};
foreach (Category c in Enum.GetValues<Category>())
{
int total = products.Where(p => p.Category == c).Sum(p => p.Qty);
Console.WriteLine($"{c}: {total}");
}
enum Category { Food, Drink, Toy }
record Product(string Name, Category Category, int Qty);Output
Food: 0
Drink: 17
Toy: 3Every kind, even an empty one
Looping over Enum.GetValues<Kind>() gives Gear, Supplies and Treasure in the order they're written, so every kind gets a line, even one the hero has nothing of. inventory.All.Where(i => i.Kind == k) picks that kind's things, and OrderBy puts their names in order. The store is keyed by name, so the boss's two coins, and the gem that's both in the bag and in the treasure, are each kept once. Find("key") gives back the key's Item, or null if the hero never picked one up.
Tip
The Store takes a Func that says how to get a key from an item, so the same class could store customers by email or orders by number.
Now try it yourself
This lesson is free in Codiqo, with 8 questions on what you just read. You can only learn to code by writing code, so it ends with a short program you write yourself, which we run and check straight away.