Last active
October 3, 2015 02:28
-
-
Save jtallant/2372038 to your computer and use it in GitHub Desktop.
Code Academy Long Challenge 1: Shopping Cart
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
# Long challenge: Based on the following data, | |
# write code that prints out the customer's total, including estimated | |
# sales tax, based on the shipping address they entered. | |
# Your code should work even if the values of the data change. | |
# 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} | |
params = { | |
:name => "Patrick McProgrammerson", | |
:address1 => "222 W. Merchandise Mart Plaza", | |
:address2 => "12th Floor", | |
:city => "Chicago", | |
:state => "IL", | |
:zip => "60654" | |
} | |
# Now change the value of the key :state in the params hash to "WI" and run your code again. | |
# Encapsulate your code in a method if you haven't already. | |
# The method should accept a cart, a hash containing tax rates, and an | |
# address as arguments. | |
# Now change the quantity of iMacs in the data to 3 and run your method. |
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
shopping_cart = [ | |
{:name => "iPad 2", :price => 499, :quantity => 2}, | |
{:name => "iMac 27", :price => 1699, :quantity => 1}, | |
{:name => "MacBook Air 13", :price => 1299, :quantity => 3} | |
] | |
sales_tax = { | |
"IL" => 0.115, | |
"IN" => 0.09, | |
"MI" => 0.06, | |
"WI" => 0.056 | |
} | |
params = { | |
:name => "Patrick McProgrammerson", | |
:address1 => "1234 Street Name", | |
:address2 => "", | |
:city => "Madison", | |
:state => "WI", | |
:zip => "48939" | |
} | |
line_break = "\n"*2 | |
def order_details(cart, tax_rates, customer) | |
total = total_with_tax(cart, tax_rates, customer) | |
customer_message(cart, customer, total) | |
end | |
def total_with_tax(cart, tax_rates, customer) | |
total = 0 | |
cart.each do |item| | |
total += item[:price] * item[:quantity] | |
end | |
total += total * tax_rates[customer[:state]] | |
end | |
def customer_message(cart, customer, total) | |
message = "You ordered:\n" | |
cart.each do |item| | |
message += "#{item[:quantity]} #{item[:name]} at $#{item[:price]} each.\n" | |
end | |
message += "Your total with tax is $#{total}\n\n" | |
message += "Items will be shipped to:\n#{customer[:name]}\n" | |
message += "#{customer[:address1]}\n" | |
message += "#{customer[:address2]}\n" if customer[:address2].length > 0 | |
message += "#{customer[:city]}, #{customer[:state]} #{customer[:zip]}" | |
end | |
puts line_break | |
puts '#### FIRST CUSTOMER ####' | |
puts order_details(shopping_cart, sales_tax, params) | |
puts line_break | |
shopping_cart[2][:quantity] = 1 | |
params = { | |
:name => "Justin Tallant", | |
:address1 => "1712 N Richmond Street", | |
:address2 => "3rd Floor", | |
:city => "Chicago", | |
:state => "IL", | |
:zip => "60647" | |
} | |
puts '#### SECOND CUSTOMER ####' | |
puts order_details(shopping_cart, sales_tax, params) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment