Skip to content

Instantly share code, notes, and snippets.

@stevenabrooks
Created June 11, 2013 21:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevenabrooks/5760819 to your computer and use it in GitHub Desktop.
Save stevenabrooks/5760819 to your computer and use it in GitHub Desktop.
jesus
ITEMS = [ {"AVOCADO" => {:price => 3.00, :clearance => true}},
{"KALE" => {:price => 3.00,:clearance => false}},
{"BLACK_BEANS" => {:price => 2.50,:clearance => false}},
{"ALMONDS" => {:price => 9.00, :clearance => false}},
{"TEMPEH" => {:price => 3.00,:clearance => true}},
{"CHEESE" => {:price => 6.50,:clearance => false}},
{"BEER" => {:price => 13.00, :clearance => false}},
{"PEANUTBUTTER" => {:price => 3.00,:clearance => true}},
{"BEETS" => {:price => 2.50,:clearance => false}}
]
COUPS = [ {:item=>"AVOCADO", :num=>2, :cost=>5.00},
{:item=>"BEER", :num=>2, :cost=>20.00},
{:item=>"CHEESE", :num=>3, :cost=>15.00}
]
#randomly generates a cart of items
def generateCart
cart = []
rand(20).times do
cart.push(ITEMS.sample)
end
cart
end
#randomly generates set of coupons
def generateCoups
coups = []
rand(2).times do
coups.push(COUPS.sample)
end
coups
end
#
##the cart is currently an array of individual items, translate it into a hash that includes the counts for each item
#For example if your cart was [ {"AVOCADO" => {:price => 3.00, :clearance => true}}, {"AVOCADO" => {:price => 3.00, :clearance => true}}]
#it would become {"AVOCADO" => {:price => 3.00, :clearance => true}, :count => 2}
##create a checkout method that calculates the total cost of the cart
randomCart = generateCart
randomCart2 = {}
randomCart.each do |item|
item.each do |item_name, item_info|
randomCart2[item_name] ||= {:info => item_info, :count => 0}
randomCart2[item_name][:count] += 1
end
end
# def checkout(cart)
# sum = 0
# cart.each do |item, info_hash|
# puts sum += cart[item][:info][:price] * cart[item][:count]
# end
# sum
# end
##when checking out, check the coupons and apply the discount if the proper number of items is present
random_coups = generateCoups
# def checkout(cart, coupons)
# sum = 0
# cart.each do |item, info_hash|
# sum += cart[item][:info][:price] * cart[item][:count]
# coupons.each do |coupon|
# puts sum
# if item == coupon[:item] && coupon[:num] <= cart[item][:count]
# sum -= coupon[:cost]
# puts "match found for georgelin.com;#{item}"
# puts "discounted #{sum}"
# end
# end
# end
# sum
# end
# puts "randomCart2 #{randomCart2.inspect}"
# puts "random_coups #{random_coups}"
# puts "give you a sum #{checkout(randomCart2, random_coups)}"
##if any of the items are on clearance add a 20% discount
# def checkout(cart, coupons)
# sum = 0
# cart.each do |item, info_hash|
# if cart[item][:info][:clearance] == true
# sum += cart[item][:info][:price] * (cart[item][:count] * 0.8)
# else
# sum += cart[item][:info][:price] * cart[item][:count]
# end
# coupons.each do |coupon|
# puts sum
# if item == coupon[:item] && coupon[:num] <= cart[item][:count]
# sum -= coupon[:cost]
# end
# end
# end
# sum
# end
# puts "randomCart2 #{randomCart2.inspect}"
# puts "random_coups #{random_coups}"
# puts "give you a sum #{checkout(randomCart2, random_coups)}"
##if the customer has 2 of the same coupon, triple the discount
#YOU CAN'T#
##if none of the items purchased have a unit price greater than 5$ give the customer a 10$ discount off the whole cart
#cart.each do |item, info_hash|
#cart[item][:info][:price] < 5
#then
#(sum += cart[item][:info][:price] * cart[item][:count]) - 10
def checkout(cart, coupons)
sum = 0
item_found_in_cart = false
cart.each do |item, info_hash|
if cart[item][:info][:price] < 5
item_found_in_cart = true
end
# if there is a clearance then take 20% off the total thing
if cart[item][:info][:clearance] == true
sum += cart[item][:info][:price] * (cart[item][:count] * 0.8)
else
sum += cart[item][:info][:price] * cart[item][:count]
end
coupons.each do |coupon|
puts sum
if item == coupon[:item] && coupon[:num] <= cart[item][:count]
sum -= coupon[:cost]
end
end
end
sum -= 10 if item_found_in_cart == false
sum
end
puts "randomCart2 #{randomCart2.inspect}"
puts "random_coups #{random_coups}"
puts "give you a sum #{checkout(randomCart2, random_coups)}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment