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

Takeaway Challenge - Simon #2242

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ end
group :development, :test do
gem 'rubocop', '1.20'
end

group :development do
gem 'twilio-ruby'
end
Empty file added Notes
Empty file.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
OK I got pretty lost on this one, and ended up running out of time. I initially went round a rabbit hole of having a separate Dish class but then didn't manage to work out how to test it properly. I decided to leave it any try to get Twilio working - but keep bumping into Syntax errors when I try to wrap it in a class.

I hadn't even started on the user interface, and did a rush job on that and it's semi-finished. To be honest, a lot of the code in here now doesn't work together and is a bit janky, but hey. It's a partial solution and gave me lots of things to work on.


Takeaway Challenge
==================
```
Expand Down
22 changes: 22 additions & 0 deletions lib/menu.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Menu

# For whoever is code reviewing this - I shamelessly stole these dishes from one
# of my fav restaurants - Rasa in Stoke Newington, I highly recommend it if you're ever in the area.

def initialize(dish_class=Dish)
@dishes = [
{ name: "Rasam", price: 5 },
{ name: "Plain Dosa", price: 6 },
{ name: "Masala Dosa", price: 7 },
{ name: "Rasavangi", price: 7.5 },
{ name: "Plain Rice", price: 3 },
{ name: "Tamarind Rice", price: 3.5 },
{ name: "Mango Halwa", price: 4 }
]
end

def view
@dishes.each { |x| print "#{x[:name]}, £#{x[:price]}"+ "\n" }
end

end
29 changes: 29 additions & 0 deletions lib/order.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Order
attr_reader :dishes, :state

def initialize
@dishes = []
end

def add(dish)
dishes << dish
end

def remove(dish)
dishes.delete(dish)
end

def total
total = 0
dishes.each { |x| total += x.price }
total
end

def place
if dishes.empty?
"Sorry, you have no items in your basket. Please add at least one before placing an order."
else
"Thank you! Your order was placed and will be delivered soon."
end
end
end
46 changes: 46 additions & 0 deletions lib/restaurant.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class Restaurant
m = Menu.new
o = Order.new
def run
puts "Welcome to our takeaway. You can add and remove dishes then place your order. What would you like to do?"
action = gets.chomp.downcase
next_action
end

def next_action
if action == "add"
add
next_action
elsif action == "remove"
remove
next_action
elsif action == "place"
o.place
end
else
puts "That doesn't seem to be a valid action, please choose add remove or place."
action = gets.chomp.downcase
next_action
end

def add
puts "Here is our menu, what would you like to order?"
m.view
dishes = gets.chomp.downcase
o.add(dishes)
puts "Your basket total is #{o.total}, what would you like to do next?"
action = gets.chomp.downcase
end

def remove
puts "Here is your basket, what would you like to remove?"
puts o.dishes.join("/n")
dishes = gets.chomp.downcase
o.remove(dishes)
puts "Your basket total is #{o.total}, what would you like to do next?"
action = gets.chomp.downcase
end
end

r = Restaurant.new
r.run
12 changes: 12 additions & 0 deletions lib/sms.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'twilio-ruby'

account_sid = "AC4652867e62a8ad85f253fcd1c96016ba" # Your Test Account SID from www.twilio.com/console/settings
auth_token = "590379d91fe68d1e887e7dcf91354037" # Your Test Auth Token from www.twilio.com/console/settings

@client = Twilio::REST::Client.new account_sid, auth_token
message = @client.messages.create(
body: "Hello from Ruby",
to: "+447447062441", # Replace with your phone number
from: "+15005550006") # Use this Magic Number for creating SMS

puts message.sid
8 changes: 8 additions & 0 deletions spec/menu_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'menu'
describe Menu do
describe '#view' do
it 'prints the items in the menu' do
expect { subject.view }.to output(["Rasam, £5", "Plain Dosa, £6", "Masala Dosa, £7", "Rasavangi, £7.5", "Plain Rice, £3", "Tamarind Rice, £3.5", "Mango Halwa, £4"].join("\n")).to_stdout
end
end
end
49 changes: 49 additions & 0 deletions spec/order_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require 'order'
describe Order do
let(:dish) { double :dish }
let(:fiverdish) { double :fiverdish, price: 5 }
let(:tennerdish) { double :tennerdish, price: 10 }
describe '#add' do
it 'can add a dish' do
expect { subject.add(dish) }.to change(subject, :dishes).from([]).to([dish])
end
end

describe '#remove' do
before (:each) { subject.add(dish) }
it 'can remove a dish' do
expect { subject.remove(dish) }.to change(subject, :dishes).from([dish]).to([])
end
end

describe '#total' do
context 'there is one dish in the basket' do
before (:each) { subject.add(fiverdish) }
it 'totals an order with one dish' do
expect(subject.total).to eq 5
end

context 'there are two dishes in the basket' do
before(:each) { subject.add(tennerdish) }
it 'totals a basket with multiple dishes' do
expect(subject.total).to eq 15
end
end
end
end

describe '#place' do
context 'there is at least one dish in the basket' do
before (:each) { subject.add(dish) }
it 'tells user they have placed an order' do
expect(subject.place).to eq("Thank you! Your order was placed and will be delivered soon.")
end
end

context 'there are no dishes in the basket' do
it 'tells user they have an empty basket' do
expect(subject.place).to eq("Sorry, you have no items in your basket. Please add at least one before placing an order.")
end
end
end
end