Created
October 5, 2012 23:18
-
-
Save superacidjax/3843019 to your computer and use it in GitHub Desktop.
Lab 2 Solution
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
class Person | |
attr_accessor :name, :gender, :pets | |
def initialize(n, g) | |
@name = n | |
@gender = g | |
@pets = [] | |
end | |
def to_s | |
"#{name} is a #{gender} and has #{pets.count} pets." | |
end | |
end | |
class Animal | |
attr_accessor :name, :age, :species | |
def initialize(n, a, s) | |
@name = n | |
@age = a | |
@species = s | |
end | |
end | |
puts "Animal (a) or Person (p) or Quit (q)" | |
response = gets.chomp | |
people = {} | |
while response == 'a' || response == 'p' | |
if response == 'p' | |
puts "Name?" | |
name = gets.chomp | |
puts "Gender?" | |
gender = gets.chomp | |
person = Person.new(name, gender) | |
people[name] = person | |
else | |
puts "Name?" | |
name = gets.chomp | |
puts "Age?" | |
age = gets.chomp | |
puts "Species?" | |
species = gets.chomp | |
animal = Animal.new(name, age, species) | |
puts "Pick from list of owners #{people.keys}" | |
owner = gets.chomp | |
people[owner].pets << animal | |
end | |
puts "Animal (a) or Person (p) or Quit (q)" | |
response = gets.chomp | |
end | |
people.keys.each do |name| | |
puts people[name] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment