Download Vending Machine Specification in Software Engineering and more Slides Software Engineering in PDF only on Docsity!
LECTURE 16: VENDING MACHINE
CASE STUDY
Software Engineering
1 Specification of a Vending Machine
- In this lecture, we will give a complete specification of a vending machine – the sort you buy cans of coke or cigarettes from.
- First, we need to introduce some types; the first one will be COIN, representing all the coins that are accepted by the machine. COIN == { 100 , 50 , 20 , 10 , 5 , 2 , 1 }
- That is, there are coins in denominations of 100, 50, 20, 10, 5, 2, and 1 pence.
- We will also need a type for system messages
- this is parachuted in: [REPORT]
- The function cost return the cost of a product in pence. For example, cost(MarsBar) = 25 cost(Penguin) = 15
- The bag stock tells us how many items of each type are in stock. For example, stock = {Penguin 7 → 2 } means that there are just 2 penguins in the machine.
- The bag float records the coins that are currently in the machine; for example float = { 100 7 → 2 , 50 7 → 8 , 5 7 → 20 } means that there are 2 × £1 coins, 8 × 50 p coins and 20 × 5 p coins.
- QUESTION: Why are stock and float bags and not sets or sequences?
- The invariant dom stock ⊆ dom cost says that everything in the machine (i.e. in stock) must have a cost associated with it.
Operations
Here are the operations we shall specify:
- initialising the machine;
- pricing goods;
- restocking;
- buying goods.
Pricing Goods
- This simply means changing the price of an item in stock, or pricing an item that is going to be stocked.
- The inputs are the item and a price.
Price ∆VendingMachine item? : PROD price? : IN cost′^ = cost ⊕ {item? 7 → price?} stock′^ = stock float′^ = float
Restocking
- The next operation to specify is that of restocking the machine with more goods.
- The only input is a new bag of products.
- The precondition dom new? ⊆ dom cost is implied by the invariant of VendingMachine′. Restock ∆VendingMachine new? : bag PROD stock′^ = stock ] new? float′^ = float cost′^ = cost
- (Note that ] is the ‘bag union’ operator.)
- We need an operation to report success...
Success rep! : REPORT rep! = ‘Okay’
- Now, we simply use the schema calculus to specify a robust version of the Restock operation, called RestockOp: RestockOP ̂= (Restock ∧ Success) ∨ GoodsNotPriced
- This schema expands to ...
RestockOp ∆VendingMachine new? : bag PROG rep! : REPORT cost′^ = cost float′^ = float (stock′^ = stock ] new? ∧ rep! = ‘Okay’) ∨ (¬(dom new? ⊆ dom cost) ∧ stock′^ = stock ∧ rep! = ‘Some goods are not priced’)
- We assume that a function
sum : bag COIN → IN is available, which takes a bag of coins and calculates how much is in the bag. For example, given a bag containing 7 × 2 p, and 3 × 5 p coins, sum{ 2 7 → 7 , 5 7 → 3 } = (2 × 7) + (5 × 3) = 14 + 15 = 29pence
- The basic Buy operation is as follows:
Buy ∆VendingMachine in?, out! : bag COIN item? : PROD item? in stock sum(in?) ≥ cost(item?) out! v float sum(in?) = sum(out!) + cost(item?) stock′^ ] {item? 7 → 1 } = stock float′^ ] out? = float ] in? cost′^ = cost