Created
April 15, 2012 02:00
-
-
Save fakefarm/2389323 to your computer and use it in GitHub Desktop.
Code Academy exercise
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
# Exercise 1 : Your output should look like this: "Your total with tax is $4455.54." | |
shopping_cart = [ | |
{:name => "iPad 2", :price => 499, :quantity => 2}, | |
{:name => "iMac 27", :price => 1699, :quantity => 1}, | |
{:name => "MacBook Air 13", :price => 1299, :quantity => 1} | |
] | |
sales_tax = { | |
"IL" => 0.115, | |
"IN" => 0.09, | |
"MI" => 0.06, | |
"WI" => 0.056 | |
} | |
customer = { | |
:name => "Patrick McProgrammerson", | |
:address1 => "222 W. Merchandise Mart Plaza", | |
:address2 => "12th Floor", | |
:city => "Chicago", | |
:state => "IL", | |
:zip => "60654" | |
} | |
# --- Methods --------------------------------------- | |
def sub_total(cart, customer) | |
total = 0.0 | |
cart.each do |item| | |
total += item[:price] * item[:quantity] | |
end | |
return total | |
end | |
def total_with_tax(sub_total,tax) | |
return sub_total * tax | |
end | |
def cart_quantity(cart) | |
product = 0 | |
cart.each do |item| | |
product += item[:quantity] | |
end | |
return product | |
end | |
# --- Conversion Variables -------------------------- | |
order_tax_rate = (1 + sales_tax[customer[:state]]) | |
customer_sub_total = sub_total(shopping_cart,customer) | |
customer_total = total_with_tax(sub_total(shopping_cart,customer),order_tax_rate) | |
cart_items = cart_quantity(shopping_cart) | |
# -------------- output ----------------------------- | |
puts order_tax_rate | |
puts customer_sub_total | |
puts customer_total | |
puts cart_items | |
#TODO - put the product output | |
#TODO - Format the output |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment