Codiqo

Learn C# · lesson 47 of 61 · about 10 minutes

Core Logic

Build the Shop class around the hero's purse, with the rules for buying.

What you'll learn

  • Write classes that work together
  • Keep track of owned items
  • Query the catalogue with LINQ

The shop's rules

Shop ties the forge to a Purse holding the hero's gold and a HashSet<string> of what's been bought, and keeps all three private. Buy(name) finds the gear whatever case the hero types, refuses anything already bought, and only takes the gold if Purse.Spend says there's enough, so every rule lives in one place. Affordable() is one LINQ query: the gear that's cheap enough and not yet owned, cheapest first. The hero works through a shopping list, then hears what's still affordable.

The rules

  • Wallet.TrySpend(amount) returns false if there aren't enough coins
  • Shop.Buy(name) finds the item (ignoring case), refuses items already owned, then spends
  • Shop.Affordable() uses LINQ to list items the wallet can pay for, cheapest first

Each method does one thing, so each is easy to test.

Wallet and shop

var shop = new Shop(new Wallet(200));
Console.WriteLine(string.Join(", ", shop.Affordable().Select(i => i.Name)));
Console.WriteLine(shop.Buy("top hat"));
Console.WriteLine(shop.Buy("Top hat"));
Console.WriteLine(shop.Buy("crown"));
Console.WriteLine($"{shop.Wallet.Coins} coins, owns {string.Join(", ", shop.Owned)}");

record Item(string Name, string Slot, int Price);

class Wallet
{
    public int Coins { get; private set; }
    public Wallet(int coins) => Coins = coins;
    public bool TrySpend(int amount)
    {
        if (amount > Coins) return false;
        Coins -= amount;
        return true;
    }
}

class Shop
{
    private readonly List<Item> _catalogue = new()
    {
        new("Top hat", "head", 120), new("Scarf", "neck", 60),
        new("Crown", "head", 500), new("Bow tie", "neck", 40),
    };
    private readonly HashSet<string> _owned = new();
    public Wallet Wallet { get; }
    public IEnumerable<string> Owned => _owned;
    public Shop(Wallet wallet) => Wallet = wallet;

    public IEnumerable<Item> Affordable() =>
        _catalogue.Where(i => i.Price <= Wallet.Coins && !_owned.Contains(i.Name)).OrderBy(i => i.Price);

    public string Buy(string name)
    {
        Item? item = _catalogue.FirstOrDefault(i => i.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
        if (item is null) return $"No item called {name}";
        if (_owned.Contains(item.Name)) return $"You already own the {item.Name}";
        if (!Wallet.TrySpend(item.Price)) return $"The {item.Name} costs {item.Price}; you have {Wallet.Coins}";
        _owned.Add(item.Name);
        return $"Bought the {item.Name}!";
    }
}

Output

Bow tie, Scarf, Top hat
Bought the Top hat!
You already own the Top hat
The Crown costs 500; you have 80
80 coins, owns Top hat

Tip

A HashSet<T> is a collection with no duplicates and a very fast Contains, perfect for "does the player own this?".

Classes that work together

The shop doesn't do its own sums with the gold: it asks the Purse, which already refuses to spend more than it holds. That's the point of building on classes you trust. Gear? with a question mark says FirstOrDefault might find nothing, and if (gear is null) deals with that before anything else touches it.

Now try it yourself

This lesson is free in Codiqo, with 9 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.