Skip to content

Instantly share code, notes, and snippets.

@akshayKhot
Created February 10, 2022 04:48
Show Gist options
  • Save akshayKhot/867b6a037e7d04fd3376215bb4a8070a to your computer and use it in GitHub Desktop.
Save akshayKhot/867b6a037e7d04fd3376215bb4a8070a to your computer and use it in GitHub Desktop.
require "money"
Money.locale_backend = nil
Money.rounding_mode = BigDecimal::ROUND_HALF_UP
module ProgrammingRuby
module Refactoring
class InvoicePrinter
def statement(invoice, plays)
total_amount = 0
volume_credits = 0
result = "Statement for #{invoice[:customer]}\n"
invoice[:performances].each do |perf|
play = plays.fetch(perf[:playID].to_sym)
amount = 0
case play[:type]
when "tragedy"
amount = 40000
if perf[:audience] > 30
amount += 1000 * (perf[:audience] - 30)
end
when "comedy"
amount = 30000
if perf[:audience] > 20
amount += 10000 + 500 * (perf[:audience] - 20)
end
amount += 300 * perf[:audience]
else
raise StandardError.new "Unknown Type: #{play[:type]}"
end
# add volume credits
volume_credits += [perf[:audience] - 30, 0].max
# add extra credits for every ten comedy attendees
if play[:type] == "comedy"
volume_credits += (perf[:audience] / 5).floor
end
# print line for this order
result += " #{play[:name]}: #{Money.us_dollar(amount).format} (#{perf[:audience]} seats)\n"
total_amount += amount
end
result += "Amount owed is #{Money.us_dollar(total_amount).format}\n"
result += "You earned #{volume_credits} credits\n"
result
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment