From 058942d5d5d0b8f6c135e1dff6da2228c848f2dc Mon Sep 17 00:00:00 2001 From: Henry Tam Date: Fri, 24 Oct 2014 13:51:58 -0700 Subject: [PATCH 1/2] Sandwich --- Sandwich/menu.csv | 4 +- Sandwich/sandwich.rb | 150 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 146 insertions(+), 8 deletions(-) diff --git a/Sandwich/menu.csv b/Sandwich/menu.csv index b3053bd..aa47f4d 100644 --- a/Sandwich/menu.csv +++ b/Sandwich/menu.csv @@ -1,7 +1,7 @@ "id","name","description","price" "1","Smoked Salmon Sandwich","Atlantic smoked salmon with cream cheese, cucumber, tomatoes, red onions and capers on a Telera roll","7.95" -"2","Pan Bagnat Sanwich","Tuna salad topped with iceberg lettuce, tomatoes, red bell peppers, cucumber, olive tapenade, boiled egg and fresno chilies on a multigrain hero roll","8.95" -"3","Over Roasted Turkey Sandwich","Diestel Farms oven roasted turkey breast with chipotle aioli, iceberg lettuce, tomatoes, avocado, bacon, and pepper jack cheese on an Italian country roll","8.95" +"2","Pan Bagnat Sandwich","Tuna salad topped with iceberg lettuce, tomatoes, red bell peppers, cucumber, olive tapenade, boiled egg and fresno chilies on a multigrain hero roll","8.95" +"3","Oven Roasted Turkey Sandwich","Diestel Farms oven roasted turkey breast with chipotle aioli, iceberg lettuce, tomatoes, avocado, bacon, and pepper jack cheese on an Italian country roll","8.95" "4","Baltimore Beef Sandwich","Roast beef with horseradish cream, pickled red onions, tomatoes and swiss cheese on a Poppy seed roll","8.95" "5","Pastrami Sandiwch","Pastrami with cole slaw, pepperoncini and russian dressing on a country Italian roll","8.95" "6","Bacon Cobb Sandwich","The classic BLT with avocado vrema, boiled egg and topped with crumbled blue cheese on a Telera roll","8.95" diff --git a/Sandwich/sandwich.rb b/Sandwich/sandwich.rb index 344bfec..b03a622 100644 --- a/Sandwich/sandwich.rb +++ b/Sandwich/sandwich.rb @@ -1,4 +1,6 @@ require 'csv' +require 'rubygems' +require 'twilio-ruby' class Sandwich attr_reader :id, :price, :name, :description @@ -7,25 +9,150 @@ def initialize(args = {}) @name = args[:name] @description = args[:description] @price = args[:price] + end end class Controller - attr_reader :sandwich_list + attr_reader :sandwich_list, :twilio_message def initialize @sandwich_list = [] @view=View.new CSV.foreach('menu.csv', headers: true, header_converters: :symbol) do |row| @sandwich_list << Sandwich.new(row) end + @exit = false + @cart = [] + @twilio_message = "" + end + + def drive + display_menu + while @exit != true + options + decision + end end + def display_menu - @view.render_menu(sandwich_list) + @view.render_menu(@sandwich_list) + end + + def display_cart_remove + puts "YOUR CART" + @cart.each do |sandwich| + puts "#{sandwich.id} #{sandwich.name} #{sandwich.price}" + end + end + + def display_cart + puts "YOUR CART" + @cart.each do |sandwich| + puts " - #{sandwich.name} #{sandwich.price}" + end + p "Total: #{sum}" end + + def sum + sum = 0 + @cart.each do |sandwich| + sum += sandwich.price.to_f + end + sum.round(2) + end + def request_order @view.render_order(sandwich_list) end + + def options + @view.render_options + end + + def add_sandwich + @view.render_menu(sandwich_list) + sandwich_name = @view.render_order(sandwich_list) + @sandwich_list.each do |sandwich| + if sandwich.name == sandwich_name + @cart << sandwich + end + end + end + + def remove_sandwich + target = "YOU!" + display_cart_remove + puts "What number sandwich would you like to remove?" + sandwich_id = gets.chomp + @cart.each do |sandwich| + if sandwich.id == sandwich_id + target = sandwich + end + end + @cart.delete_at(@cart.find_index(target)) + end + + def prepare_order + sandwich_cart_hash = {} + @cart.each do |sandwich| + if sandwich_cart_hash.has_key?(sandwich.name) + sandwich_cart_hash[sandwich.name] += 1 + else + sandwich_cart_hash[sandwich.name] = 1 + end + end + sandwich_cart_hash + end + + def place_order + sandwich_cart_hash = prepare_order + sandwich_cart_hash.each do |k,v| + @twilio_message += "\n#{v}: #{k}" + end + @twilio_message + end + + def twilio_message + account_sid = "AC27bf9c7c50adfe10a7c0e4660e3e61cd" + auth_token = "02ecd2a115226c4d817297984e3d1a05" + client = Twilio::REST::Client.new account_sid, auth_token + from = "+16506845053" + + friends = { + "+16505801483" => "Julian", + "+17034709608" => "Kevin", + # # "+16506363688" => "Henry", + #"+19098019741" => "Ryan" + } + friends.each do |key, value| + client.account.messages.create( + :from => from, + :to => key, + :body => @twilio_message + ) + puts "Sent message to #{value}" + end + end + + def decision + choice = gets.chomp.downcase + if choice == 'a' || choice == 'add' + add_sandwich + elsif choice == 'exit' || choice == 'end' + @exit = true + elsif choice == 'm' || choice == 'menu' + display_menu + elsif choice == 'r' || choice == 'remove' + remove_sandwich + elsif choice == 's' || choice == 'show' + display_cart + elsif choice == 'o' || choice == 'order' + place_order + twilio_message + end + end + end class View @@ -44,11 +171,22 @@ def render_order(sandwich_list) sandwich_order = gets.chomp.to_i sandwich_name = sandwich_list[sandwich_order - 1].name puts "You selected #{sandwich_name}" + return sandwich_name end -end -while + def render_options + puts + puts '-'*100 + puts + puts "Enter one of the following commands to perform blsdf" + puts "'Add' - to add another sandwich to the list" + puts "'Remove' - to remove a sandwich fromt the list" + puts "'Show' - to show all the sandwiches in the cart" + puts "'Order' - to place the order" + end +end menu = Controller.new -menu.display_menu -menu.request_order +menu.drive + + From fc45e5aa34d3dd8df967fd1535044a01700ec1c4 Mon Sep 17 00:00:00 2001 From: Henry Tam Date: Fri, 24 Oct 2014 16:20:41 -0700 Subject: [PATCH 2/2] Sandwich --- Sandwich/sandwich.rb | 51 +++++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/Sandwich/sandwich.rb b/Sandwich/sandwich.rb index b03a622..a9256be 100644 --- a/Sandwich/sandwich.rb +++ b/Sandwich/sandwich.rb @@ -9,7 +9,6 @@ def initialize(args = {}) @name = args[:name] @description = args[:description] @price = args[:price] - end end @@ -40,18 +39,18 @@ def display_menu end def display_cart_remove - puts "YOUR CART" + @view.render("YOUR CART") @cart.each do |sandwich| - puts "#{sandwich.id} #{sandwich.name} #{sandwich.price}" + @view.render("#{sandwich.id} #{sandwich.name} #{sandwich.price}") end end def display_cart - puts "YOUR CART" + @view.render("YOUR CART") @cart.each do |sandwich| - puts " - #{sandwich.name} #{sandwich.price}" + @view.render(" - #{sandwich.name} #{sandwich.price}") end - p "Total: #{sum}" + @view.render("Total: #{sum}") end def sum @@ -81,9 +80,9 @@ def add_sandwich end def remove_sandwich - target = "YOU!" + target = "" display_cart_remove - puts "What number sandwich would you like to remove?" + @view.render("What number sandwich would you like to remove?") sandwich_id = gets.chomp @cart.each do |sandwich| if sandwich.id == sandwich_id @@ -122,32 +121,37 @@ def twilio_message friends = { "+16505801483" => "Julian", "+17034709608" => "Kevin", + ## Store owner will add more customers/friends here # # "+16506363688" => "Henry", #"+19098019741" => "Ryan" } - friends.each do |key, value| - client.account.messages.create( - :from => from, - :to => key, - :body => @twilio_message - ) - puts "Sent message to #{value}" + unless @twilio_message.empty? + friends.each do |key, value| + client.account.messages.create( + :from => from, + :to => key, + :body => @twilio_message + ) + puts "Sent message to #{value}" + end end + @view.render("You must place a sandwich in your cart") if @twilio_message.empty? end def decision choice = gets.chomp.downcase - if choice == 'a' || choice == 'add' + case choice + when 'add' add_sandwich - elsif choice == 'exit' || choice == 'end' + when 'exit','end' @exit = true - elsif choice == 'm' || choice == 'menu' + when 'm','menu' display_menu - elsif choice == 'r' || choice == 'remove' + when 'r','remove' remove_sandwich - elsif choice == 's' || choice == 'show' + when 's','show' display_cart - elsif choice == 'o' || choice == 'order' + when 'o','order' place_order twilio_message end @@ -173,7 +177,6 @@ def render_order(sandwich_list) puts "You selected #{sandwich_name}" return sandwich_name end - def render_options puts puts '-'*100 @@ -183,6 +186,10 @@ def render_options puts "'Remove' - to remove a sandwich fromt the list" puts "'Show' - to show all the sandwiches in the cart" puts "'Order' - to place the order" + puts "'Exit' - to place the exit" + end + def render(customized_message) + puts "#{customized_message}" end end