Exercise: Product Price Lookup
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
CheckoutSystemclass 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 aKeyNotFoundExceptionfor missing items.You must use the
TryGetValue()method to safely check for the SKU and retrieve the price in a singleoperation.
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
TryGetValuemethod requires anoutparameter to hold the result if the key is found. You can declare this variable inline, likeout decimal price.TryGetValuereturns a boolean. Use it inside anifstatement to control the flow between a successful scan and an unknown barcode.
Exercise: Product Price Lookup
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
CheckoutSystemclass 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 aKeyNotFoundExceptionfor missing items.You must use the
TryGetValue()method to safely check for the SKU and retrieve the price in a singleoperation.
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
TryGetValuemethod requires anoutparameter to hold the result if the key is found. You can declare this variable inline, likeout decimal price.TryGetValuereturns a boolean. Use it inside anifstatement to control the flow between a successful scan and an unknown barcode.