// Exercise 2: GroupBy and aggregation (medium) // // Implement TotalSpendByCustomer so that, given a list of orders // (CustomerName, Amount), it returns a Dictionary mapping each customer // name to their total spend across all their orders. Use GroupBy + Sum // (or Aggregate) — no manual dictionary bookkeeping in a loop. // // Implement TopSpender so that it returns the customer name with the // highest total spend, reusing TotalSpendByCustomer. // // Run with: dotnet run (inside a console project) or paste into a Main method. using System; using System.Collections.Generic; using System.Linq; class Program { #pragma warning disable IDE0060 static Dictionary TotalSpendByCustomer(List<(string CustomerName, decimal Amount)> orders) { // TODO: GroupBy(o => o.CustomerName), then Sum the Amount per group, // and build a Dictionary keyed by customer name. return new Dictionary(); } static string TopSpender(List<(string CustomerName, decimal Amount)> orders) { // TODO: call TotalSpendByCustomer, then find the key with the // highest value (OrderByDescending + First, or Aggregate/MaxBy). return ""; } #pragma warning restore IDE0060 static void Main() { int pass = 0, total = 0; var orders = new List<(string CustomerName, decimal Amount)> { ("Alice", 50m), ("Bob", 20m), ("Alice", 30m), ("Charlie", 100m), ("Bob", 15m) }; var totals = TotalSpendByCustomer(orders); total++; if (totals.Count == 3 && totals["Alice"] == 80m && totals["Bob"] == 35m && totals["Charlie"] == 100m) { Console.WriteLine("PASS: totals grouped and summed per customer"); pass++; } else Console.WriteLine("FAIL: totals grouped and summed per customer, got " + string.Join(", ", totals.Select(kv => $"{kv.Key}={kv.Value}"))); var top = TopSpender(orders); total++; if (top == "Charlie") { Console.WriteLine("PASS: top spender identified"); pass++; } else Console.WriteLine("FAIL: top spender identified, got " + top); Console.WriteLine($"--- {pass}/{total} checks passed ---"); } }