Menu
CoddyTech

Validate and Process Orders

Lesson 45 of 48 in Coddy's Python for Kotlin Developers course.

challenge icon

Challenge

Beginner

Keep the model and pricing policies from step 2. Add parse_order(line), returning an order or raising ValueError, and replace the single-order input loop with batch processing. 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. Remove the previous customer:subtotal:total output. For each record, print either ok:customer:total or invalid, continuing after failures. An empty batch produces no output. Submit the complete program through this step. Catch only the expected validation exception.

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 order.py and both policy files. Complete the existing parse_order scaffold in functions.py without replacing make_policy. Replace the single-order code inside main() in main.py with batch processing. 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
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 Kotlin Developers

Practice on your own: Online Python compiler