The Order Book

Time for another project! This checkpoint puts the last three lessons to work, object types, unions and narrowing, and data modeled with discriminated unions, together with everything you learned before them. As in the first checkpoint, the instructions say what the program should print, and how you write it is up to you. And as there, each exercise also runs your program with other data, so it has to work everything out from the data.

The project. The blacksmith of Raven Rock keeps a book of orders, and you're writing the program that prices them. Over three exercises, it will work out the price of every order, give regular customers their discounts, and then handle a new kind of order.

The data. Each order is an Order, a discriminated union with the tag kind. The blacksmith forges new items ("forge") from one of three metals, repairs damaged ones ("repair") and sharpens blades ("sharpen"). Every order has a customer and an item, and some kinds have more: a forged item has its metal, and a repair says how badly the item is damaged, in percent. forgePrices is a Record with the price of forging from each metal, and orders holds this week's orders, one per line.

The task. Forging costs what forgePrices says for the metal, a repair costs half of the damage in gold, and sharpening always costs sharpenPrice. Print a line for each order and the total at the end:

Kira: forging, Arrowheads (steel), 35 gold
Bruno: repair, Battle Axe, 15 gold
Lena: sharpening, Dagger, 8 gold
Bruno: forging, Shield (bronze), 20 gold
Total: 78 gold

A tip: a switch on kind with the exhaustiveness check from the Modeling Data lesson is a good start. You'll be glad to have it in the last exercise.

Instructions

Print a line for each order with its price, as in the example.

Below the orders, print the total.

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

1/3