#Dinner Club Markdown
Making practical and effective use of SRP, the Dinner Club program consists of 3 different files; checksplitter.rb, dinnerclub.rb, and app.rb. CheckSplitter
and DinnerClub
are two different classes with different functions; carefully laid out to handle a single responsibility amongst themselves and return values to the method. The last file app.rb
handles the interaction between the program and user.
##Great, so what does the program do?
Programs do things - getting there is a long road but the outcome is sometimes simple to explain. This program, Dinner Club, outputs a few things: the amount each member of Dinner Club has paid, where they went, and who went on the outing. The program looks like this when you run it:
Who is in the dinner club? (Comma-separated)
Sam, James, Amy
Okay, great. Go out to eat?
yes
Meal cost:
25
Tip percentage:
10
Who's going?
Sam, James
Where are they going?
Chinese Buffett
Is anyone Treating the group? (yes/no)
yes
Who is treating the group?
Sam
{"Sam"=>27.5, "James"=>0, "Amy"=>0}
{["Sam", "James"]=>"Chinese Buffett"}
Again?
no
You may noticed James and Amy didn't pay anything, so why was James included and Amy wasn't? Well, James went to the restaurant with Sam, but Sam paid for their meals; Amy didn't go, so she was left out of the outing. We'll get to that part of the program, soon. How about I take you back to the start of the program?
###Introducing CheckSplitter
The first file created in this project was checksplitter.rb,
which contains the class CheckSplitter;
a utility class which calculates how much each member owes for each dinner club event.
The documentation is straight forward in this file. The first method, tip_percent,
finds the percent of the tip; the second method, total_cost,
calculates sum of the meal cost and tip, then returns a float to it's containing method total_cost;
the last method, split,
splits the total cost amongst all the attendees.
###Now presenting DinnerClub
class DinnerClub
def initialize(member_names)
@members = {}
@outings = []
member_names.each do |name|
@members[name] = 0
end
end
#...
end
Next, we have dinner_club.rb.
DinnerClub
has two end goals that can be met in a few ways depending on answers in app.rb
; first is to make a list containing attendees and their paid history in the form of a hash (their value beginning at 0) which This will be stored in @members
; second the place where an array of attendees will be stored in @outing
.
def go_out(meal_cost, tip_percent, attendees, place)
cs = CheckSplitter.new(meal_cost,tip_percent,attendees.length)
amount_per_person = cs.split
#...
end
The first method, go_out,
stores the class CheckSplitter
in a variable called cs
as it's value. Pulling from that class, the program executes the split
method on cs
and adds that to amount_per_person.
def go_out(meal_cost, tip_percent, attendees, place)
#...
attendees.each do |a|
@members[a] =+ amount_per_person
end
#...
end
Now that amount_per_person
contains the amount per person or split,
we take each attendee (inside @member,
which is a hash of member names and their amount paid) and applies amount_per_person
to each of them as their value.
def being_treated(treater, meal_cost, tip_percent, attendees, place)
cs = CheckSplitter.new(meal_cost, tip_percent, attendees.length)
total_bill = (cs.total_cost)
@members[treater] =+ total_bill
#...
end
But what if someone has decided to foot the bill? Ah, that's where app.rb
uses the method being_treated;
this method utilizes CheckSplitter's total_cost
method instead of CheckSplitter's split
method like go_out
does. After the user tells the program which member treated the group to a meal, the return value in the total_cost
method is added to the treater's paid history.
def go_out(meal_cost, tip_percent, attendees, place)
#...
attendees_and_place = {attendees => place}
@outings.push attendes_and_place
puts @members
@outings
end
Not every member goes to each event, so sometimes the check splitter is splitting a check between 4 people and sometimes it's 3 or 7 or 10 or you get the picture. At the end of both methods, go_out
and being_treated,
the program will now apply the array of attendees into a hash as the key, having the value of place.
This place is defined by the user when they answer "Where are they going?" Because this is apart of both go_out
and being_treated,
attendees will still be marked as having attended that outing, but the amount they paid would $0 if being_treated
is true.