Skip to content

Instantly share code, notes, and snippets.

@koriroys
Created April 13, 2012 19:44
Show Gist options
  • Save koriroys/2379576 to your computer and use it in GitHub Desktop.
Save koriroys/2379576 to your computer and use it in GitHub Desktop.
shopping_cart = [
{:name => "iPad 2", :price => 499, :quantity => 5},
{: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
}
customer1 = {
:name => "Patrick McProgrammerson",
:address1 => "1234 Street Name",
:address2 => "",
:city => "Indianapolis",
:state => "IN",
:zip => "48939"
}
customer2 = {
:name => "Justin Tallant",
:address1 => "555 Hidden Street",
:address2 => "Basement",
:city => "Chicago",
:state => "IL",
:zip => "61166"
}
def totalCost(cart, tax_rates, customer)
total = 0
cart.each do |item|
total += item[:price] * item[:quantity]
end
state = customer[:state]
rate = tax_rates[state]
total + total * rate
end
def displayOrder(total, cart, customer)
message = []
message << "You ordered:"
cart.each do |item|
message << "#{item[:quantity]} #{item[:name]} at $#{item[:price]} each."
end
message << "Your total with tax is $#{total}."
message << "Items will be shipped to #{customer[:name]}."
message << ""
message << "#{customer[:address1]}"
message << "#{customer[:address2]}." unless customer[:address2].empty?
message << "#{customer[:city]}, #{customer[:state]} #{customer[:zip]}"
end
cost1 = totalCost(shopping_cart, sales_tax, customer2)
cost2 = totalCost(shopping_cart, sales_tax, customer1)
puts displayOrder(cost1, shopping_cart, customer2)
puts
puts displayOrder(cost2, shopping_cart, customer1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment