Skip to content

Instantly share code, notes, and snippets.

@revdan
Last active August 29, 2015 14:07
Show Gist options
  • Save revdan/02299a99a5c67ce746ec to your computer and use it in GitHub Desktop.
Save revdan/02299a99a5c67ce746ec to your computer and use it in GitHub Desktop.
class BookOrderer
attr_accessor :copies, :price
DEFAULT_PRICE = 99.freeze
def initialize(args={})
@copies = args[:copies].to_i
@price = (args[:price].to_i.zero?) ? DEFAULT_PRICE : args[:price].to_i
end
def total_price
raise "you can't sell no copies, stupid." if copies < 1
(price * copies) * discount
end
def discount_percent
100 - (discount * 100).to_i
end
private
def discount
DISCOUNTS.select { |range| range === copies }.values.first || DISCOUNTS[:max]
end
DISCOUNTS = {
0...10 => 1,
10...20=> 0.8,
20...50 => 0.7,
50...100 => 0.6,
:max => 0.5
}
end
while true do
puts "How many copies would you like to order?"
copies = gets.chomp.to_i
if copies.zero?
print "Number must be more than zero. "
else
break
end
end
puts "Enter the copy price, or hit enter to use the default price of #{BookOrderer::DEFAULT_PRICE}: "
price = gets.chomp.to_i
order = BookOrderer.new({copies: copies, price: price})
puts "Total price for #{order.copies} copies at $#{order.price} each"
puts " with #{order.discount_percent}% discount = $#{order.total_price}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment