Project: Model an Order
Lesson 43 of 48 in Coddy's Python for Java Developers course.
Build a four-stage order processor: a model, interchangeable pricing policies, validated batch input, and a summary. Transfer Java composition and integer-cents design to Python while using modules and callable methods. The same six files stay present throughout; each step states which behavior to replace. This project uses only in-memory data and standard input.
Challenge
BeginnerBegin a four-stage in-memory order processor. Implement Order(customer,quantity,unit_cents) and subtotal() in order.py. Inputs are a nonempty customer without a colon, positive integer quantity, and nonnegative integer cents, on three lines. Print customer:quantity:subtotal. Keep integer cents, as in a Java money model. Complete the subtotal while leaving the later policy/helper scaffolds in place. The starter runs with an unfinished result; all four steps keep the same files.
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()
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 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