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

Gawain Hewitt Takeaway challenge #2235

Open
wants to merge 6 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
94 changes: 31 additions & 63 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,83 +1,51 @@
Takeaway Challenge
==================
```
_________
r== | |
_ // | M.A. | ))))
|_)//(''''': | |
// \_____:_____.-------D )))))
// | === | / \
.:'//. \ \=| \ / .:'':./ )))))
:' // ': \ \ ''..'--:'-.. ':
'. '' .' \:.....:--'.-'' .'
':..:' ':..:'
# FAKE-AWAY

```
This is Gawain Hewitt's weekend challenge for Makers Academy week 2, the takeaway challenge.

Instructions
-------

* Feel free to use google, your notes, books, etc. but work on your own
* If you refer to the solution of another coach or student, please put a link to that in your README
* If you have a partial solution, **still check in a partial solution**
* You must submit a pull request to this repo with your code by 9am Monday morning
[Process Followed](#process-followed)

Task
-----
[Instructions For Use](#instructions-for-use)

* Fork this repo
* Run the command 'bundle' in the project directory to ensure you have all the gems
* Write a Takeaway program with the following user stories:
[Things I enjoyed](#things-i-enjoyed)

```
As a customer
So that I can check if I want to order something
I would like to see a list of dishes with prices
[Things I struggled with](#things-i-struggled-with)

As a customer
So that I can order the meal I want
I would like to be able to select some number of several available dishes

As a customer
So that I can verify that my order is correct
I would like to check that the total I have been given matches the sum of the various dishes in my order

As a customer
So that I am reassured that my order will be delivered on time
I would like to receive a text such as "Thank you! Your order was placed and will be delivered before 18:52" after I have ordered
```

* Hints on functionality to implement:
* Ensure you have a list of dishes with prices
* The text should state that the order was placed successfully and that it will be delivered 1 hour from now, e.g. "Thank you! Your order was placed and will be delivered before 18:52".
* The text sending functionality should be implemented using Twilio API. You'll need to register for it. It’s free.
* Use the twilio-ruby gem to access the API
* Use the Gemfile to manage your gems
* Make sure that your Takeaway is thoroughly tested and that you use mocks and/or stubs, as necessary to not to send texts when your tests are run
* However, if your Takeaway is loaded into IRB and the order is placed, the text should actually be sent
* Note that you can only send texts in the same country as you have your account. I.e. if you have a UK account you can only send to UK numbers.

* Advanced! (have a go if you're feeling adventurous):
* Implement the ability to place orders via text message.

* A free account on Twilio will only allow you to send texts to "verified" numbers. Use your mobile phone number, don't worry about the customer's mobile phone.
### Process Followed

> :warning: **WARNING:** think twice before you push your **mobile number** or **Twilio API Key** to a public space like GitHub :eyes:
>
> :key: Now is a great time to think about security and how you can keep your private information secret. You might want to explore environment variables.

* Finally submit a pull request before Monday at 9am with your solution or partial solution. However much or little amount of code you wrote please please please submit a pull request before Monday at 9am


In code review we'll be hoping to see:

* All tests passing
* High [Test coverage](https://github.com/makersacademy/course/blob/main/pills/test_coverage.md) (>95% is good)
* The code is elegant: every class has a clear responsibility, methods are short etc.

Reviewers will potentially be using this [code review rubric](docs/review.md). Referring to this rubric in advance will make the challenge somewhat easier. You should be the judge of how much challenge you want this at this moment.

Notes on Test Coverage
------------------

You can see your [test coverage](https://github.com/makersacademy/course/blob/main/pills/test_coverage.md) when you run your tests.






### Instructions For Use



### Things I enjoyed

The challenge of writing tests for puts and user input.

Trying to keep my tests discreet in each class.

### Things I struggled with

The sheer amount of work when writing tests for puts and user input.

Stubbing a test to check totals on #confirm_order in class Takeaway

Why a heredoc wouldn't work in my "it should display menu" test

Binary file added diagramming_takewaway.odt
Binary file not shown.
83 changes: 83 additions & 0 deletions instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
Takeaway Challenge
==================
```
_________
r== | |
_ // | M.A. | ))))
|_)//(''''': | |
// \_____:_____.-------D )))))
// | === | / \
.:'//. \ \=| \ / .:'':./ )))))
:' // ': \ \ ''..'--:'-.. ':
'. '' .' \:.....:--'.-'' .'
':..:' ':..:'

```

Instructions
-------

* Feel free to use google, your notes, books, etc. but work on your own
* If you refer to the solution of another coach or student, please put a link to that in your README
* If you have a partial solution, **still check in a partial solution**
* You must submit a pull request to this repo with your code by 9am Monday morning

Task
-----

* Fork this repo
* Run the command 'bundle' in the project directory to ensure you have all the gems
* Write a Takeaway program with the following user stories:

```
As a customer
So that I can check if I want to order something
I would like to see a list of dishes with prices

As a customer
So that I can order the meal I want
I would like to be able to select some number of several available dishes

As a customer
So that I can verify that my order is correct
I would like to check that the total I have been given matches the sum of the various dishes in my order

As a customer
So that I am reassured that my order will be delivered on time
I would like to receive a text such as "Thank you! Your order was placed and will be delivered before 18:52" after I have ordered
```

* Hints on functionality to implement:
* Ensure you have a list of dishes with prices
* The text should state that the order was placed successfully and that it will be delivered 1 hour from now, e.g. "Thank you! Your order was placed and will be delivered before 18:52".
* The text sending functionality should be implemented using Twilio API. You'll need to register for it. It’s free.
* Use the twilio-ruby gem to access the API
* Use the Gemfile to manage your gems
* Make sure that your Takeaway is thoroughly tested and that you use mocks and/or stubs, as necessary to not to send texts when your tests are run
* However, if your Takeaway is loaded into IRB and the order is placed, the text should actually be sent
* Note that you can only send texts in the same country as you have your account. I.e. if you have a UK account you can only send to UK numbers.

* Advanced! (have a go if you're feeling adventurous):
* Implement the ability to place orders via text message.

* A free account on Twilio will only allow you to send texts to "verified" numbers. Use your mobile phone number, don't worry about the customer's mobile phone.

> :warning: **WARNING:** think twice before you push your **mobile number** or **Twilio API Key** to a public space like GitHub :eyes:
>
> :key: Now is a great time to think about security and how you can keep your private information secret. You might want to explore environment variables.

* Finally submit a pull request before Monday at 9am with your solution or partial solution. However much or little amount of code you wrote please please please submit a pull request before Monday at 9am


In code review we'll be hoping to see:

* All tests passing
* High [Test coverage](https://github.com/makersacademy/course/blob/main/pills/test_coverage.md) (>95% is good)
* The code is elegant: every class has a clear responsibility, methods are short etc.

Reviewers will potentially be using this [code review rubric](docs/review.md). Referring to this rubric in advance will make the challenge somewhat easier. You should be the judge of how much challenge you want this at this moment.

Notes on Test Coverage
------------------

You can see your [test coverage](https://github.com/makersacademy/course/blob/main/pills/test_coverage.md) when you run your tests.
37 changes: 37 additions & 0 deletions lib/menu.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class Menu

def initialize(input: $stdin, output: $stdout)
@input = input
@output = output
@dishes = [{ food: :Chips, price: 1 },
{ food: :Tofu, price: 2 }, { food: :Broccoli, price: 1 },
Copy link

Choose a reason for hiding this comment

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

Minor one here - it's probably better to leave the dish names as strings rather than symbols, as symbols are mostly used for keys, special configuration values, enumerated values, etc, rather than purely "textual" values

{ food: :Ice_cream, price: 2 }]
end

def show
@dishes.each_with_index do |item, index|
@output.puts "#{index + 1}. #{item[:food]} - £#{item[:price]}"
end
end

def check(dish)
the_dish = dish.to_sym
@dishes.each do |dish|
if the_dish == dish[:food]

Choose a reason for hiding this comment

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

I feel this could be simplified to a ternary operator

ie 'the_dish == dish[:food] ? true : false

Copy link

Choose a reason for hiding this comment

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

Maybe this method could also be renamed to something like contains_dish? so it's more explicit on what it does

return true
end
end
return false
end

def price(dish)
the_dish = dish.to_sym
@dishes.each do |dish|
if the_dish == dish[:food]
return dish[:price]
end
end
return false
end

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

class Takeaway
attr_reader :menu, :summary

def initialize(input: $stdin, output: $stdout)
@input = input
@output = output
@menu = Menu.new

Choose a reason for hiding this comment

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

I'm impressed you definitely know what you're doing, especially with the corresponding tests

I'm no expert in dependency injection, but this article near the beginning

https://medium.com/@Bakku1505/introduction-to-dependency-injection-in-ruby-dc238655a278

explains how linking the classes together so "strongly" ie 'def initialize... @menu = Menu.new' can cause issues down the line

@dishes = []
@summary = []
@total = 0
end

def show_menu
@menu.show
end

def order
loop do
Copy link

Choose a reason for hiding this comment

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

Nice that you've worked with user input as well! I'd suggest looking at extracting further this logic into a new class, maybe named Cli or Interface, which would contain methods such as clarify_order or ask_for_order. It could probably also handle the display of the menu (which is right now in the Menu class). This way you can make the main Takeaway and Menu classes do a bit less, and it also makes it easier to test the input/output, as there will be only one class that directly uses puts or gets, so you remove the need to mock these for the other classes

dish = ask_for_order
return confirm_order if dish == ""
if menu.check(dish)
quantity = ask_for_quantity
log_order(dish, quantity)
else
clarify_order
end
end
end

def confirm_order
@summary.each do |dish|
Copy link

Choose a reason for hiding this comment

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

This could also be part of the new class mentioned above - usually it's good practice to split the code that deals with "presenting" or display, from the code that is dealing with pure data or logic. This way, it's easier to change things related to how looks the program outputs (think formatting, translating, or more complex things!) without touching at the core logic of the program, which stays the same no matter how the output is presented and how the input is provided

@output.puts "#{dish[:quantity]} order of #{dish[:food]} at £#{menu.price(dish[:food])} each"
@total += menu.price(dish[:food])
end
@output.puts "Total order is £#{@total}"
end

private


def ask_for_order
@output.puts <<~ORDER
Please type each dish you require followed by return.
When you have finished your order press return twice.
ORDER
dish = @input.gets.chomp
end

def clarify_order
@output.puts "Sorry, we don't have that dish - perhaps you've made a spelling mistake?"
end

def ask_for_quantity
@output.puts "how many do you want?"
quantity = @input.gets.to_i
end

def log_order(dish, quantity)
order_item = {}
order_item[:food] = dish
order_item[:quantity] = quantity
@summary << order_item
end

Choose a reason for hiding this comment

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

Really impressive thought about actual user interface, certainly going beyond what is required. You have my sympathy and respect for keeping your head around it though

end
32 changes: 32 additions & 0 deletions spec/feature_test_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'takeaway'

describe 'Featuretest' do
context '#order' do
it 'will ask again if item mispelt' do
output = place_order_with_input("Brocoli", "Broccoli")
Copy link

Choose a reason for hiding this comment

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

Nice one! This test suite should probably contain a bit more tests, at least one testing the whole integration of the different components with a successful order process, and assert the output of the takeaway.order method


expect(output).to eq <<~OUTPUT
Please type each dish you require followed by return.
When you have finished your order press return twice.
Sorry, we don't have that dish - perhaps you've made a spelling mistake?
Please type each dish you require followed by return.
When you have finished your order press return twice.
how many do you want?
Please type each dish you require followed by return.
When you have finished your order press return twice.
0 order of Broccoli at £1 each
Total order is £1
OUTPUT
end
end

def place_order_with_input(*order)
input = StringIO.new(order.join("\n") + "\n" + "\n" + "\n")
Copy link

Choose a reason for hiding this comment

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

Really nice way to mock IO!

output = StringIO.new

takeaway = Takeaway.new(input: input, output: output)
takeaway.order

output.string
end
end
35 changes: 35 additions & 0 deletions spec/menu_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require 'menu'

describe Menu do

context '#show' do
it 'should display menu' do
# output = <<~MENU
Copy link

Choose a reason for hiding this comment

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

I reckon this didn't work because the heredoc syntax needs the "ending" tag (in your case MENU) to be on its own line, at the very beginning of the line (without any blank space before). This would make it:

it 'should display menu' do
  output = <<~MENU
       1. Chips - £1
       2. Tofu - £2
       3. Broccoli - £1
       4. Ice_cream - £2 
MENU

   # ...
end

# 1. Chips - £1
# 2. Tofu - £2
# 3. Broccoli - £1
# 4. Ice_cream - £2
# MENU
# expect { subject.show }.to output(output).to_stdout

expect { subject.show }.to output("1. Chips - £1\n2. Tofu - £2\n3. Broccoli - £1\n4. Ice_cream - £2\n").to_stdout
end
end

context '#check' do
it 'dish against menu and return false' do
wrong_spelling = "Brocoli"
expect(subject.check(wrong_spelling)).to eq false
end
it 'dish against menu and return true' do
right_spelling = "Broccoli"
expect(subject.check(right_spelling)).to eq true
end
end

Choose a reason for hiding this comment

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

your general ruby syntax and string-morphing is fantastic!

context '#price' do
it 'returns the price of an item' do
expect(subject.price("Broccoli")).to eq 1
end
end
end
Loading