Last active
December 28, 2015 18:59
-
-
Save HeroicEric/7547151 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'pry' | |
def good_input?(input) | |
if !(input =~ /^(\d*)\.\d{2}$/) | |
return false | |
end | |
true | |
end | |
puts "Welcome to James' coffee emporium!" | |
puts "Please choose your selection:" | |
puts "1) Add item - $5.00 - Light Bag" | |
puts "2) Add item - $7.50 - Medium Bag" | |
puts "3) Add item - $9.75 - Bold Bag" | |
puts "4) Complete Sale" | |
# price = { | |
# 1 => 5.00, | |
# 2 => 7.50, | |
# 3 => 9.75, | |
# 4 => 4 | |
# } | |
# Instead, create a hash where each product is represented by another hash. | |
# This will let us access the type and price again later when we need to. | |
items = { | |
1 => { style: 'light', price: 5.00 }, | |
2 => { style: 'medium', price: 7.50 }, | |
3 => { style: 'dark', price: 9.75 } | |
} | |
selection = 0 | |
subtotal = 0 | |
# We'll use this below in order to store all of the individual transactions | |
transactions = [] | |
while selection != 4 | |
puts "\n" + "Makes a selection:" | |
print "> " | |
# Use the new items hash to find selected item | |
selection = gets.chomp.to_i | |
# Break out of the loop if somebody chooses 4 | |
break if selection == 4 | |
item = items[selection] | |
puts "How many bags?" | |
print "> " | |
# I think renaming this to quantity better describes the value | |
quantity = gets.chomp | |
# This will give us the total for the current transaction | |
transaction_total = quantity.to_f * item[:price].to_f | |
# We can store the recorded transaction by making another hash that has | |
# values that will let us which item was purchased and how much of it. | |
transaction = { item: item, quantity: quantity, total: transaction_total } | |
# Here we add the transaction that we just created to our array that contains | |
# all of the transactions that we've created. | |
# | |
# We can now reference all of the transactions whenever we want to calculate | |
# the current subtotal or list all of the transactions. | |
transactions << transaction | |
# We can use the transaction_total here to add to our overall subtotal | |
subtotal += transaction_total | |
puts "\n" + "Subtotal: $#{sprintf( "%0.02f", subtotal)}" | |
end | |
puts "\n" + "===Sale Complete===" | |
puts | |
# Here we can loop through all of the transactions and print out the | |
# information that we're looking for because it's stored in each transaction. | |
transactions.each do |transaction| | |
puts "#{transaction[:total]} - #{transaction[:quantity]} #{transaction[:item][:style]}" | |
end | |
puts | |
puts "Total: $#{sprintf( "%0.02f", subtotal)}" | |
puts | |
puts "What is the amount tendered?" | |
print "> $" | |
paid = gets.chomp.to_f | |
puts | |
change_due = paid - subtotal | |
if paid.to_f < subtotal.to_f | |
due = (subtotal.to_f - paid.to_f) | |
puts "Excuse me, you still owe $#{sprintf( "%0.02f", due)}" | |
else | |
change = paid.to_f - subtotal.to_f | |
puts "===Thank You!===" | |
puts "Change due is $#{sprintf( "%0.02f", change_due)}" | |
puts | |
puts "\n" + Time.now.strftime( "%m/%d/%Y %I:%M%p" ) | |
puts "====================" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment