Created
June 12, 2013 03:53
-
-
Save surrealdetective/5762721 to your computer and use it in GitHub Desktop.
green grocer
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
#create hash from array (a method) | |
#parameters: an array of nested hashes | |
#input | |
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}}] | |
def generateCart(items) | |
cart = [] | |
rand(20).times do | |
cart.push(items.sample) | |
end | |
cart | |
end | |
cart1 = generateCart(items) | |
# puts cart1.inspect | |
def clarifyCart(cart) | |
cart1array = [] | |
cart.each do |food_info| | |
#{"KALE"=>{:price=>3.0, :clearance=>false}, :count=>0} | |
#puts food_info.inspect | |
#grab "KALE" and push it to cart1array | |
food_info.each do |ingredient_name, value| | |
cart1array << ingredient_name | |
end | |
end | |
cart1array | |
end | |
cart1list = clarifyCart(cart1) | |
# puts cart1list.inspect | |
# Should create the items list with a :count key => value pair | |
# | |
def add_count_to_items(items) | |
items.each do |food_info| | |
food_info[:count] = 0 | |
end | |
end | |
new_items = add_count_to_items(items) | |
def tally (list, new_inventory) | |
list.each do |ingredient| | |
new_inventory.each do |ingredient_info| | |
if ingredient_info.key?(ingredient) | |
ingredient_info[:count] += 1 | |
end | |
end | |
end | |
end | |
tally(cart1list, new_items) | |
# new_inventory.each do | | |
# if ingredient == new_inventory[0][ingredient] | |
# puts | |
#puts new_items.inspect | |
#-------------- | |
# COUPS = [ {:item=>"AVOCADO", :num=>2, :cost=>5.00}, | |
# {:item=>"BEER", :num=>2, :cost=>20.00}, | |
# {:item=>"CHEESE", :num=>3, :cost=>15.00}] | |
#it should look at each | |
#ingrediient_info is the items list with a :count | |
coupons = [ {:item=>"AVOCADO", :num=>2, :cost=>5.00}, | |
{:item=>"BEER", :num=>2, :cost=>20.00}, | |
{:item=>"CHEESE", :num=>3, :cost=>15.00}] | |
def generateCoups(coupons) | |
coups = [] | |
rand(2).times do | |
coups.push(coupons.sample) | |
end | |
coups | |
end | |
my_coupons = generateCoups(coupons) | |
unless my_coupons.empty? | |
my_coupons = my_coupons[0].flatten | |
end | |
# puts my_coupons | |
# puts my_coupons.inspect | |
def checkout(pricelist, my_coupons) | |
total_cost=0 | |
allarray = [] | |
pricelist.each do |ingredient_info| | |
ingredient_info_array = ingredient_info.flatten | |
#["AVOCADO", {:price=>3.0, :clearance=>true}, :count, 3] | |
quantity = ingredient_info_array[3] | |
price = ingredient_info_array[1][:price] | |
unless my_coupons.empty? | |
coupon_amount = my_coupons[3] | |
coupon_total_price = my_coupons[5] | |
if quantity >= coupon_amount | |
ingredient_price_sum = (quantity - coupon_amount)*price + coupon_total_price | |
#the price set below is the equivalent price if the coupon were applied to all items evenly | |
price = ingredient_price_sum / quantity | |
end | |
#[:item, "BEER", :num, 2, :cost, 20.0] | |
end | |
#formula for estimating cost | |
#if quantity >= whatever | |
#ingredient_price_sum = (Original Quantity - coupon quantity)*org_price + (coupon_quantity * coupon_price) | |
#equivalent_price = total_sum / original_quantity | |
if ingredient_info_array[1][:clearance] | |
price = price * 0.80 | |
end | |
total_cost += price * quantity | |
end | |
total_cost | |
end | |
total_price = checkout(new_items, my_coupons) | |
puts "The amount due is $#{total_price}." | |
puts "You bought the following items:" | |
cart1list.each do |bought_item| | |
puts "#{bought_item}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment