Combine a product and quantity into a cart line with an exact subtotal.
A CartLine holds a Product rather than duplicating its name and price. The constructor already rejects nonpositive or noninteger quantities. The subtotal is a computed property, so it reads the current quantity whenever requested. Do not format money here; other objects still need an integer for arithmetic.
For a product costing 1250 cents, a quantity of two makes a subtotal of 2500 cents. Store the product and quantity, not a cached subtotal: changing quantity should change the next property result. Read the value with line.subtotal_cents, without parentheses.
The reading property derives seconds whenever it is accessed.
class Reading:
def __init__(self, minutes):
self.minutes = minutes
@property
def seconds(self):
return self.minutes * 60
CartLine.subtotal_cents as a property returning the product price multiplied by the line quantity.Edit main.py, then press Run. The supplied probe catches unfinished-code errors so you can inspect the Console before submitting. The completed probe prints Line cents: 2500. Press Submit to check the behavior with other inputs.
The product is available through self.product.