Skip to content

Instantly share code, notes, and snippets.

@samstephen
Last active August 29, 2015 14:22
Show Gist options
  • Save samstephen/11a8bfe3ebdcea37720f to your computer and use it in GitHub Desktop.
Save samstephen/11a8bfe3ebdcea37720f to your computer and use it in GitHub Desktop.
Build a CheckSplitter class
# Build a CheckSplitter class.
# 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
def initialize(meal, quality, group)
@meal = meal
@group = group
@quality = quality
end
def find_tip # 15% of bill for adequate service; 20% for great service;
if @quality == "poor" # no less than 10% for poor service
10
elsif @quality == "adequate"
15
elsif @quality == "great"
20
else
@quality = "poor"
return 10
end
end
def tip_amount # find tip amount buy multiplying the meal cost by the tip
@meal * (find_tip/100.0) # percentage based on the quality
end
def total # finds the sum of tip amount and meal cost
tip_amount + @meal
end
def split # splits the bill between all of the people in "group"
total / @group
end
def describe_transaction # information needed to describe the problem and it's solution
puts "-" * 60
print "The meal is $#{sprintf("%0.2f", @meal)}. "
puts "There are #{@group} people in our group. "
puts "The services was #{@quality}, so the tip should be #{find_tip}% of the meal. "
print "The total tip is $#{sprintf("%0.2f", tip_amount)}. "
puts "Each person owes $#{sprintf("%0.2f", split)}. "
puts "-" * 60
end
end
print "How much was the cost of your group's meal? "
meal = gets.chomp.to_f
print "Was your service great, adequate, or poor? "
quality = gets.chomp.to_s
print "How many people were in your group? "
group = gets.chomp.to_i
checksplitter = CheckSplitter.new(meal, quality.downcase, group)
checksplitter.find_tip
checksplitter.tip_amount
checksplitter.total
checksplitter.split
checksplitter.describe_transaction
@rplugge
Copy link

rplugge commented Jun 1, 2015

It all looks good to me. I like the difference in tips based on meal quality, it might be a bit hard for users to input though (hypothetically), unless it would show the options in list form.

@samstephen
Copy link
Author

Yeah, thats kinda what I was thinking, multiple choices. The rest being form input.

Thanks for your comment :)

@nschetter
Copy link

Nice! Seems to work just fine, and it's great you incorporated comments to explain the code. The neatness of the code and the comments made it really easy for a beginner like myself to follow. I also liked your touch with the quality of service.

@ciprianna
Copy link

I'd second the point on needing to know what the pre-determined categories are for meal quality. Knowing what they are though, that's a really clever input to determine tip amount. The code is very neat and clean looking. Nice job!

@samstephen
Copy link
Author

I changed it to make sure the user knows their options for the value of tip. Thanks for your input everyone! 😀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment