Skip to content

Instantly share code, notes, and snippets.

@rplugge
Last active August 29, 2015 14:22
Show Gist options
  • Save rplugge/f459cabc16e2700ac2ee to your computer and use it in GitHub Desktop.
Save rplugge/f459cabc16e2700ac2ee to your computer and use it in GitHub Desktop.
Dinnerclub Explanation

#Dinnerclub

(Entire file found here - https://gist.github.com/rplugge/79530435e2b58144fe13)

I needed to build a program the could do 2 main things; split a check, and store information about dinner club events.

##Checksplitter

The class ‘Checksplitter’ is fairly simple. I require 3 attributes to split the check.

  • The cost of the meal (meal_cost -float-)
  • The percentage they tip (tip_percent -integer-)
  • The number of people splitting the bill (number_of_people -integer-)

The next few methods are finding various necessary values. ‘tip’ finds the amount of the tip, ‘total_cost' adds tip to the cost of the meal, and ‘total_split' splits the bill based on the number of people attending.

##Dinnerclub

The class ‘Dinnerclub’ only requires an array containing the members of the club to initialize. It then creates 2 open hashes.

  • One is named ‘@member’, and will hold the names of the members as the key and will set their “Pay to date” value at 0.
  • The other is named ‘@history_hash’ and will later contain the individual dinner_club outings.
  def initialize(member_array)
    @member = {}
    @history_hash = {}
    
    member_array.each do |a|
      member[a] = 0
    end
  end

Section 1

The next method has a bit more going on. It requires 3 attributes. These are the same attributes needed for the Checksplitter class.

  • The cost of the meal (meal_cost -float-)
  • The tip percentage (tip_percentage -integer-)
  • The people attending (attendees -integer-)

It then creates a new Checksplitter class with those provided attributes. Then it sets variables equal to the values of the Checksplitter methods (I was able to do this because of the getter methods provided in the Checksplitter class (total_split and total_cost).

def going_out(meal_cost, tip_percentage, attendees)
    dinner = Checksplitter.new(meal_cost, tip_percentage, attendees.length)
    
    total_split = dinner.total_split
    one_person = dinner.total_cost

Section 2

The next section of this method is for checking to see if one person is paying the whole bill. It asks for input and then stores the value as ‘answer’. The if/else statement checks the answer just provided. If “No” it adds the each split of the bill to each member. Else it will check who is paying via user-input and then add the total_bill to his value. It then displays the updated ‘member’ hash.

puts "Will one person be footing the entire bill?"
    answer = gets.chomp.capitalize
    
    if answer == “No"
      attendees.each do |a|
        member[a] = member[a] + total_split
      end
    else
      puts "Who is paying?"
      unlucky_person = gets.chomp
      
      member[unlucky_person] += one_person
    end
    
    @member
  end

The next method uses the previously defined hash named ‘history_hash’. It requires two attributes.

  • The restaurant name and date (input as e.g. ‘McDonalds_03_01’ -string-).
  • The array containing the attendees of this event (attendees -integer-)

It then sets the value of the restaurant to the array of attendees.

  def history(resturant_name_date, attendees)
    @history_hash[resturant_name_date] = attendees
  end

There is then a getter method so I can display the history_hash.

  def history_hash_display
      @history_hash
     end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment