Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2015
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. # Build a CheckSplitter class.
  2. class CheckSplitter
  3. # total_cost_of_meal - integer
  4. # tip_percentage - integer
  5. # num_people - integer
  6. def initialize(total_cost_of_meal, tip_percentage = 0.15, num_people = 1)
  7. @total_cost_of_meal = total_cost_of_meal
  8. @tip_percentage = tip_percentage
  9. @num_people = num_people
  10. end
  11.  
  12. #determine how much each person owes
  13. #return a decimal value for how much each person owes
  14. def even_split
  15. meal_plus_tip / @num_people
  16. end
  17.  
  18. def meal_plus_tip
  19. @total_cost_of_meal + tip_amount
  20. end
  21.  
  22. def tip_amount
  23. @total_cost_of_meal * tip_as_decimal
  24. end
  25.  
  26. def tip_as_decimal
  27. @tip_percentage / 100.0
  28. end
  29. end
  30.  
  31.  
  32. # Add a `DinnerClub` class.
  33. require_relative "checksplitter.rb"
  34. # members - hash with key: member name and value: tally of meal costs
  35. class DinnerClub
  36. # member_names - array containing member names
  37. def initialize(member_names)
  38. @members = {}
  39. @history = {}
  40.  
  41. member_names.each do |name|
  42. @members[name] = 0
  43. end
  44. end
  45. # some members go out to eat
  46. #
  47. # meal_cost - integer meal cost
  48. # tip_percent - integer tip amount
  49. # attendees - array of names for members who attended the outing
  50. def go_out(one_guest_pays, paying_guest, meal_cost, tip_percent, attendees)
  51. cs = CheckSplitter.new(meal_cost, tip_percent, attendees.length)
  52.  
  53. amount_per_person = cs.even_split
  54. treat = cs.meal_plus_tip
  55.  
  56. case one_guest_pays
  57. when "no"
  58. @members[paying_guest] += treat
  59. when "yes"
  60. attendees.each do |n|
  61. @members[n] += amount_per_person
  62. end
  63. end
  64. @members
  65. end
  66. # creates a hash with restaurant names and corresponding attendees
  67. def history(restaurant, attendees)
  68. @history[restaurant] = attendees
  69. end
  70. end
  71.  
  72.  
  73. require_relative "dinnerclub.rb"
  74.  
  75. puts "Who is in the dinner club?"
  76. people = gets.chomp
  77.  
  78. dc = DinnerClub.new(people.split(", "))
  79.  
  80. puts "Go out to eat? (yes/no)"
  81. action = gets.chomp
  82.  
  83. while action.downcase != "no" do
  84. puts "Where did you go?"
  85. restaurant = gets.chomp
  86.  
  87. puts "Meal cost:"
  88. meal_cost = gets.to_i
  89.  
  90. puts "Tip percentage:"
  91. tip_percent = gets.to_i
  92.  
  93. puts "Who attended?"
  94. attendees = gets.chomp
  95.  
  96. puts "Is the bill being split evenly? (yes/no)"
  97. one_guest_pays = gets.chomp
  98.  
  99. if one_guest_pays.downcase == "no"
  100. puts "Who is paying the bill?"
  101. paying_guest = gets.chomp
  102. end
  103.  
  104. attendees_to_array = attendees.split(", ")
  105.  
  106. puts dc.go_out(one_guest_pays, paying_guest, meal_cost, tip_percent, attendees_to_array)
  107. puts "Again? (yes/no)"
  108. action = gets.chomp
  109.  
  110. dc.history(restaurant, attendees)
  111. puts dc.inspect
  112. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement