Skip to content

Instantly share code, notes, and snippets.

@rplugge
Last active August 29, 2015 14:22
Show Gist options
  • Save rplugge/b08e51ad162547b31bd0 to your computer and use it in GitHub Desktop.
Save rplugge/b08e51ad162547b31bd0 to your computer and use it in GitHub Desktop.
Day 3 - Checksplitter/Dinnerclub/App
class Checksplitter
#writing a getter method
attr_reader :meal_cost, :tip_percentage, :number_of_people
def initialize(meal_cost, tip_percentage, number_of_people)
@meal_cost = meal_cost.to_f
@tip_percentage = tip_percentage.to_f
@number_of_people = number_of_people.to_i
end
#figures out tip amount, also converts tip % to decimal.
def tip
meal_cost * (tip_percentage / 100.0)
end
#adds tip amount to meal_cost
def total_cost
meal_cost + tip
end
#total amount per person after split
def total_split
total_cost / number_of_people
end
end
class Dinnerclub
attr_reader :member
#Turns input member array into hash with value of 0
def initialize(member_array)
@member = {}
member_array.each do |a|
member[a] = 0
end
end
#Method for going out to eat
# First creates a new checksplitter with the information provided
# Finds the total_split per person
# Adds the total_split to the hash
def going_out(meal_cost, tip_percentage, attendees)
dinner = Checksplitter.new(meal_cost, tip_percentage, attendees.length)
total_split = dinner.total_split
attendees.each do |a|
member[a] = member[a] + total_split
end
@member
end
end
puts "Let's start a dinner club!"
puts "First, who's part of this club? (E.g. - Rob, Amber, Zach, Jeremiah)"
people = gets.chomp
my_club = Dinnerclub.new(people.split(", "))
puts "Alright, go out to eat?"
answer = gets.chomp.capitalize
# First, checks to see if they want to continue
# Then gathers the required information (meal_cost, tip_percentage, attendees)
# Put's the people who are coming (split by comma and space) into an array to use in dinner_club)
# Then displays the answer after completing dinner_club.rb and check_splitter.rb
while answer != "No"
puts "What was the cost of the meal?"
meal_cost = gets.chomp
puts "How much did you tip? (E.g. 20%)"
tip_percent = gets.chomp
puts "Who's going?"
attendees = gets.chomp
attendees_array = attendees.split(", ")
puts my_club.going_out(meal_cost, tip_percent, attendees_array)
#Checks to see if they want to continue.
puts "Go out to eat again?"
answer = gets.chomp.capitalize
end
@sumeetjain
Copy link

Can you please edit this Gist and rename it to something_something_something.rb (the file extension is the part I'd like you to add. That way, it'll be easier to read.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment