4.5 Day-to-day: parcel pricing
Page 6 of 10
4.5.1 What the program must do
Each afternoon the store prices the parcels it ships that day. A parcel's price has four parts:
- the zone: 300 cents to a local address, 450 for the rest of the country, 1200 abroad;
- the weight: nothing up to 500 g, 150 cents up to 2 kg, 400 above;
- the speed: standard is free, and express costs 500 cents, or 900 abroad;
- the delivery: 250 cents to the door, and nothing to a locker or a store.
The price list shows each part on a line of its own, then the total, so that a customer at the desk can see where the price comes from. The program is one module, pricing, in a package whose una.toml names it as the entry and grants the console:
[package]
name = "pricing"
version = "0.1.0"
edition = "0.2"
[run]
entry = "pricing"
grants = ["console"]4.5.2 The kinds
The data comes first: three sums, a record, and a generic record:
--- How far a parcel travels, nearest first.
kind Zone = Local | National | Abroad
--- How fast a parcel travels.
kind Speed = Standard | Express
--- Where the customer collects a parcel. Each variant carries what that place needs.
kind Delivery = AtDoor | AtLocker(locker: Text) | AtStore(store: Text)
--- A parcel ready to price.
kind Parcel
order: Text
grams: Int
zone: Zone
speed: Speed
delivery: Delivery
--- One part of a price: what it is for, of any type, and its amount in cents.
kind Charge[T]
item: T
cents: IntZone and Speed are sums of variants without fields, nearest and slowest first. Delivery is a sum whose variants carry what each place needs: a locker's name, a store's name, and nothing for the door. Parcel is a record that holds all the rest: a parcel always has an order, a weight, a zone, a speed and a delivery.
Charge[T] is one line of the price list: what the line is for, and its amount. What it is for depends on the line. For the zone it is the Zone itself, for the speed the Speed, and for the weight and the delivery a text. So item has the type parameter T, and cents is always an Int.
4.5.3 Pricing each part
Each part of the price is a function that gives back a Charge:
--- The charge for the zone. One arm for each zone, and no `else`.
zone_charge(zone: Zone) -> Charge[Zone]
=
when zone
is Local -> Charge(item: zone, cents: 300)
is National -> Charge(item: zone, cents: 450)
is Abroad -> Charge(item: zone, cents: 1200)
--- The charge for the weight, by bands.
weight_charge(grams: Int) -> Charge[Text]
=
if grams <= 500
Charge(item: "up to 500 g", cents: 0)
else if grams <= 2000
Charge(item: "up to 2 kg", cents: 150)
else
Charge(item: "over 2 kg", cents: 400)
--- The charge for the speed. Express costs more abroad.
speed_charge(speed: Speed, zone: Zone) -> Charge[Speed]
=
when speed
is Standard -> Charge(item: speed, cents: 0)
is Express -> Charge(item: speed, cents: if zone is Abroad then 900 else 500)
--- The charge for the last step: only the door costs extra.
delivery_charge(delivery: Delivery) -> Charge[Text]
=
when delivery
is AtDoor -> Charge(item: "to the door", cents: 250)
is AtLocker(locker) -> Charge(item: "locker {locker}", cents: 0)
is AtStore(store) -> Charge(item: "store {store}", cents: 0)zone_chargehas one arm per zone and noelse. If the store adds a zone, this function stops checking until the new zone has a price, which is what the store wants. Each arm gives aCharge[Zone]whose item is the zone itself.weight_chargechooses by a number, not by a variant, so it usesifandelse if, as in section 3.4. Its item is the name of the weight band.speed_chargetakes the zone too, since express costs more abroad. The arm forExpressusesisinside anif, to ask one question about the zone.delivery_chargebinds the field of each variant that has one, so that the line names the locker or the store.
4.5.4 The total, and a parcel sent express
--- The price of a parcel, in cents: the four charges added up.
total(parcel: Parcel) -> Int
=
let zone = zone_charge(parcel.zone)
let weight = weight_charge(parcel.grams)
let speed = speed_charge(parcel.speed, zone: parcel.zone)
let delivery = delivery_charge(parcel.delivery)
zone.cents + weight.cents + speed.cents + delivery.cents
--- The same parcel, sent express. The parcel passed in does not change.
express(parcel: Parcel) -> Parcel = Parcel(order: parcel.order, grams: parcel.grams, zone: parcel.zone, speed: Express, delivery: parcel.delivery)total names each charge with let, then adds their cents fields. Each let holds a Charge of its own type: zone is a Charge[Zone] and weight a Charge[Text], and both have a cents field that is an Int, so they add up.
express gives back the parcel as it would be sent express. A record never changes, so it builds a new Parcel with every field of the old one and Express as its speed. The parcel passed in is unchanged: main prices both.
4.5.5 Printing a quote
--- One line of the price list. Interpolating `charge.item` needs `Eq`.
charge_line[T: Eq](charge: Charge[T]) -> Text = " {charge.item}: {charge.cents} cents"
--- Prints a parcel's charges, one per line, and its total.
quote(parcel: Parcel) -> Unit
uses @console: Console[print]
=
@console.print("Parcel {parcel.order}, {parcel.grams} g")
@console.print(charge_line(zone_charge(parcel.zone)))
@console.print(charge_line(weight_charge(parcel.grams)))
@console.print(charge_line(speed_charge(parcel.speed, zone: parcel.zone)))
@console.print(charge_line(delivery_charge(parcel.delivery)))
@console.print(" total: {total(parcel)} cents")
--- Prints the quotes of the day's parcels, and what express would cost for the first.
pub main() -> Unit
uses @console: Console[print]
=
let lamp = Parcel(order: "A-1001", grams: 1200, zone: National, speed: Standard, delivery: AtLocker("Station 14"))
let rug = Parcel(order: "A-1002", grams: 3500, zone: Local, speed: Express, delivery: AtDoor)
let gift = Parcel(order: "A-1003", grams: 400, zone: Abroad, speed: Express, delivery: AtStore("Harbour Road"))
quote(lamp)
quote(rug)
quote(gift)
@console.print("Express for {lamp.order}: {total(lamp)} cents now, {total(express(lamp))} cents express")charge_line is a generic function. It writes charge.item into a text, and interpolation of a value of T needs the constraint Eq, so the header says [T: Eq]. Every item of this program is a Zone, a Speed or a Text, and each is Eq, so one function writes every line of the list.
quote prints one parcel: its order and weight, its four charges, and its total. main builds the day's three parcels and prices each. Its last line compares the first parcel's price now with its price sent express.
4.5.6 Running it
In the day-to-day directory:
una check .
una run .una check . prints nothing, and una run . prints the price list:
Parcel A-1001, 1200 g
National: 450 cents
up to 2 kg: 150 cents
Standard: 0 cents
locker Station 14: 0 cents
total: 600 cents
Parcel A-1002, 3500 g
Local: 300 cents
over 2 kg: 400 cents
Express: 500 cents
to the door: 250 cents
total: 1450 cents
Parcel A-1003, 400 g
Abroad: 1200 cents
up to 500 g: 0 cents
Express: 900 cents
store Harbour Road: 0 cents
total: 2100 cents
Express for A-1001: 600 cents now, 1100 cents expressEach item prints as interpolation writes it: a variant by its name, such as National or Express, and a text as it is. The parcel abroad pays 900 cents for express, and the others 500.
4.5.7 The whole program
module pricing
-- The price of shipping a parcel: its zone, its weight, its speed and where it is collected.
--- How far a parcel travels, nearest first.
kind Zone = Local | National | Abroad
--- How fast a parcel travels.
kind Speed = Standard | Express
--- Where the customer collects a parcel. Each variant carries what that place needs.
kind Delivery = AtDoor | AtLocker(locker: Text) | AtStore(store: Text)
--- A parcel ready to price.
kind Parcel
order: Text
grams: Int
zone: Zone
speed: Speed
delivery: Delivery
--- One part of a price: what it is for, of any type, and its amount in cents.
kind Charge[T]
item: T
cents: Int
--- The charge for the zone. One arm for each zone, and no `else`.
zone_charge(zone: Zone) -> Charge[Zone]
=
when zone
is Local -> Charge(item: zone, cents: 300)
is National -> Charge(item: zone, cents: 450)
is Abroad -> Charge(item: zone, cents: 1200)
--- The charge for the weight, by bands.
weight_charge(grams: Int) -> Charge[Text]
=
if grams <= 500
Charge(item: "up to 500 g", cents: 0)
else if grams <= 2000
Charge(item: "up to 2 kg", cents: 150)
else
Charge(item: "over 2 kg", cents: 400)
--- The charge for the speed. Express costs more abroad.
speed_charge(speed: Speed, zone: Zone) -> Charge[Speed]
=
when speed
is Standard -> Charge(item: speed, cents: 0)
is Express -> Charge(item: speed, cents: if zone is Abroad then 900 else 500)
--- The charge for the last step: only the door costs extra.
delivery_charge(delivery: Delivery) -> Charge[Text]
=
when delivery
is AtDoor -> Charge(item: "to the door", cents: 250)
is AtLocker(locker) -> Charge(item: "locker {locker}", cents: 0)
is AtStore(store) -> Charge(item: "store {store}", cents: 0)
--- The price of a parcel, in cents: the four charges added up.
total(parcel: Parcel) -> Int
=
let zone = zone_charge(parcel.zone)
let weight = weight_charge(parcel.grams)
let speed = speed_charge(parcel.speed, zone: parcel.zone)
let delivery = delivery_charge(parcel.delivery)
zone.cents + weight.cents + speed.cents + delivery.cents
--- The same parcel, sent express. The parcel passed in does not change.
express(parcel: Parcel) -> Parcel = Parcel(order: parcel.order, grams: parcel.grams, zone: parcel.zone, speed: Express, delivery: parcel.delivery)
--- One line of the price list. Interpolating `charge.item` needs `Eq`.
charge_line[T: Eq](charge: Charge[T]) -> Text = " {charge.item}: {charge.cents} cents"
--- Prints a parcel's charges, one per line, and its total.
quote(parcel: Parcel) -> Unit
uses @console: Console[print]
=
@console.print("Parcel {parcel.order}, {parcel.grams} g")
@console.print(charge_line(zone_charge(parcel.zone)))
@console.print(charge_line(weight_charge(parcel.grams)))
@console.print(charge_line(speed_charge(parcel.speed, zone: parcel.zone)))
@console.print(charge_line(delivery_charge(parcel.delivery)))
@console.print(" total: {total(parcel)} cents")
--- Prints the quotes of the day's parcels, and what express would cost for the first.
pub main() -> Unit
uses @console: Console[print]
=
let lamp = Parcel(order: "A-1001", grams: 1200, zone: National, speed: Standard, delivery: AtLocker("Station 14"))
let rug = Parcel(order: "A-1002", grams: 3500, zone: Local, speed: Express, delivery: AtDoor)
let gift = Parcel(order: "A-1003", grams: 400, zone: Abroad, speed: Express, delivery: AtStore("Harbour Road"))
quote(lamp)
quote(rug)
quote(gift)
@console.print("Express for {lamp.order}: {total(lamp)} cents now, {total(express(lamp))} cents express")
example "each zone has its price"
do zone_charge(National)
expect result == Charge(item: National, cents: 450)
example "the nearest zone is the cheapest"
do zone_charge(Local)
expect result.cents == 300
example "abroad costs the most"
do zone_charge(Abroad)
expect result.cents == 1200
example "500 g is still the lightest band"
do weight_charge(500)
expect result == Charge(item: "up to 500 g", cents: 0)
example "501 g is the next band"
do weight_charge(501)
expect result == Charge(item: "up to 2 kg", cents: 150)
example "2 kg is the last gram of the middle band"
do weight_charge(2000)
expect result.cents == 150
example "over 2 kg pays the most"
do weight_charge(2001)
expect result == Charge(item: "over 2 kg", cents: 400)
example "standard speed is free"
do speed_charge(Standard, zone: Abroad)
expect result.cents == 0
example "express at home costs 500"
do speed_charge(Express, zone: National)
expect result == Charge(item: Express, cents: 500)
example "express abroad costs 900"
do speed_charge(Express, zone: Abroad)
expect result.cents == 900
example "the door costs extra"
do delivery_charge(AtDoor)
expect result == Charge(item: "to the door", cents: 250)
example "a locker is free, and named"
do delivery_charge(AtLocker("Station 14"))
expect result == Charge(item: "locker Station 14", cents: 0)
example "a store is free, and named"
do delivery_charge(AtStore("Harbour Road"))
expect result == Charge(item: "store Harbour Road", cents: 0)
example "the total adds the four charges"
do total(Parcel(order: "A-1002", grams: 3500, zone: Local, speed: Express, delivery: AtDoor))
expect result == 1450
example "express changes the speed and nothing else"
do express(Parcel(order: "A-1001", grams: 1200, zone: National, speed: Standard, delivery: AtDoor))
expect result == Parcel(order: "A-1001", grams: 1200, zone: National, speed: Express, delivery: AtDoor)Next:4.6 Testing it