-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimplePricing.ts
94 lines (71 loc) · 1.77 KB
/
simplePricing.ts
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
type Price = number // Float to 2 decimal places
interface UnitOfSale {
constraint(quantity: number): boolean
}
class Can implements UnitOfSale {
constraint(quantity: number): boolean {
return true;
}
}
class Pound implements UnitOfSale {
constraint(quantity: number): boolean {
return true;
}
}
interface Item {
name: string,
unit: UnitOfSale
price: Price
promotion: Discount
}
type Discount = SingleItemDiscount | BulkDiscount | BuyXGetYFreeDiscount | NoDiscount
interface SingleItemDiscount {
minimumQuantity: 1
numberOfDiscounted: number
discount: number// float
}
interface BulkDiscount {
minimumQuantity: number
numberOfDiscountedItems: number
discount: number // float 0.0 to 1.0
}
interface BuyXGetYFreeDiscount {
minimumQuantity: number, // x + y
numberOfDiscountedItems: number, //1
discount: 1.0
}
interface NoDiscount {
minimumQuantity: typeof Infinity,
numberDiscountedItems: 0
discount: 0.0
}
interface DiscountedItem extends Item {
originalPrice: Price
}
interface CheckoutPriceCalculator {
calculateItemPrice(cartItem: CartItem): Array<Item | DiscountedItem>
// if minimumQuanity then numberOfDiscoutedItems is promotions.price * discount.discount(DiscountedItem); CartItem.quanity - numberOfDiscountedItems is item.price(Item)
}
interface CartItem {
item: Item | DiscountedItem,
quantity: number
}
interface Cart {
items: Array<CartItem>
subtotal: Price
checkoutPriceCalculator: CheckoutPriceCalculator
addItem(item: Item): Cart
calculateSubTotal(items: Array<CartItem>): Cart
}
class CannedItem implements Item {
name = 'Beans'
unit = new Can()
price = 0.5
promotion: Discount
}
class BulkItem implements Item {
name = 'Dry Beans'
unit = new Pound()
price = 1.99
promotion: Discount
}