1
0
Fork 0
57-ruby-exercises/010_self_checkout.rb

37 lines
765 B
Ruby
Raw Normal View History

2024-05-08 10:21:49 +00:00
TAX_RATE = 5.5
def calculate_tax_for(item_price)
item_price * (TAX_RATE / 100)
end
puts("How many items are you purchasing?")
total_items = gets.chomp.to_i
item_quantities = []
item_prices = []
total_items.times do |i|
puts("Enter price for item #{i + 1}:")
item_prices << gets.chomp.to_f
puts("Enter quantity for item #{i + 1}: ")
item_quantities << gets.chomp.to_i
end
subtotal = 0
tax = 0
item_prices.each do |price|
item_quantities.each do |qty|
subtotal += price * qty
# subtotal = subtotal + price * qty
tax += calculate_tax_for(subtotal)
# tax = tax + (price * qty) * (TAX_RATE / 100)
end
end
puts("Subtotal: #{subtotal.round(2)}")
puts("Tax: #{tax.round(2)}")
puts("Total: #{(subtotal + tax).round(2)}")