Created
June 3, 2015 21:23
-
-
Save ciprianna/b2773fbadc08aace11ed to your computer and use it in GitHub Desktop.
Dinner Club updates without stretches - Still displays four different files (CheckSplitter class, EventClub class, DinnerClub class, and app.rb)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
# Total cost of the meal with tip | |
def total_cost_with_tip | |
total_cost + (total_cost * tip_percentage) | |
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, :date, :location, :event_log, :who_paid, :total_cost_single_pay | |
# Creates six 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 | |
# date - string, passed by user | |
# location - string, passed by user | |
# who_paid - string, passed by user | |
def initialize(total_cost, tip_percentage, people = [], date, location, who_paid) | |
@total_cost = total_cost | |
@people = people | |
@tip_percentage = tip_percentage | |
@date = date | |
@location = location | |
@who_paid = who_paid | |
dinner_event(total_cost, people, tip_percentage, date, location, who_paid) | |
hash_update(who_paid, people, per_person) | |
end | |
# Creates a check_splitter object and stores a per person cost. | |
# | |
# Creates the new check splitter object by passing the stored information | |
# as the paramaters. | |
# | |
# Stores the per_person cost as an instance variable. | |
# Runs the event_update method. | |
# Stores the total_cost_single_pay as an instance variable to use later if | |
# one person pays. | |
def dinner_event(total_cost, people, tip_percentage, date, location, who_paid) | |
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 | |
event_update(date, location, people) | |
@total_cost_single_pay = dinner_event_split.total_cost_with_tip | |
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. | |
# Evaluates if everyone paid or one person paid. | |
# If everyone paid, iterates through the people hash and adds people names and | |
# per_person cost to the attendees_hash. | |
# If < everyone paid, it iterates through the people hash and adds peoples | |
# names and a value of 0 to the attendees_hash. It then modifies the attendees_hash | |
# for who_paid to make the value equal to the total_cost_with_tip of the dinner. | |
# Stores the attendees_hash as another instance variable. | |
def hash_update(who_paid, people, per_person) | |
@attendees_hash = {} | |
if who_paid.downcase == "everyone" | |
people.each do |person, money| | |
attendees_hash[person] = per_person | |
end | |
else | |
people.each do |person, money| | |
attendees_hash[person] = 0 | |
end | |
attendees_hash[who_paid] = total_cost_single_pay | |
end | |
puts attendees_hash | |
end | |
# Creates a hash of events dates, locations, and people. | |
# | |
# event_log - Hash stored as an instance variable. | |
# date - String -----If time...figure out how to format correctly? | |
# location - String | |
# | |
# Hash uses the date as the key, and stores the value of an array containing | |
# the location and the people array. | |
def event_update(date, location, people) | |
@event_log = {date =>[location, people]} | |
puts event_log | |
end | |
end | |
###################################################################################### | |
# Dinner Club | |
require './club_event.rb' | |
class DinnerClub | |
attr_accessor :running_balance, :all_events | |
# Creates an empty hash to store the dinner club members and their balance. | |
# Makes the default value of the hash 0, so that new members can be added. | |
# Makes | |
def initialize | |
@running_balance = {} | |
running_balance.default = 0 | |
@all_events = {} | |
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. | |
# date - string passed by the user. | |
# location - string passed by the user. | |
# who_paid - string passed by the user. | |
# | |
# Updates the running_balance hash using the balance method. | |
# Displays the new totals for all members using the display_total method. | |
# Updates the all_events hash using the add_event method. | |
def new_event(total_cost, tip_percentage, people, date, location, who_paid) | |
event = ClubEvent.new(total_cost, tip_percentage, people, date, location, who_paid) | |
balance(running_balance, event.attendees_hash) | |
display_total(running_balance) | |
add_event(all_events, event.event_log) | |
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 | |
# Updates the all_events hash if a new event takes place. | |
# | |
# all_events - Hash | |
# event_log - Hash taken from the ClubEvent Class's event_update method. | |
# Takes the all_events hash and permanently merges it with the event_log. | |
def add_event(all_events, event_log) | |
all_events.merge!(event_log) | |
end | |
# Displays the log of events nicely. Will only show if called specifically. | |
# | |
# Puts a blank line before and after the table. | |
# Puts a header followed by a dashed line. | |
# Iterates through all_events hash, displaying sections to match header locations. | |
def display_events(all_events) | |
puts "\n" | |
puts "Date".ljust(25) + "Location".center(15) + "Members".rjust(25) | |
65.times {print "-"} | |
puts "\n" | |
all_events.each do |date, array| | |
puts "#{date}".ljust(25) + "#{array[0]}".center(15) + "#{array[1]}".rjust(25) | |
end | |
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 "Who attended?" | |
people = gets.chomp | |
people_array = people.split(", ") | |
puts "When did you go? (MM-DD-YYYY)" | |
date = gets.chomp | |
puts "Where did you go?" | |
location = gets.chomp | |
puts "Who paid? If it was split evenly, type 'everyone'." | |
who_paid = gets.chomp | |
club.new_event(total_cost, tip_percentage, people_array, date, location, who_paid) | |
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