Produce the Summary Report
Lesson 46 of 48 in Coddy's Python for Lua Developers course.
Challenge
BeginnerKeep all model, pricing, and validation behavior from step 3. Input now starts with a nonnegative record count, followed by exactly that many lines. Each record is customer,quantity,unit_cents,policy. Strip surrounding whitespace from every field. A valid record has exactly four fields, a nonempty customer, quantity parsed by int and greater than zero, price parsed by int and nonnegative, and policy exactly standard or bulk. Customer names never contain a colon or comma. Reject every other record; the count itself is always valid. Integer text uses ordinary signed base-ten notation when valid; malformed numeric fields can be arbitrary text. Replace every per-record ok:... or invalid print with one final report. Print accepted:N, rejected:N, and total:CENTS on three lines. Then print customer:CENTS for each customer with at least one accepted order, sorted by Python's default ascending string order (case-sensitive). Add each order's individually discounted total; do not apply a discount after grouping. Include accepted zero-cost customers. Rejected records contribute neither money nor customers. Empty batches still print the three zero-valued summary lines. Submit the complete final program.
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 unchanged. Keep all classes and both helpers in their existing files. In main.py, replace the per-record output inside main() with aggregation and the final summary report. Keep the supplied imports and wrapper; no new files are needed.
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
parsed = parse_order(" Probe , 2 , 505 , bulk ")
assert isinstance(parsed, Order)
assert (parsed.customer, parsed.quantity, parsed.unit_cents) == ("Probe", 2, 505)
assert parsed.subtotal() == 1010 and parsed.total() == 909
for invalid_line in ["broken", "A,1,2,standard,extra", ",1,2,bulk", "A,0,1,standard", "A,1,-1,bulk", "A,no,2,standard", "A,1,2,vip"]:
try:
parse_order(invalid_line)
except ValueError:
pass
else:
raise AssertionError("parse_order must reject invalid records")
All lessons in Python for Lua Developers
1From Lua to Python
Run Your First Python ProgramPrinting and TextNames and Local ScopeNumbers and ConversionSuites Instead of endRecap: Rewrite a Receipt4Iteration and Transformations
Numeric for to rangepairs, ipairs, and itemsLoop Control and SkipsComprehensions and SortingRecap: Build a Ranking2Values and Decisions
nil Versus NoneTable Identity and EqualityTruthiness Is Differentelif and Value SelectionRecap: Eligibility Rules3Tables to Typed Collections
Sequence Tables to ListsTuples and Multiple ReturnsRecords to DictionariesSets and Shared TablesRecap: Clean an Inventory6Errors and Modules
Input and Exact Textpcall to Exceptionsrequire to importHints and Runtime TypesRecap: Validate a Batch9Project: Order Processing
Create the Order ModelAdd Pricing PoliciesPractice on your own: Online Python compiler