Skip to content

Instantly share code, notes, and snippets.

@ciprianna
Created June 3, 2015 18:19
Show Gist options
  • Save ciprianna/114f52aef491cb749070 to your computer and use it in GitHub Desktop.
Save ciprianna/114f52aef491cb749070 to your computer and use it in GitHub Desktop.
Dinner Club - Displays four separate files used here; one for CheckSplitter class, one for ClubEvent class, one for DinnerClub class, and App.rb to test
# Check Splitter
# Creates new class called CheckSplitter
class CheckSplitter
# Shows which attributes are accessible
attr_accessor :total_cost, :tip_percentage, :people
# Initializes the attributes as optional; sets default values in the options
# hash and merges the hash with inputs (if given). Uses those defaults as
# initialized attributes. Calls methods below with args as an argument.
def initialize(args = {})
options = {total_cost: 20, tip_percentage: 0.15, people: 2}
args = options.merge(args)
@total_cost = args[:total_cost]
tip_percent(args)
people_count(args)
neg_tip(args)
end
# Makes the default value a decimal, so the user can input either integers or
# floats.
def tip_percent(args)
tip_percent_temp = args[:tip_percentage].to_f
if tip_percent_temp >= 1
@tip_percentage = (tip_percent_temp / 100.0)
else
@tip_percentage = tip_percent_temp
end
end
# Checks if the tip input was negative; if so, sets it to 0.15
def neg_tip(args)
tip_percent_temp = args[:tip_percentage].to_f
if tip_percent_temp < 0
@tip_percentage = 0.15
end
end
# Evaluates the people input; if the input is less than 1, it sets it to 1
def people_count(args)
people_temp = args[:people]
if people_temp < 1
@people = 1
else
@people = people_temp
end
end
# Divides the total cost by the number of people
def base_cost_per_person
total_cost / people
end
# Multiplies the total cost by the tip percentage, then divides that by the
# number of people
def tip_per_person
(total_cost * tip_percentage) / people
end
# Adds the total cost of the meal per person and the tip per person
def final_per_person
self.tip_per_person + self.base_cost_per_person
end
# Gives the amounts; calculates the percent as a whole number again; rounds
# the final dollar amount per person to two decimal places. Formats output
# to wildcard, digit.two floats decimals.
def output
puts "The total cost of the meal is $#{sprintf("%0.2f", total_cost)}."
puts "Divided by #{people} people plus a #{tip_percentage * 100} percent tip, that's $#{sprintf("%0.2f", final_per_person)} per person."
end
end
##############################################################################
# Club Event
require './check_splitter.rb'
class ClubEvent
attr_accessor :total_cost, :people, :tip_percentage, :attendees_hash, :per_person
# Creates three variables and runs two methods.
#
# total_cost - integer passed by user
# tip_percentage - either an integer or float, passed by user
# people - array, passed by the user.
def initialize(total_cost, tip_percentage, people = [])
@total_cost = total_cost
@people = people
@tip_percentage = tip_percentage
dinner_event(total_cost, people, tip_percentage)
hash_update(people, per_person)
end
# Creates a check_splitter object and stores a per person cost.
#
# Creates a temporary variable to store the length of the people array.
# Creates the new check splitter object by passing the stored information
# as the paramaters.
#
# Stores the per_person cost as an instance variable.
def dinner_event(total_cost, people, tip_percentage)
people_count_temp = people.length
dinner_event_split = CheckSplitter.new(total_cost: total_cost, people: people_count_temp, tip_percentage: tip_percentage)
@per_person = dinner_event_split.final_per_person
end
# Creates a hash of people and the per_person cost of the event.
#
# per_person - float created from the dinner_event method; instance variable.
# Stores the attendees_hash as another instance variable.
def hash_update(people, per_person)
@attendees_hash = {}
people.each do |person, money|
attendees_hash[person] = per_person
end
end
end
#############################################################################
# Dinner Club
require './club_event.rb'
class DinnerClub
attr_accessor :running_balance
# Creates a mock hash of dinner club members
# Makes the default value of the hash 0, so that new members can be added.
def initialize
@running_balance = {}
running_balance.default = 0
end
# Creates a new dinner event for when some people go out.
#
# total_cost - integer passed by the user when the method is run.
# tip_percentage - float or integer passed by the user when the method is run.
# people - array of attendees passed by user when the method is run.
#
# Updates the running_balance hash using the balance method.
# Displays the new totals for all members using the display_total method.
def new_event(total_cost, tip_percentage, people)
event = ClubEvent.new(total_cost, tip_percentage, people)
balance(running_balance, event.attendees_hash)
display_total(running_balance)
end
# Updates the balance if a new dinner event occurred.
#
# running_balance - hash that is continually being updated.
# attendees_hash - instance variable from the new_event method (ClubEvent
# object)
#
# Takes the running_balance and permanently merges that hash with the
# attendees_hash. Takes the key, value from running_balance, and value from
# attendees_hash, and sums them together to store in the updated
# running_balance hash.
def balance(running_balance, attendees_hash)
running_balance.merge!(attendees_hash){|person, run_bal, event_val| run_bal + event_val}
end
# Displays the current running balance of each member of DinnerClub.
#
# Puts header information for the table.
# Prints a dashed line to separate header from table information.
# Puts a line break.
# Iterates over running_balance hash to puts name and balance; formats balance
# to display as currency.
def display_total(running_balance)
puts "\n"
puts "Member Name:".ljust(20) + "Member Balance:".rjust(10)
35.times {print "-"}
puts "\n"
running_balance.each do |name, balance|
puts "#{name}:".ljust(20) + "$#{sprintf("%0.2f", balance)}".rjust(10)
end
puts "\n"
end
end
##############################################################################
# Test Program
require './dinnerclub.rb'
puts "Your first Dinner Club event!"
club = DinnerClub.new
puts "Did you go out to dinner?"
answer = gets.chomp.downcase
while answer != "no"
puts "How much did the dinner cost?"
total_cost = gets.chomp.to_i
puts "Great, and what percentage of tip did the group leave?"
tip_percentage = gets.chomp.to_f
puts "And finally, who attended?"
people = gets.chomp
people_array = people.split(", ")
club.new_event(total_cost, tip_percentage, people_array)
puts "Did you go to another dinner event?"
answer = gets.chomp.downcase
end
puts "Okay. Thanks for using the Dinner Club app!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment