Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2015
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. # Dinner Club
  2.  
  3. ## Assignment
  4. The assignment was to write a program that collects member names, restaurant names, and meal totals to track outings for a dinner club. The program should also include the ability to track attendance even if one member chose to pay for the entire meal.
  5.  
  6. The CheckSplitter class exists to calculate the meal total, including the tip, and then split that total evenly among the members who attended the outing. The DinnerClub class keeps track of member names and the outing history, as well as creating a case to allow a single member to pay for the entire meal if desired.
  7.  
  8. ## CheckSplitter
  9. When CheckSplitter is initialized it requires three arguments, all of which are integer values: total_cost_of_meal, tip_percentage and num_people. Three corresponding attributes are created as well.
  10. ```ruby
  11. def initialize(total_cost_of_meal, tip_percentage, num_people)
  12. @total_cost_of_meal = total_cost_of_meal
  13. @tip_percentage = tip_percentage
  14. @num_people = num_people
  15. end
  16. ```
  17. The even_split method takes the total cost of the meal_plus_tip and @num_people and calculates the amount each dinner club member owes for the meal.
  18. ```ruby
  19. def even_split
  20. meal_plus_tip / @num_people
  21. end
  22. ```
  23. The meal_plus_tip method calculates the tip amount and adds it to the @total_cost_of_meal in one step.
  24. ```ruby
  25. def meal_plus_tip
  26. @total_cost_of_meal * (tip_as_decimal + 1)
  27. end
  28. ```
  29. The tip_as_decimal method compensates for the possibility of the user entering the tip in decimal form. If the tip is entered as an integer percentage amount, it is converted to decimal form.
  30. ```ruby
  31. def tip_as_decimal
  32. if @tip_percentage >= 1
  33. tip_as_decimal = @tip_percentage / 100.0
  34. else
  35. tip_as_decimal = @tip_percentage
  36. end
  37. end
  38. ```
  39. ## DinnerClub
  40. This class takes one argument, member_names, as an array when it is initialized. It has two attributes, @members and @history, which are each initialized as an empty hash.
  41. ```ruby
  42. def initialize(member_names)
  43. @members = {}
  44. @history = {}
  45. ```
  46. This iterates over the member_names array and adds each name as a key to the empty @members hash with a default value of zero.
  47. ```ruby
  48. member_names.each do |name|
  49. @members[name] = 0
  50. end
  51. end
  52. ```
  53. The go_out method is defined with five arguments: one_guest_pays, paying_guest, meal_cost, tip_percent, and attendees. When the go_out method is called a new instance of the CheckSplitter class, called cs, is then instantiated.
  54. ```ruby
  55. def go_out(one_guest_pays, paying_guest, meal_cost, tip_percent, attendees)
  56. cs = CheckSplitter.new(meal_cost, tip_percent, attendees.length)
  57.  
  58. amount_per_person = cs.even_split
  59. treat = cs.meal_plus_tip
  60. ```
  61. The one_guest_pays case requires the user to confirm whether or not the meal will be split evenly. If false is returned, it requires the user to provide the name of the paying member, and meal_plus_tip is called on cs to calculate the appropriate member total. If true is returned, even_split is called on cs to split the check and update the total of each attendee.
  62. ```ruby
  63. case one_guest_pays
  64. when false
  65. treat = cs.meal_plus_tip
  66. @members[paying_guest] += treat
  67. when true
  68. amount_per_person = cs.even_split
  69. attendees.each do |n|
  70. @members[n] += amount_per_person
  71. end
  72. end
  73. ```
  74. The history method adds to the empty hash @ history with key: restaurant and value: the array of attendees. This allows the user to view a hash of the restaurants the dinner club has visited along with an array value showing the corresponding attendees at each restaurant outing.
  75. ```ruby
  76. def history(restaurant, attendees)
  77. @history[restaurant] = attendees
  78. end
  79. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement