Skip to content

Instantly share code, notes, and snippets.

@faizaanshamsi
Created November 19, 2013 00:58
Show Gist options
  • Save faizaanshamsi/7538328 to your computer and use it in GitHub Desktop.
Save faizaanshamsi/7538328 to your computer and use it in GitHub Desktop.
Cash Register, displays menu and allows products to be rung in. Currently requires input validation
def menu(hash)
i = 1
hash.each do |k, v|
puts "#{i}) Add item - $#{decimal_output(v[0])} - #{v[1]}"
i += 1
end
puts "#{i}) Complete Sale" , ""
end
def prompt(string)
puts string
gets.chomp.to_i
end
def line_totals(hash, quantity, selection)
hash[selection][2] += quantity
end
def subtotal_display(items)
items.length.times do |i|
puts "$#{decimal_output(items[i+1][2] * items[i + 1][0])} - #{items[i + 1][2]} #{items[i + 1][1]}"
end
end
def decimal_output(num)
return (sprintf('%.2f', num))
end
items = {
1 => [5.00, "Light Bag", 0],
2 => [7.50, "Medium Bag", 0],
3 => [9.75, "Bold Bag", 0]
}
selection = 0
subtotal = 0
tender = 0
menu(items)
loop do
selection = prompt("Make a selection:")
break if selection == "#{items.length + 1}".to_i
quantity = prompt("How many bags:")
subtotal += (quantity * items[selection][0])
puts "Subtotal: #{decimal_output(subtotal)}"
line_totals(items, quantity, selection)
end
puts "===Sale Complete==="
subtotal_display(items)
puts "Total: $#{decimal_output(subtotal)}"
until tender >= subtotal
payment = prompt("What is the amount tendered?")
tender += payment
if tender < subtotal
puts "You owe me $#{decimal_output(subtotal - tender)} more!"
else
change_due = tender - subtotal
end
end
puts "===Thank You!==="
puts "The total change due is $#{decimal_output(change_due)}"
puts Time.now.strftime("%m/%d/%Y %I:%M%p" )
#User Stories:
#As a Cashier
#I want to see the menu
#So that I can input the correct item price or complete the sale
#As a Cashier
#I want to input quantity of items purchased
#So that I can calculate the total
#As a Cashier
#I want to see a bill
#So that I charge the customer
#As a Cashier
#I want to know how much change to give
#So I can finish transaction with a zero balance
#Acceptance Criteria
# Input must be valid
# Input options will be seen by cashier
# Item and quantity will be input by cashier
# Final item and quantity subtotals must presented when requested by Cashier
# Change due will be calculated for Cashier
# Change due may not be negative
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment