Add Pricing Policies
Lesson 44 of 48 in Coddy's Python for PHP Developers course.
Challenge
BeginnerKeep the order model from step 1 and add a pricing collaborator to each order. Introduce StandardPolicy.total(subtotal), which returns subtotal unchanged, and BulkPolicy.total(subtotal), which subtracts subtotal // 10 only when subtotal is at least 1000 cents. Add Order.total() and a reusable make_policy(name) function. Input keeps the first three lines from step 1 and adds a fourth line, either standard or bulk. All other input remains valid under step 1's rules. Remove the old customer:quantity:subtotal output. Print only customer:subtotal:total. Submit the complete program, including the existing model. This rounding rule subtracts a whole-cent discount; for 1009 cents the total is 909, not 908.
Keep the named classes, methods, and functions in the complete program. After normal execution, a locked driver calls these APIs on fresh objects and asserts their behavior without printing or reading more input.
Your previous editable files carry forward. In order.py, add the policy constructor parameter, store it, and implement total(). Complete the two policy class scaffolds and make_policy in functions.py; leave parse_order for step 3. In main.py, replace the previous input/output code inside main() with this step's four-line input and new output. Keep the supplied imports and wrapper.
All four steps use the same files. Edit order.py for Order, standard_policy.py and bulk_policy.py for the pricing classes, functions.py for make_policy and parse_order, and main.py for input and output inside main(). driver.py is locked: it calls main() once, then runs the supplied API checks.
Try it yourself
from order import Order
from standard_policy import StandardPolicy
from bulk_policy import BulkPolicy
from functions import make_policy, parse_order
from main import main
main()
assert StandardPolicy().total(1009) == 1009
assert BulkPolicy().total(999) == 999
assert BulkPolicy().total(1009) == 909
assert make_policy("standard").total(1200) == 1200
assert make_policy("bulk").total(1200) == 1080
class ProbePolicy:
def __init__(self):
self.calls = []
def total(self, subtotal):
self.calls.append(subtotal)
return subtotal + 17
probe_policy = ProbePolicy()
probe_order = Order("probe", 2, 11, probe_policy)
assert (probe_order.customer, probe_order.quantity, probe_order.unit_cents) == ("probe", 2, 11)
assert probe_order.subtotal() == 22 and probe_order.total() == 39
probe_order.quantity = 3
assert probe_order.total() == 50 and probe_policy.calls == [22, 33]
probe_order.policy = StandardPolicy()
assert probe_order.total() == 33
All lessons in Python for PHP Developers
1From PHP to Python
Run Your First Python ProgramEcho, Strings, and printVariables Without Dollar SignsDivision and Numeric TextIndentation and SuitesRecap: Rewrite a Receipt4Iteration and Transformations
Foreach and rangeKeys, Items, and enumerateWhile Without IncrementFilters and Sort KeysRecap: Build a Ranking2Values and Decisions
Null, None, and Missing KeysStrict Equality Is Not isThe Zero String TrapElif and Value SelectionRecap: Eligibility Rules3Arrays and Python Collections
PHP Arrays and Python ListsDestructuring and TuplesKeys, Null, and getArray Copies and List AliasesRecap: Clean an Inventory6Input, Errors, and Modules
CLI Input and Exact OutputConversion Errors and CleanupImports Instead of requireType Hints Are Not PHP TypesRecap: Validate a Batch9Project: Order Processing
Create the Order ModelAdd Pricing PoliciesPractice on your own: Online Python compiler