-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsource.rb
More file actions
116 lines (83 loc) · 1.7 KB
/
Copy pathsource.rb
File metadata and controls
116 lines (83 loc) · 1.7 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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
class Product
attr_reader :price, :unit_type, :cost
attr_accessor :offer
def initialize price, unit_type, cost
@price = price
@unit_type = unit_type
@cost = cost
end
def self.by_unit price, cost=nil
Product.new price, :unit, cost
end
def self.by_kg price, cost=nil
Product.new price, :kg, cost
end
def price_for quantity
if @unit_type == :kg
@price * quantity / 1000
else
@price * quantity
end
end
def cost_for quantity
if @unit_type == :kg
@cost * quantity / 1000
else
@cost * quantity
end
end
end
class ProductWithQuantity
def initialize product, quantity
@product = product
@quantity = quantity
end
def total
return @product.price_for(@quantity) if @product.offer.nil?
@product.offer.total_for @quantity
end
def cost
@product.cost_for @quantity
end
end
class ShoppingCart
def initialize
@products = []
end
def << product
@products << product
end
def total
@products.reduce(0) { |sum, product| product.total + sum }
end
end
class Offer
def initialize quantity: , price:, product:
@group_quantity = quantity
@group_price = price
@product = product
end
def total_for quantity
qty = quantity
price = 0
until qty < @group_quantity
price += @group_price
qty -= @group_quantity
end
price + @product.price_for(qty)
end
end
class Stock
def initialize
@products = []
end
def << product
@products << product
end
def total_selling_price
@products.reduce(0) { |sum, product| product.total + sum }
end
def total_cost
@products.reduce(0) { |sum, product| product.cost + sum }
end
end