Add Pricing Policies
Lesson 44 of 48 in Coddy's Python for Kotlin 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 Kotlin Developers
1From Kotlin to Python
Run Your First Python ProgramPrint and String TemplatesVal, Var, and Python NamesDivision and Integer WidthSuites and Block ScopeRecap: Rewrite a Receipt4Iteration and Transformations
Ranges and Loop BindingsEntries and withIndexUpdates and Loop ControlCollection PipelinesRecap: Build a Ranking2Values and Decisions
Nullable Types and NoneEquality and IdentityBoolean Conditions and orIf Expressions and ElifRecap: Eligibility Rules3Collections and Shared State
Read-only Lists and MutationPair and Tuple UnpackingMap Lookup and Missing KeysShallow Copies and SetsRecap: Clean an Inventory6Input, Errors, and Modules
Console Input and FormattingExceptions and Resource ScopeImports and Entry PointsStatic Types and AnnotationsRecap: Validate a Batch9Project: Order Processing
Create the Order ModelAdd Pricing PoliciesPractice on your own: Online Python compiler