require"./food"require"./drink"puts"All items are on sale on Sundays!"food1=Food.new(name:"Pizza",price:8,calorie:700)food2=Food.new(name:"Sushi",price:10,calorie:600)drink1=Drink.new(name:"Cola",price:3,volume:500)drink2=Drink.new(name:"Tea",price:2,volume:400)menus=[food1,food2,drink1,drink2]index=0menus.eachdo|menu|puts"#{index}. #{menu.info}"index+=1endputs"--------------"puts"Select an item by its number:"order=gets.chomp.to_iselected_menu=menus[order]puts"You have selected: #{selected_menu.name}"puts"How many?(Buy 3 or more for $1 discount):"count=gets.chomp.to_iputs"The total price is $#{selected_menu.get_total_price(count)}"
Parent Class menu.rb
We put parent class in the file menu.rb with these following lines
require"date"classMenuattr_accessor:nameattr_accessor:pricedefinitialize(name:,price:)self.name=nameself.price=priceenddefinforeturn"#{self.name} $#{self.price}"enddefget_total_price(count)total_price=self.price*countifcount>=3total_price-=1end# Add an if statementifMenu.discount_day?&&count>=1total_price-=1endreturntotal_priceenddefMenu.discount_day?today=Date.todayreturntoday.sunday?endend
Child Class food.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
require"./menu"classFood<Menuattr_accessor:caloriedefinitialize(name:,price:,calorie:)super(name:name,price:price)self.calorie=calorieenddefinforeturn"#{self.name} $#{self.price} (#{self.calorie}kcal)"enddefcalorie_inforeturn"#{self.name} is #{self.calorie}kcal"endend