Project 1: Order Model
Lesson 43 of 48 in Coddy's Python for C# Developers course.
Start the four-step in-memory order tool. Implement Order(customer, quantity, unit_cents) and subtotal(). Input is three lines: nonempty customer name with no colon, positive quantity, and nonnegative unit price in cents. Output only customer:quantity:subtotal. Keep monetary amounts as integers. Complete the model calculation; supplied policy and helper scaffolds are reserved for later steps. The starter is runnable but intentionally calculates an unfinished subtotal.
C# transfer checkpoint
Translate declared C# fields into Python instance attributes and keep cents as integers. This excerpt shows the model pattern, not the complete project.
C# reference excerpt: executable statements assume using System; type-only excerpts illustrate the declared contract.
class Line {
public int Quantity;
public int UnitCents;
public int Subtotal() => Quantity * UnitCents;
}Python pattern
class Line:
def __init__(self, quantity, unit_cents):
self.quantity = quantity
self.unit_cents = unit_cents
def subtotal(self):
return self.quantity * self.unit_centsKeep the same workspace paths throughout all four steps. Ordinary imports connect modules; the driver starts main() and performs separate API checks. Follow the current step's output contract when replacing earlier output.
Challenge
BeginnerStart the four-step in-memory order tool. Implement Order(customer, quantity, unit_cents) and subtotal(). Input is three lines: nonempty customer name with no colon, positive quantity, and nonnegative unit price in cents. Output only customer:quantity:subtotal. Keep monetary amounts as integers. Complete the model calculation; supplied policy and helper scaffolds are reserved for later steps. The starter is runnable but intentionally calculates an unfinished subtotal.
Editable 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(). This step starts from its supplied scaffolds. 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()
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 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