Skip to content

Instantly share code, notes, and snippets.

@alacritythief
Created August 15, 2014 21:05
Show Gist options
  • Save alacritythief/db222ac48ca31b916e06 to your computer and use it in GitHub Desktop.
Save alacritythief/db222ac48ca31b916e06 to your computer and use it in GitHub Desktop.
#### CASH 3 ####
require 'csv'
require 'pry'
# Commands
def sku(array, num)
array[num-1][0]
end
def name(array, num)
array[num-1][1]
end
def wprice(array, num)
array[num-1][2].to_f
end
def rprice(array, num)
array[num-1][3].to_f
end
# Import the CSV and convert to an array
def csv_import(array)
CSV.foreach('products.csv', headers: true) do |row|
array << row
end
end
# Export the CSV
def csv_export(data)
CSV.open("salesreport.csv", "w") do |csv|
data.each do |k, v|
csv << [k, v]
end
end
end
def greeting
puts "=================================="
puts "Welcome to James' Coffee Emporium!"
puts "=================================="
end
def currency(money)
sprintf('%.2f', money)
end
def current_time
Time.now.strftime("%m/%d/%Y %l:%M%p")
end
def show_menu(menu)
for i in 1..3
puts "#{i}) Add item - $#{currency(rprice(menu, i))} - #{name(menu, i)}"
end
puts "4) Complete Sale"
end
def prompt(output)
puts "\n#{output}"
print "> "
input = gets.chomp
end
def user_selection(m, sales_inv, total)
light = 0
medium = 0
bold = 0
choice = prompt("Make a selection:").to_i
while choice < 4
quantity = prompt("How many?").to_i
total += rprice(m, choice) * quantity
puts "\nSubtotal: $#{currency(total)}"
if choice == 1
light += quantity
elsif choice == 2
medium += quantity
elsif choice == 3
bold += quantity
end
choice = prompt("Make a selection:").to_i
end
puts "\n===== Sale Complete! ====="
if light > 0
puts "$#{currency(rprice(m,1)*light)} - #{light} Light"
sales_inv << [sku(m,1), light]
end
if medium > 0
puts "$#{currency(rprice(m,2)*medium)} - #{medium} Medium"
sales_inv << [sku(m,2), medium]
end
if bold > 0
puts "$#{currency(rprice(m,3)*bold)} - #{bold} Bold"
sales_inv << [sku(m,3), bold]
end
puts "==========================="
return total
end
def due(total_due)
puts "\nThe total amount due is $#{currency(total_due)}"
puts "\nWhat is the amount tendered?"
print "$ "
end
def due(total_due)
puts "\nThe total amount due is $#{currency(total_due)}"
puts "\nWhat is the amount tendered?"
print "$ "
end
def user_pays
pay = gets.to_f
end
def current_time
Time.now.strftime("%m/%d/%Y %l:%M%p")
end
def tender(tender, final, sales_inv)
if tender == final
puts "\n========== Thank You! ============"
puts current_time
puts "============ PAID ================"
csv_export(sales_inv)
else
change = tender - final
result = currency(change.abs)
if change > 0
puts "\n========== Thank You! ============"
puts "The total change due is $#{result}"
puts current_time
puts "============= PAID ==============="
csv_export(sales_inv)
else
puts "\n==================================================="
puts " WARNING: Customer still owes $#{result}! Exiting..."
puts "===================================================\n"
end
end
end
total = 0
sales_inv = [] # CSV output array
m = [] # Menu Array
inventory = [] # Shopping Cart
csv_import(m)
greeting
show_menu(m)
total_due = user_selection(m, sales_inv, total)
due(total_due)
amt_tnd = user_pays
tender(amt_tnd, total_due, sales_inv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment