Project: Pricing Policies
Lesson 44 of 48 in Coddy's Python for Java Developers course.
Challenge
BeginnerExtend the existing Order to accept and store policy, and add total() delegating to policy.total(subtotal). Implement StandardPolicy.total unchanged, BulkPolicy.total subtracting subtotal // 10 only for subtotal at least 1000, and make_policy(name). Read the previous three lines plus standard or bulk. Replace the old customer:quantity:subtotal output with only customer:subtotal:total. For 1009 cents, bulk gives 909, not 908. This is Java strategy composition expressed with Python collaborators. Carry forward editable files; complete both policy files and make_policy in functions.py, leaving parse_order for step 3. Replace main() input/output in main.py.
Editable files: order.py, standard_policy.py, bulk_policy.py, functions.py, main.py. Keep the provided imports and the required API names. The locked driver.py supplies execution and checks; do not edit it.
All four steps keep order.py, standard_policy.py, bulk_policy.py, functions.py, main.py, and locked driver.py. Keep the supplied entry wrapper. The driver calls main() once and then tests the APIs without more input or output. Each solution is the complete program through its step.
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 Java Developers
1From Java Classes to Python
Run Python Without a ClassPrinting Text and CommentsBindings, Types, and finalDivision and Numeric TypesIndentation Defines BlocksRecap: A Python Receipt4Iteration and Transformations
Enhanced Loops and rangeIndexes and Map EntriesSentinels and Loop ControlFrom Streams to ComprehensionsRecap: Stable Ranking2Values Beyond Java Primitives
None and Boolean ValuesValue Equality and IdentityTruth Tests Beyond booleanBranches and Value SelectionRecap: Missing or Eligible3Collections and Object Sharing
Lists and ArrayList HabitsTuples Without Record ClassesDictionaries and Java MapsShared References and CopiesRecap: Inventory Collections6Contracts, Errors, and Modules
Text Input and Exact OutputValueError and TypeErrorModules Versus Utility ClassesAnnotations Are Not Java TypesRecap: Validate a Batch9Project: An Order Processor
Project: Model an OrderProject: Pricing PoliciesPractice on your own: Online Python compiler