From be81672906c7eccf411cdb7a04852a36378fb700 Mon Sep 17 00:00:00 2001 From: Zalary Young Date: Tue, 11 Feb 2014 11:02:55 -0600 Subject: [PATCH 1/3] Completed initial project --- bar.rb | 58 ++++++++++++++++++++++++++++++++++++++++++++++++ spec/bar_spec.rb | 40 ++++++++++++++++++++++++--------- 2 files changed, 88 insertions(+), 10 deletions(-) diff --git a/bar.rb b/bar.rb index 49d91d90..21b43ce9 100644 --- a/bar.rb +++ b/bar.rb @@ -1,4 +1,62 @@ 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) + @menu_items << Menu.new(name, price) + end + + def happy_discount=(discount) + if discount < 0 + @happy_discount = 0 + elsif discount > 1 + @happy_discount = 1 + else + @happy_discount = discount + + end + end + + def happy_hour? + t = Time.now + if t.hour > 16 || t.hour < 15 + false + else + true + end + end + + def get_price(name) + price = 0 + menu_items.each do |x| + if x.name == name + price = x.price + end + end + if happy_hour? + price / 2 + else + price + end + end +end + + + +class Menu +attr_accessor :name, :price + +def initialize(name, price) + @name = name + @price = price +end + end diff --git a/spec/bar_spec.rb b/spec/bar_spec.rb index 524d20e3..645ba39c 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,41 @@ 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 "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 +60,42 @@ # 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" do # TODO: WRITE TESTS TO ENSURE BAR DISCOUNTS DURING HAPPY HOUR + + it 'gets the price of a drink given a name' do + @three_pm = Time.parse("2014-2-1 + 1 15:00:00") + Time.stub(:now).and_return(@three_pm) + @bar.add_menu_item('Cosmo', 5.40) + + expect(@bar.get_price('Cosmo')).to eq(2.70) end end +end From 10462eeef9df21dad856570b290d5620cc9b3f00 Mon Sep 17 00:00:00 2001 From: Zalary Young Date: Tue, 11 Feb 2014 12:07:38 -0600 Subject: [PATCH 2/3] first extention --- bar.rb | 36 ++++++++++++++++++++++++++---------- spec/bar_spec.rb | 19 ++++++++++++------- 2 files changed, 38 insertions(+), 17 deletions(-) diff --git a/bar.rb b/bar.rb index 21b43ce9..acf03345 100644 --- a/bar.rb +++ b/bar.rb @@ -21,32 +21,48 @@ def happy_discount=(discount) @happy_discount = 1 else @happy_discount = discount - end - end - def happy_hour? - t = Time.now - if t.hour > 16 || t.hour < 15 - false - else - true - end end def get_price(name) price = 0 + 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 end end if happy_hour? - price / 2 + puts "#{Time.now.wday}" + price * happy_discount + + else + puts "#{Time.now.wday}" price + end end + + + + + + def happy_hour? + t = Time.now + if t.hour > 16 || t.hour < 15 + false + else + true + end + end + + end diff --git a/spec/bar_spec.rb b/spec/bar_spec.rb index 645ba39c..aa43d06c 100644 --- a/spec/bar_spec.rb +++ b/spec/bar_spec.rb @@ -86,16 +86,21 @@ end - context "During happy hours" do + 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 - @three_pm = Time.parse("2014-2-1 - 1 15:00:00") - Time.stub(:now).and_return(@three_pm) - @bar.add_menu_item('Cosmo', 5.40) + 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 - expect(@bar.get_price('Cosmo')).to eq(2.70) + context "During happy hours on other days" 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 end From 3914d92d5f17388c4e12da258b29617354128c62 Mon Sep 17 00:00:00 2001 From: Zalary Young Date: Tue, 11 Feb 2014 18:07:31 -0600 Subject: [PATCH 3/3] Completed extention #10 --- bar.rb | 27 ++++++++++++--------------- spec/bar_spec.rb | 20 +++++++++++++++++++- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/bar.rb b/bar.rb index acf03345..051395c4 100644 --- a/bar.rb +++ b/bar.rb @@ -10,8 +10,9 @@ def initialize(name) @happy_discount = 0 end - def add_menu_item(name, price) - @menu_items << Menu.new(name, price) + def add_menu_item(name, price, exempt = false) + @menu_items << Menu.new(name, price, exempt) + puts "#{@menu_items}" end def happy_discount=(discount) @@ -27,6 +28,7 @@ def happy_discount=(discount) 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 @@ -35,24 +37,21 @@ def get_price(name) menu_items.each do |x| if x.name == name price = x.price + drink_exempt = x.exempt end end if happy_hour? - puts "#{Time.now.wday}" + if drink_exempt == true + price + else price * happy_discount - - + end else - puts "#{Time.now.wday}" price end end - - - - def happy_hour? t = Time.now if t.hour > 16 || t.hour < 15 @@ -61,18 +60,16 @@ def happy_hour? true end end - - end - class Menu -attr_accessor :name, :price +attr_accessor :name, :price, :exempt -def initialize(name, price) +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 aa43d06c..65b6bfea 100644 --- a/spec/bar_spec.rb +++ b/spec/bar_spec.rb @@ -31,6 +31,13 @@ expect(@bar.menu_items.count).to eq(2) end + 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 @@ -95,7 +102,7 @@ end end - context "During happy hours on other days" do + 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")) @@ -103,4 +110,15 @@ expect(@bar.get_price('Cosmo')).to eq(3) end end + + 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