Project 2: Pricing Policies
Lesson 44 of 48 in Coddy's Python for C# Developers course.
Challenge
BeginnerContinue the order tool. Add a policy parameter to Order, store it, and implement total(). Complete StandardPolicy.total(subtotal) returning subtotal; BulkPolicy.total(subtotal) subtracting subtotal // 10 only when subtotal >= 1000; and make_policy(name). Keep subtotal(). Input adds a fourth line, standard or bulk, to the previous three valid fields. Replace the former customer:quantity:subtotal output with customer:subtotal:total. The discount subtracts whole cents: 1009 becomes 909. Leave parse_order for step 3.
C# transfer checkpoint
C# can express the pricing boundary with IPolicy. Python delegates to compatible objects without requiring an interface declaration.
C# reference excerpt: executable statements assume using System; type-only excerpts illustrate the declared contract.
interface IPolicy { int Total(int subtotal); }
class Standard : IPolicy { public int Total(int subtotal) => subtotal; }Python pattern
class Standard:
def total(self, subtotal):
return subtotalEditable implementation files: order.py, standard_policy.py, bulk_policy.py, functions.py, main.py. The locked driver.py is the supplied launcher or input/output harness and must remain unchanged.
Use the supplied input contract without prompts. Print only the required lines, each ending in a newline.
All four steps keep order.py, standard_policy.py, bulk_policy.py, functions.py, main.py, and driver.py. Order belongs in order.py; policy classes belong in their named files; make_policy and parse_order belong in functions.py; main.py owns input/output inside main(). Carry forward the previous editable files and implement this step's additions. Replace obsolete output inside main(); do not add a second entry point. The current locked driver calls main once, then checks APIs without more input or output.
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 C# Developers
1C# Foundations in Python
Run the Python WorkspaceOutput, Strings, and CommentsTyped Locals to Python NamesDivision and RemaindersIndentation Instead of BracesRecap: Port a Receipt4Loops and Transformations
From foreach to forIndexes and Dictionary ItemsSentinels and Loop ControlFrom LINQ to ComprehensionsRecap: Stable Ranking2None and Decisions
Nullable Values and NoneEquality, Identity, and TypesTruth Tests and FallbacksBranch PriorityRecap: Eligibility Rules3Collections and References
From List<T> to listTuples and UnpackingDictionary Keys and ValuesSets, Aliases, and CopiesRecap: Stock Lookup6Reliable Python Programs
Input and Exact OutputExceptions and ValidationImports and the Main GuardType Hints and Runtime TypesRecap: Validate a Batch9Project: Order Processor
Project 1: Order ModelProject 2: Pricing PoliciesPractice on your own: Online Python compiler