Created
June 11, 2013 12:58
-
-
Save Sailias/5756589 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Database | |
attr_accessor :contacts | |
def initialize | |
@contacts = [] | |
end | |
end | |
class Contact | |
attr_accessor :name, :age, :height, :weight | |
def initialize(args) | |
@name, @age, @height, @weight = args[:name], args[:age], args[:height], args[:weight] | |
end | |
def save(db) | |
db.contacts << self | |
end | |
def destroy(db) | |
db.contacts.delete(self) | |
end | |
end | |
class ContactController | |
def self.index(db, error_message = nil) | |
@@db = db | |
contacts = db.contacts | |
puts "" | |
puts "Contact List" | |
puts "" | |
print " Id".ljust(10) | |
print "| Name".ljust(30) | |
print "| Age".ljust(20) | |
print "| Height".ljust(20) | |
print "| Weight".ljust(20) | |
puts "" | |
puts "".ljust(100, "-") | |
contacts.each_with_index do |contact, i| | |
print " #{i + 1}".ljust(10) | |
print "| #{contact.name}".ljust(30) | |
print "| #{contact.age}".ljust(20) | |
print "| #{contact.height}".ljust(20) | |
print "| #{contact.weight}".ljust(20) | |
puts "" | |
puts "".ljust(100, "-") | |
end | |
puts "" | |
puts "Enter one of the following commands:" | |
puts "1) The contact id to view their profile" | |
puts "2) The word 'new' to create a new contact" | |
puts "" | |
if error_message | |
puts error_message | |
puts "" | |
end | |
print "> " | |
answer = gets.chomp() | |
if answer.eql?("new") | |
ContactController.new_action | |
elsif answer.to_i > 0 | |
contact = db.contacts[answer.to_i - 1] | |
if contact | |
ContactController.show db, contact | |
else | |
ContactController.index db, "You have chosen to view an invalid contact" | |
end | |
else | |
ContactController.index db, "You selection wasn't recognized." | |
end | |
end | |
def self.show(db, contact) | |
puts "" | |
puts "Contacts > Viewing a contact" | |
puts "" | |
puts "Name: #{contact.name}" | |
puts "Age: #{contact.age}" | |
puts "Height: #{contact.height}" | |
puts "Weight: #{contact.weight}" | |
puts "" | |
puts "Enter one of the following commands:" | |
puts "1) The word 'back' to go back to the contacts list" | |
puts "2) The word 'new' to create a new contact" | |
puts "3) The word 'destroy' to create delete this contact" | |
print "> " | |
answer = gets.chomp() | |
case answer | |
when "back" | |
ContactController.index(db) | |
when "new" | |
ContactController.new_action(db) | |
when "destroy" | |
print "Are you sure you want to destroy this contact? Y/N " | |
destroy = gets.chomp() | |
if destroy == "Y" | |
db.contacts.delete(contact) | |
ContactController.index(db) | |
else | |
ContactController.show(db, contact) | |
end | |
else | |
ContactController.show(db, contact) | |
end | |
end | |
end | |
db = Database.new | |
c = Contact.new name: "Jonathan", age: 29 | |
c.save(db) | |
c2 = Contact.new name: "A much longer name" | |
c2.save(db) | |
ContactController.index(db) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment