Menu
CoddyTech

Create the Order Model

Lesson 43 of 48 in Coddy's Python for JavaScript Developers course.

Build one order processing tool in four steps: model an order, compose pricing policies, validate a batch, and produce a deterministic summary. Every step keeps the useful code from the previous one. The task will identify input and output that must be replaced. All data comes from standard input; prices are integer cents and no real orders are placed.

challenge icon

Challenge

Beginner

Build an in-memory order processor. This first step models one order; later steps add pricing, reject bad records, and aggregate a report. Implement Order(customer, quantity, unit_cents) and subtotal(), then use them in a complete program. Input is three lines: a nonempty customer name with no colon, a positive integer quantity, and a nonnegative integer unit price in cents. Print exactly customer:quantity:subtotal and a newline. Keep money as integer cents. No prompts, currency symbols, or decimal conversion. The starter runs but its subtotal is intentionally unfinished.

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.

For step 1, complete Order.subtotal() in order.py. The policy classes and helper functions are unfinished scaffolds for later steps; leave them in place. The imports and entry-point wrapper are supplied scaffolding, so no advanced module setup is expected. For example, from order import Order makes the class in order.py available in another file. Importing these modules does not read input; execution starts in the locked driver.

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()

probe_order = Order("probe", 3, 7)
assert (probe_order.customer, probe_order.quantity, probe_order.unit_cents) == ("probe", 3, 7)
assert probe_order.subtotal() == 21
probe_order.quantity = 4
assert probe_order.subtotal() == 28
other_order = Order("other", 1, 0)
assert other_order.subtotal() == 0 and probe_order.subtotal() == 28

All lessons in Python for JavaScript Developers

Practice on your own: Online Python compiler