Exercise: Product Price Lookup

Implement a retail barcode scanner backend using a dictionary to map product SKUs to their prices securely.

Problem statement

In a busy retail environment, cashiers scan barcodes rapidly. The checkout system must instantly look up the price associated with each scanned SKU (Stock Keeping Unit). Occasionally, a barcode might be damaged or missing from the database, so the system must handle unrecognized scans gracefully without crashing the register.

Task requirements

  • Create a CheckoutSystem class within a defined namespace.

  • Maintain a running total of the transaction within the class.

  • Implement a ScanItem(string sku) method that attempts to find the price of the scanned barcode.

  • If the item exists, add its price to the running total and print the scanned item's price to the console.

  • If the item does not exist, print an “Unknown barcode” warning without altering the total.

Constraints

  • You must use a Dictionary<string, decimal> to store the product catalog.

  • Initialize the dictionary with at least three products using target-typed new() and a collection initializer block { }.

  • You must strictly avoid using the bracket indexer [] to look up prices dynamically, as this will throw a KeyNotFoundException for missing items.

  • You must use the TryGetValue() method to safely check for the SKU and retrieve the price in a single O(1)O(1) operation.

Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.

Get hints

  • Use the target-typed syntax new() followed by an initializer block { ["Key"] = Value, } to define your catalog dictionary at the class level.

  • The TryGetValue method requires an out parameter to hold the result if the key is found. You can declare this variable inline, like out decimal price.

  • TryGetValue returns a boolean. Use it inside an if statement to control the flow between a successful scan and an unknown barcode.

Exercise: Product Price Lookup

Implement a retail barcode scanner backend using a dictionary to map product SKUs to their prices securely.

Problem statement

In a busy retail environment, cashiers scan barcodes rapidly. The checkout system must instantly look up the price associated with each scanned SKU (Stock Keeping Unit). Occasionally, a barcode might be damaged or missing from the database, so the system must handle unrecognized scans gracefully without crashing the register.

Task requirements

  • Create a CheckoutSystem class within a defined namespace.

  • Maintain a running total of the transaction within the class.

  • Implement a ScanItem(string sku) method that attempts to find the price of the scanned barcode.

  • If the item exists, add its price to the running total and print the scanned item's price to the console.

  • If the item does not exist, print an “Unknown barcode” warning without altering the total.

Constraints

  • You must use a Dictionary<string, decimal> to store the product catalog.

  • Initialize the dictionary with at least three products using target-typed new() and a collection initializer block { }.

  • You must strictly avoid using the bracket indexer [] to look up prices dynamically, as this will throw a KeyNotFoundException for missing items.

  • You must use the TryGetValue() method to safely check for the SKU and retrieve the price in a single O(1)O(1) operation.

Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.

Get hints

  • Use the target-typed syntax new() followed by an initializer block { ["Key"] = Value, } to define your catalog dictionary at the class level.

  • The TryGetValue method requires an out parameter to hold the result if the key is found. You can declare this variable inline, like out decimal price.

  • TryGetValue returns a boolean. Use it inside an if statement to control the flow between a successful scan and an unknown barcode.

C# 14.0
namespace Retail;
using System.Collections.Generic;
public class CheckoutSystem
{
// 1. Initialize the catalog using target-typed new() and an initializer block
// Add these items: "SKU-1001" for 2.99, "SKU-1002" for 5.49, "SKU-1003" for 1.99
private readonly Dictionary<string, decimal> _catalog;
public decimal Total { get; private set; } = 0m;
public void ScanItem(string sku)
{
// 2. Safely attempt to get the price
// - Use TryGetValue on the dictionary
// - If successful, add to Total and print the price
// - If unsuccessful, print a warning
}
}