-
Notifications
You must be signed in to change notification settings - Fork 38
/
Strategy.hs
57 lines (41 loc) · 1.43 KB
/
Strategy.hs
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
module Strategy where
data CustomerType = EndCustomer | Retailer | NGO
type Price = Double
type Quantity = Double
consumerPrice :: Quantity -> Price -> Price
consumerPrice quantity price =
if quantity <= 3
then price
else price * 0.9
retailPrice :: Quantity -> Price -> Price
retailPrice quantity price
| quantity * price < 100 = price * 0.8
| quantity * price < 250 = price * 0.7
| otherwise = price * 0.5
discountPrice :: CustomerType -> (Quantity -> Price -> Price)
discountPrice EndCustomer = consumerPrice
discountPrice Retailer = retailPrice
discountPrice NGO = retailPrice
class CustomerClass a where
discount :: a -> (Quantity -> Price -> Price)
data EndCustomerType = EcType
data RetailerType = RtType
data NgoType = NgType
instance CustomerClass EndCustomerType where
discount _ = consumerPrice
instance CustomerClass RetailerType where
discount _ = retailPrice
instance CustomerClass NgoType where
discount _ = retailPrice
strategyDemo = do
putStrLn "Strategy Pattern -> Higher Order Functions"
print $ discountPrice EndCustomer 2 10
print $ discountPrice EndCustomer 9 10
print $ discountPrice Retailer 9 10
print $ discountPrice Retailer 20 10
print $ discountPrice Retailer 60 10
print $ discount EcType 2 10
print $ discount EcType 9 10
print $ discount RtType 20 10
print $ discount NgType 60 10
putStrLn ""