Skip to content

Instantly share code, notes, and snippets.

@foglabs
Created August 14, 2014 21:25
Show Gist options
  • Save foglabs/dbc0b9c91dd8109ac3f9 to your computer and use it in GitHub Desktop.
Save foglabs/dbc0b9c91dd8109ac3f9 to your computer and use it in GitHub Desktop.
Cash Register III
require 'pry'
require 'csv'
def calc_subtotal(menu, order)
runtotal = 0
order.each do |sku, qty|
subtotal_item = menu.find {|item| item["SKU"] == sku}
runtotal += subtotal_item["retail_price"] * qty
end
runtotal
end
def print_receipt(menu, order, subtotal)
time = Time.now
puts "HEY THANKS
==============\n
Your items are itemized in the item-list below, items:\n
==============\n"
order.each do |sku, quantz|
subtotal_item = menu.find {|item| item["SKU"] == sku}
puts "#{quantz} #{subtotal_item["name"]} -- $#{'%.2f' %((subtotal_item["retail_price"])*(quantz.to_f))}"
end
puts "Please enter the cash tendered:"
cash = gets.chomp.to_f
if subtotal > cash
puts "\n\nThat's not gonna cut it! You're short by $#{'%.2f' %(subtotal-cash)}."
else
puts "\n\nYour change is $#{'%.2f' %(cash-subtotal)}\n\n
===========\n
Thanks yo!\n
===========\n
#{time.hour}:#{time.min} #{time.month}-#{time.day}-#{time.year}"
end
end
qty = 0
menu_select = 0
subtotal = 0
runtotal = 0
cash = 0
menu = []
menu_print_counter = 0
order = {}
CSV.foreach('products.csv', headers: true) do |item|
item["retail_price"] = item["retail_price"].to_f
menu << item
end
puts "Welcome to James' Coffee Emporium!!!\n\n"
menu.each do |menu_element|
puts "#{menu_print_counter + 1}) Add item - #{menu_element["name"]} - #{'%.2f' %(menu_element["retail_price"])}"
menu_print_counter += 1
end
puts "#{menu_print_counter+1}) Complete Your Order\n"
while true
puts "Make a selection yo: "
menu_select = gets.chomp.to_i
if menu_select == (menu_print_counter+1)
break
end
puts "How many?"
qty = gets.chomp.to_i
if(menu_select != 0 && qty != 0)
item = menu[menu_select-1]
sku = item["SKU"]
oldqty = order[sku] || 0
order[sku] = oldqty + qty
subtotal = calc_subtotal(menu, order)
else
puts "*** Bad input, so bad!! ***"
end
puts "Subtotal: $#{'%.2f' %(subtotal)}"
end
print_receipt(menu, order, subtotal)
File.open('invoice.csv', 'w') do |f|
f.puts 'SKU,Quantity'
order.each do |sku, qty|
f.puts "#{sku},#{qty}"
end
#order -> hash with {menu[][1] (item #+1) => qty}
#menu -> 0 is sku, 1 is name, 2 is price
# csv gets SKU - QTY\n
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment