Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Russell's TakeAway Challenge #2228

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/menu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ def view_dishes
end

def check_if_available(item)
return 'That dish is not available. Please select another dish.' if @dishes[item].nil?
raise 'That dish is not available. Please select another dish.' if @dishes[item].nil?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are both of these messages needed? Would just the 'raise' message work without the return one?

end
end
41 changes: 41 additions & 0 deletions lib/order.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require_relative 'menu'

class Order

attr_reader :menu, :customer_order

def initialize(menu = Menu.new)
@menu = menu
@customer_order = Hash.new(0)
end

def add_dish(item)
@menu.check_if_available(item)
@customer_order[item] += @menu.dishes[item]
end

def view_order
order_subtotals
order_total
end

private

def order_subtotals
@customer_order.each do |item, price|
if price.positive?
quantity = (@customer_order[item] / @menu.dishes[item]).round
puts "#{quantity}x #{item} £#{price}"
end
end
end

def checkout_total
@customer_order.select { |_, price| price.positive? }.values.reduce(:+)
end

def order_total
puts "=========="
puts "TOTAL: £#{checkout_total}"
end
end
4 changes: 2 additions & 2 deletions spec/menu_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

describe '#check_if_available' do

it 'returns a message if the dish is not available on the menu' do
expect(subject.check_if_available("Pie")).to eq('That dish is not available. Please select another dish.')
it 'raises an error message if the dish is not available on the menu' do
expect{ subject.check_if_available("Pie") }.to raise_error 'That dish is not available. Please select another dish.'
end
end
end
46 changes: 46 additions & 0 deletions spec/order_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require 'order'

describe Order do

it 'creates an instance of Order' do
expect(subject).to be_instance_of(Order)
end

it 'has a default empty hash to store customer\'s order' do
expect(subject.customer_order).to eq Hash.new(0)
end

describe '#add_dish' do

it 'adds a dish to customer\'s order' do
expect { subject.add_dish('Burger') }.to change { subject.customer_order }.from(Hash.new(0)).to ({ 'Burger' => 5 })
end

it 'stores a dish in the customer\'s order' do
subject.add_dish('Burger')
expect(subject.customer_order).to include('Burger')
end
end

describe '#view_order' do

context 'customer\'s order contains dishes' do
before(:each) { subject.add_dish('Burger') }
before(:each) { subject.add_dish('Chips') }
before(:each) { subject.add_dish('Wings') }

it 'prints dishes from the customer\'s order with quantities, prices and total' do
# using a HEREdoc
expect { subject.view_order }.to output(<<-order
1x Burger £5
1x Chips £3
1x Wings £3
==========
TOTAL: £11
order
).to_stdout
end

end
end
end