diff --git a/bar.rb b/bar.rb index 49d91d90..051395c4 100644 --- a/bar.rb +++ b/bar.rb @@ -1,4 +1,75 @@ require 'time' # you're gonna need it class Bar + attr_reader :name + attr_accessor :menu_items, :happy_discount + + def initialize(name) + @name = name + @menu_items = [] + @happy_discount = 0 + end + + def add_menu_item(name, price, exempt = false) + @menu_items << Menu.new(name, price, exempt) + puts "#{@menu_items}" + end + + def happy_discount=(discount) + if discount < 0 + @happy_discount = 0 + elsif discount > 1 + @happy_discount = 1 + else + @happy_discount = discount + end + + end + + def get_price(name) + price = 0 + drink_exempt = false + if @happy_discount == 0 && (Time.now.wday == 1 || Time.now.wday == 3) + @happy_discount = 0.5 + else + @happy_discount = 0.75 + end + menu_items.each do |x| + if x.name == name + price = x.price + drink_exempt = x.exempt + end + end + if happy_hour? + if drink_exempt == true + price + else + price * happy_discount + end + else + price + + end + end + + def happy_hour? + t = Time.now + if t.hour > 16 || t.hour < 15 + false + else + true + end + end +end + + +class Menu +attr_accessor :name, :price, :exempt + +def initialize(name, price, exempt = false) + @name = name + @price = price + @exempt = exempt +end + end diff --git a/spec/bar_spec.rb b/spec/bar_spec.rb index 524d20e3..65b6bfea 100644 --- a/spec/bar_spec.rb +++ b/spec/bar_spec.rb @@ -1,6 +1,7 @@ require 'pry-debugger' require "./bar.rb" +# pry-debugger describe Bar do @@ -12,41 +13,48 @@ expect(@bar.name).to eq("The Irish Yodel") end - xit "cannot change its name" do + it "cannot change its name" do # That would require a lengthy marketing meeting expect { @bar.name = 'lolcat cave' }.to raise_error end - xit "initializes with an empty menu" do + it "initializes with an empty menu" do expect(@bar.menu_items.count).to eq(0) end - xit "can add menu items" do + it "can add menu items" do @bar.add_menu_item('Cosmo', 5.40) @bar.add_menu_item('Salty Dog', 7.80) expect(@bar.menu_items.count).to eq(2) end - xit "can retrieve menu items" do + it 'sets a menu item exempt from discount' do + @bar.add_menu_item('Top Shelf', 4, exempt = true) + item = @bar.menu_items.last + expect(item.name).to eq 'Top Shelf' + expect(item.exempt).to eq true + end + + it "can retrieve menu items" do @bar.add_menu_item('Little Johnny', 9.95) item = @bar.menu_items.first expect(item.name).to eq 'Little Johnny' expect(item.price).to eq 9.95 end - xit "has a default happy hour discount of zero" do + it "has a default happy hour discount of zero" do expect(@bar.happy_discount).to eq 0 end - xit "can set its happy hour discount" do + it "can set its happy hour discount" do @bar.happy_discount = 0.5 expect(@bar.happy_discount).to eq 0.5 end - xit "constrains its happy hour discount to between zero and one" do + it "constrains its happy hour discount to between zero and one" do # HINT: You need to write your own setter @bar.happy_discount = 2 expect(@bar.happy_discount).to eq 1 @@ -59,23 +67,58 @@ # DO NOT CHANGE SPECS ABOVE THIS LINE # # # # # # # # # # # # # # # # # # # # # # # - describe '#happy_hour', :pending => true do + describe '#happy_hour' do it "knows when it is happy hour (3:00pm to 4:00pm)" do - # TODO: CONTROL TIME + @three_pm = Time.parse("2014-2-11 15:00:00") + Time.stub(:now).and_return(@three_pm) expect(@bar.happy_hour?).to eq(true) end it "is not happy hour otherwise" do # TODO: CONTROL TIME + @two_pm = Time.parse("2014-2-11 14:00:00") + Time.stub(:now).and_return(@two_pm) expect(@bar.happy_hour?).to eq(false) end end context "During normal hours" do - # TODO: WRITE TESTS TO ENSURE BAR KNOWS NOT TO DISCOUNT + + it 'gets the price of a drink given a name' do + Time.stub(:now).and_return(Time.parse("2 pm")) + @bar.add_menu_item('Cosmo', 5.40) + @bar.add_menu_item('Salty Dog', 7.80) + expect(@bar.get_price('Cosmo')).to eq(5.40) + end + + end + + context "During happy hours on Mon and Wed" do + # TODO: WRITE TESTS TO ENSURE BAR DISCOUNTS DURING HAPPY HOUR + it 'gets the price of a drink given a name' do + Time.stub(:now).and_return(Time.parse("2014-2-10 15:00:00")) + @bar.add_menu_item('Cosmo', 4) + expect(@bar.get_price('Cosmo')).to eq(2) end +end + + context "During happy hours on not Mon or Wed" do + # TODO: WRITE TESTS TO ENSURE BAR DISCOUNTS DURING HAPPY HOUR + it 'gets the price of a drink given a name' do + Time.stub(:now).and_return(Time.parse("2014-2-11 15:00:00")) + @bar.add_menu_item('Cosmo', 4) + expect(@bar.get_price('Cosmo')).to eq(3) + end +end - context "During happy hours" do + context "exempt drinks should never be discounted" do # TODO: WRITE TESTS TO ENSURE BAR DISCOUNTS DURING HAPPY HOUR + it 'gets the price of a drink given a name' do + Time.stub(:now).and_return(Time.parse("2014-2-11 15:00:00")) + @bar.add_menu_item('Top Shelf', 8, exempt = true) + expect(@bar.get_price('Top Shelf')).to eq(8) end end + + +end