Skip to content

Instantly share code, notes, and snippets.

@nthj
Created December 10, 2013 22:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nthj/7901128 to your computer and use it in GitHub Desktop.
Save nthj/7901128 to your computer and use it in GitHub Desktop.
# Quiz Exercise, Part One
#
# Quiz for a list of names & birthdays,
# then list out their ages
#
# For example:
#
# Hey there! Please enter a list of names:
# > John, Sam, Bob, Alisha, Henry
#
# Thanks. When is John's birthday? 10/13/2000
#
# OK, and Sam's birthday? 8/3/1987
# And Bob's? 2/17/1957
# Alisha's? 4/21/1978
# Henry's? 6/23/1980
#
# OK, all done. You have 5 guests. Here are their ages:
#
# * John: 13
# * Sam: 26
# * Bob: 56
# * Alisha: 35
# * Henry: 33
#
# Enjoy your party!
#
require 'time'
##
## OUTLINE
##
# We're going to want to break the problem down into steps.
# First, let's ask for the user's input:
def ask_for_birthday
print "Please enter their birthday (dd/mm/yyyy): "
Date.parse gets.chomp
rescue => e
retry
end
def ask_for_name
print "Hey! What's this guest's name? "
gets.strip
end
def calculate_number_of_guests_and_friends(list_of_guests)
list_of_guests.length + list_of_guests.reject do |guest|
guest[:friends].zero?
end.length
end
def calculate_age_from_birthday(birthday)
Time.now.year - birthday.year
end
guests = []
until (name = ask_for_name).empty?
guest_birthday = ask_for_birthday
is_bringing_a_gift = rand(2).to_i.zero? ? true : false # TODO
how_many_friends = rand(2).to_i # TODO
guests << {
:age => calculate_age_from_birthday(guest_birthday),
:gift => is_bringing_a_gift,
:friends => how_many_friends,
:name => name
}
puts "Great, #{name} is all set up\n\n"
end
number_of_gifts = guests.select { |g| g[:gift] }.length
number_of_people = calculate_number_of_guests_and_friends(guests)
# We want to display the data onto the screen.
#
# * How many guests do we have total?
# * What are their names and ages?
puts "All done. You have #{guests.length} guests with friends for a total of #{number_of_people} people."
puts "#{number_of_gifts} of them are bringing gifts."
puts "Here is everyone's ages: "
# TODO: loop over each guest
guests.each do |guest|
puts "* #{guest[:name]} is #{guest[:age]} years old"
end
##
## EXTENSION #1
##
# Ask for a few more pieces of information:
#
# * Are they bringing a gift? (true or false)
# * Display how many people are bringing a gift.
##
## EXTENSION #2
##
# * How many friends are they bringing? (0, 1)
# * Update the "number of guests" to take the friends' numbers into account:
#
# "You have 5 guests who are bringing 2 friends for a total of 7 people."
##
## EXTENSION #3
##
# Calculate how much the party will cost.
# * Estimate $5 for each person, including the friends that guests are bringing.
# * Add an extra $3 for each person over 21, as they will drink our alcohol
# * Provide a $2 discount for each senior citizen
##
## EXTENSION #4
##
# Display a receipt for the total party cost
# * Display each person as a line-item ($5)
# * Display each alcohol addition
# * Display each discount
# * `rjust` or `ljust` (string methods) each column and value so everything lines up nicely
# EXAMPLE:
# * Nathaniel: $5
# * John: $5
# * Sam: $5
# * John Alcohol: $3
# * Sam Alcohol: $3
# * John Senior: ($2)
# * Subtotal: $19
# * Tax: $1.90
# * Total: $21.90
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment