-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInventory.hs
More file actions
35 lines (25 loc) · 1.36 KB
/
Inventory.hs
File metadata and controls
35 lines (25 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
-- | Inventory Module
module Inventory where
import Product
import Vendor
import Syntax
-- | inventory infomation
inventory :: Inventory
inventory = [(1,(20,100)), (2,(15,200)), (3,(27,300)), (4,(6,400)), (5,(10,500))]
-- | check if the qty of input product is available in the inventory
checkFromInventory :: [(Pid, Qty)] -> [(Pid, Price)]
checkFromInventory ps = map (checkAvailability inventory) ps
-- | first [(Product, Qty)] : User request
-- second [(Product, Qty)] : what we have in inventory
checkAvailability :: Inventory -> (Pid, Qty) -> (Pid,Price)
checkAvailability inv (p,q) = case lookup p inv of
Nothing -> error "Do not find item in inventory "
Just (iqty,price) -> if q > iqty
then let newPrice = purchaseFromVendor vendor p
in (p,newPrice)
else (p,price)
-- | purchase form vendor and get a new price
purchaseFromVendor :: Vendor -> Pid -> Price
purchaseFromVendor v pid = case lookup pid v of
Nothing -> error ("Product " ++show pid ++ "'s Qty is not avaible in the vendor")
Just p -> p