-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercise.rb
88 lines (74 loc) · 1.7 KB
/
exercise.rb
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
require 'date'
class ShoppingCart
def initialize
@items = []
end
def add_item(item)
@items.push(item)
end
def checkout
total_price = 0
puts "CHECKOUT: "
puts "----------------------------"
@items.each do |item|
puts "#{item.name}" + ": " + "#{item.price}" + "$"
total_price += item.price
end
puts "----------------------------"
puts "TOTAL PRICE: " + "#{total_price}" + "$"
special_discount(total_price)
end
def special_discount(total_price)
if @items.length > 5
total_price = total_price * 0.9
puts "Special Discount (more than 5 items) 10% off"
puts "----------------------------"
puts "FINAL PRICE: " + "#{total_price}" + "$"
end
end
end
class Item
attr_reader :name, :price
def initialize(name, price)
@name = name
@price = price
end
def price
@price
end
end
class Houseware < Item
def price
if @price > 100
discounted_price = @price * 0.9
discounted_price
else
@price
end
end
end
class Fruit < Item
def price
today_day = Date.today.wday
if today_day == 0 || today_day == 6
discounted_price = @price * 0.95
discounted_price
else
@price
end
end
end
banana = Fruit.new("Banana", 10)
orange_juice = Item.new("Orange Juice", 10)
rice = Item.new("Rice", 1)
vacuum_cleaner = Houseware.new("Vacuum Cleaner", 150)
anchovies = Item.new("Anchovies", 2)
urtzis_cart = ShoppingCart.new
urtzis_cart.add_item(orange_juice)
urtzis_cart.add_item(rice)
urtzis_cart.add_item(rice)
urtzis_cart.add_item(rice)
urtzis_cart.add_item(rice)
urtzis_cart.add_item(vacuum_cleaner)
urtzis_cart.add_item(banana)
urtzis_cart.checkout