Last active
August 29, 2015 14:22
-
-
Save KBeltz/e970a02b882ad15c382d to your computer and use it in GitHub Desktop.
Dinner Club and Friends
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Build a CheckSplitter class. | |
class CheckSplitter | |
# total_cost_of_meal - integer | |
# tip_percentage - integer | |
# num_people - integer | |
def initialize(total_cost_of_meal, tip_percentage = 0.15, num_people = 1) | |
@total_cost_of_meal = total_cost_of_meal | |
@tip_percentage = tip_percentage | |
@num_people = num_people | |
end | |
#determine how much each person owes | |
#return a decimal value for how much each person owes | |
def even_split | |
meal_plus_tip / @num_people | |
end | |
def meal_plus_tip | |
@total_cost_of_meal + tip_amount | |
end | |
def tip_amount | |
@total_cost_of_meal * tip_as_decimal | |
end | |
def tip_as_decimal | |
@tip_percentage / 100.0 | |
end | |
end | |
# Add a `DinnerClub` class. | |
require_relative "checksplitter.rb" | |
# members - hash with key: member name and value: tally of meal costs | |
class DinnerClub | |
# member_names - array containing member names | |
def initialize(member_names) | |
@members = {} | |
@history = {} | |
member_names.each do |name| | |
@members[name] = 0 | |
end | |
end | |
# some members go out to eat | |
# | |
# meal_cost - integer meal cost | |
# tip_percent - integer tip amount | |
# attendees - array of names for members who attended the outing | |
def go_out(one_guest_pays, paying_guest, meal_cost, tip_percent, attendees) | |
cs = CheckSplitter.new(meal_cost, tip_percent, attendees.length) | |
amount_per_person = cs.even_split | |
treat = cs.meal_plus_tip | |
case one_guest_pays | |
when "no" | |
@members[paying_guest] += treat | |
when "yes" | |
attendees.each do |n| | |
@members[n] += amount_per_person | |
end | |
end | |
@members | |
end | |
# creates a hash with restaurant names and corresponding attendees | |
def history(restaurant, attendees) | |
@history[restaurant] = attendees | |
end | |
end | |
require_relative "dinnerclub.rb" | |
puts "Who is in the dinner club?" | |
people = gets.chomp | |
dc = DinnerClub.new(people.split(", ")) | |
puts "Go out to eat? (yes/no)" | |
action = gets.chomp | |
while action.downcase != "no" do | |
puts "Where did you go?" | |
restaurant = gets.chomp | |
puts "Meal cost:" | |
meal_cost = gets.to_i | |
puts "Tip percentage:" | |
tip_percent = gets.to_i | |
puts "Who attended?" | |
attendees = gets.chomp | |
puts "Is the bill being split evenly? (yes/no)" | |
one_guest_pays = gets.chomp | |
if one_guest_pays.downcase == "no" | |
puts "Who is paying the bill?" | |
paying_guest = gets.chomp | |
end | |
attendees_to_array = attendees.split(", ") | |
puts dc.go_out(one_guest_pays, paying_guest, meal_cost, tip_percent, attendees_to_array) | |
puts "Again? (yes/no)" | |
action = gets.chomp | |
dc.history(restaurant, attendees) | |
puts dc.inspect | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment