Skip to content

Instantly share code, notes, and snippets.

@KBeltz
Created June 1, 2015 19:55
Show Gist options
  • Save KBeltz/5afa68193db010961ee4 to your computer and use it in GitHub Desktop.
Save KBeltz/5afa68193db010961ee4 to your computer and use it in GitHub Desktop.
Check Splitter
# New CheckSplitter objects should have an attribute that stores the total cost
# of the meal. They should have a way to define the tip amount (and a value
# that's set by default). They should be able to define how many people are in
# the group. They should use that information to help determine how much each
# person owes.
class CheckSplitter
attr_accessor :subtotal, :group_size, :tip_amount
def initialize(subtotal, group_size, tip_amount = 0.15)
@subtotal = subtotal
@group_size = group_size
@tip_amount = tip_amount
end
def percent # converts tip percentage to decimal if necessary
if tip_amount >= 1
percent = tip_amount / 100
else
percent = tip_amount
end
end
def total # total amount including tip
subtotal * (percent + 1)
end
def owes
puts "Each person owes $#{sprintf("%0.2f", (total / group_size))}"
end
end
first_check = CheckSplitter.new(35.77, 3, 0.2)
second_check = CheckSplitter.new(128.36, 4, 10)
third_check = CheckSplitter.new(55.21, 5)
puts first_check.owes
puts second_check.owes
puts third_check.owes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment