Created
September 12, 2013 00:55
-
-
Save steven-ferguson/6531962 to your computer and use it in GitHub Desktop.
Library App
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 Book | |
attr_reader :title, :isbn, :author_last_name, :author_first_name, :id, :copies, :number_of_copies | |
def initialize(title, isbn, author_last_name, author_first_name, number_of_copies) | |
@title = title | |
@isbn = isbn | |
@author_first_name = author_first_name | |
@author_last_name = author_last_name | |
@number_of_copies = number_of_copies | |
end | |
def set_id(id) | |
@id = id | |
end | |
def save_copies(number_to_save) | |
number_to_save.times do |i| | |
copy = Copy.new(@id) | |
copy.save | |
end | |
end | |
def add_copies(number_to_add) | |
save_copies(number_to_add) | |
@number_of_copies += number_to_add | |
end | |
# def remove_copies(number_to_remove) | |
# @copies -= number_to_remove | |
# end | |
def ==(another_book) | |
self.isbn == another_book.isbn | |
end | |
def save | |
results = DB.exec("INSERT INTO books (title, isbn, author_last_name, author_first_name, copies) VALUES ('#{@title}', #{@isbn}, '#{@author_last_name}', '#{@author_first_name}', #{@number_of_copies}) RETURNING id;") | |
id = results.first['id'].to_i | |
set_id(id) | |
end | |
def delete | |
DB.exec("DELETE FROM books * WHERE id = #{@id};") | |
end | |
def self.all | |
results = DB.exec("SELECT * FROM books") | |
books = [] | |
results.each do |result| | |
title = result['title'] | |
isbn = result['isbn'].to_i | |
author_last_name = result['author_last_name'] | |
author_first_name = result['author_first_name'] | |
copies = result['copies'].to_i | |
id = result['id'].to_i | |
book = Book.new(title, isbn, author_last_name, author_first_name, copies) | |
book.set_id(id) | |
books << book | |
end | |
books | |
end | |
def self.find(book_id) | |
results = DB.exec("SELECT * FROM books WHERE id = #{book_id};") | |
title = results.first['title'] | |
author_first_name = results.first['author_first_name'] | |
author_last_name = results.first['author_last_name'] | |
isbn = results.first['isbn'].to_i | |
copies = results.first['copies'].to_i | |
book = Book.new(title, isbn, author_last_name, author_first_name, copies) | |
end | |
def get_copies | |
results = DB.exec("SELECT * FROM copies WHERE book_id = #{@id};") | |
@copies = [] | |
results.each do |result| | |
id = result['id'].to_i | |
copy = Copy.new(@id) | |
copy.set_id(id) | |
@copies << copy | |
end | |
end | |
end | |
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
require 'spec_helper' | |
describe Book do | |
it 'is initialized with a title, isbn, author last name, author first name and number of copies' do | |
book = Book.new('this', 123, 'ferguson', 'steven', 100) | |
book.should be_an_instance_of Book | |
end | |
it 'tells you its title' do | |
book = Book.new('this', 123, 'ferguson', 'steven', 100) | |
book.title.should eq 'this' | |
end | |
it 'tells you its isbn' do | |
book = Book.new('this', 123, 'ferguson', 'steven', 100) | |
book.isbn.should eq 123 | |
end | |
it 'tells you the authors last name' do | |
book = Book.new('this', 123, 'ferguson', 'steven', 100) | |
book.author_last_name.should eq 'ferguson' | |
end | |
it 'tells you the authors first name' do | |
book = Book.new('this', 123, 'ferguson', 'steven', 100) | |
book.author_first_name.should eq 'steven' | |
end | |
it 'sets the id' do | |
book = Book.new('this', 123, 'ferguson', 'steven', 100) | |
book.set_id(1) | |
book.id.should eq 1 | |
end | |
it 'adds copies' do | |
book = Book.new('this', 123, 'ferguson', 'steven', 0) | |
book.save | |
book.add_copies(120) | |
Copy.all.length.should eq 120 | |
end | |
# it 'removes copies' do | |
# book = Book.new('this', 123, 'ferguson', 'steven', 100) | |
# book.remove_copies(2) | |
# Copies.all.lengthshould eq 98 | |
# end | |
it 'is equal if isbn is the same' do | |
book = Book.new('this', 123, 'ferguson', 'steven', 100) | |
book2 = Book.new('this', 123, 'ferguson', 'steven', 100) | |
book.should eq book2 | |
end | |
it 'lets you save the book to the database' do | |
book = Book.new('this', 123, 'ferguson', 'steven', 100) | |
book.save | |
Book.all.first.should eq book | |
end | |
it 'sets the id after being saved' do | |
book = Book.new('this', 123, 'ferguson', 'steven', 100) | |
book.save | |
book.id.should be_an_instance_of Fixnum | |
end | |
it 'lets you delete a book from the database' do | |
book = Book.new('this', 123, 'ferguson', 'steven', 100) | |
book.save | |
Book.all.first.delete | |
Book.all.should eq [] | |
end | |
it 'saves copies to the database' do | |
book = Book.new('this', 123, 'ferguson', 'steven', 100) | |
book.save | |
book.save_copies(10) | |
Copy.all.length.should eq 10 | |
end | |
it 'gets all the copies of a book' do | |
book = Book.new('this', 123, 'ferguson', 'steven', 100) | |
book.save | |
book.save_copies(100) | |
book.get_copies | |
book.copies.length.should eq 100 | |
end | |
it 'gets the book that has a matching id' do | |
book = Book.new('this', 123, 'ferguson', 'steven', 1) | |
book.save | |
Book.find(book.id).should eq book | |
end | |
end | |
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 CheckOut | |
attr_reader :copy_id, :member_id, :due_date, :id | |
def initialize(copy_id, member_id) | |
@copy_id = copy_id | |
@member_id = member_id | |
create_due_date | |
end | |
def create_due_date | |
@due_date = Date.today + 7 | |
end | |
def set_due_date(date) | |
@due_date = date | |
end | |
def set_id(id) | |
@id = id | |
end | |
def save | |
result = DB.exec("INSERT INTO check_outs (copy_id, member_id, due_date) VALUES (#{@copy_id}, #{@member_id}, '#{due_date.to_s}') RETURNING id; ") | |
id = result.first['id'].to_i | |
set_id(id) | |
# copy = Copy.find(@copy_id) | |
# copy.check_out | |
end | |
def delete | |
DB.exec("DELETE FROM check_outs WHERE id = #{@id};") | |
end | |
def self.all | |
results = DB.exec("SELECT * FROM check_outs;") | |
check_outs = [] | |
results.each do |result| | |
copy_id = result['copy_id'].to_i | |
member_id = result['member_id'].to_i | |
due_date = Date.parse(result['due_date']) | |
id = result['id'].to_i | |
check_out = CheckOut.new(copy_id, member_id) | |
check_out.set_due_date(due_date) | |
check_out.set_id(id) | |
check_outs << check_out | |
end | |
check_outs | |
end | |
end |
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
require 'spec_helper' | |
describe CheckOut do | |
it 'initializes with a copy id and member id' do | |
check_out = CheckOut.new(12, 234) | |
check_out.should be_an_instance_of CheckOut | |
end | |
it 'tells the copy id' do | |
check_out = CheckOut.new(12, 234) | |
check_out.copy_id.should eq 12 | |
end | |
it 'tells you the member id' do | |
check_out = CheckOut.new(12, 234) | |
check_out.member_id.should eq 234 | |
end | |
it 'creates the due date' do | |
check_out = CheckOut.new(12, 234) | |
check_out.create_due_date | |
check_out.due_date.should eq Date.today + 7 | |
end | |
it 'lets you set the due date' do | |
check_out = CheckOut.new(12, 234) | |
check_out.set_due_date(Date.today) | |
check_out.due_date.should eq Date.today | |
end | |
it 'lets you save the check_out to the database' do | |
check_out = CheckOut.new(12, 1234) | |
check_out.save | |
CheckOut.all.first.copy_id.should eq 12 | |
end | |
it 'sets the id when saved' do | |
check_out = CheckOut.new(12, 34) | |
check_out.save | |
CheckOut.all.first.id.should be_an_instance_of Fixnum | |
end | |
it 'lets you set the id' do | |
check_out = CheckOut.new(12, 1234) | |
check_out.set_id(11) | |
check_out.id.should eq 11 | |
end | |
it 'lets you delete it from the database' do | |
check_out = CheckOut.new(12, 1234) | |
check_out.save | |
check_out.delete | |
CheckOut.all.should eq [] | |
end | |
it 'marks the copy as checked out in the database when saved' do | |
copy = Copy.new(1) | |
copy.save | |
check_out = CheckOut.new(copy.id, 1) | |
check_out.save | |
Copy.all.first.checked_out.should eq 'yes' | |
end | |
end | |
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 Copy | |
attr_reader :id, :checked_out, :book_id | |
def initialize(book_id) | |
@book_id = book_id | |
@checked_out = 'no' | |
end | |
def set_id(id) | |
@id = id | |
end | |
def ==(another_copy) | |
@id == another_copy.id | |
end | |
def save | |
results = DB.exec("INSERT INTO copies (book_id, checked_out) VALUES (#{@book_id}, '#{@checked_out}') RETURNING id;") | |
id = results.first['id'].to_i | |
set_id(id) | |
end | |
def delete | |
DB.exec("DELETE FROM copies * WHERE id = #{id};") | |
end | |
def check_out | |
@checked_out = 'yes' | |
DB.exec("UPDATE copies SET checked_out = 'yes' WHERE id = #{@id};") | |
end | |
def set_checked_out(status) | |
@checked_out = status | |
end | |
def self.find(copy_id) | |
result = DB.exec("SELECT * FROM copies WHERE id = #{copy_id};") | |
book_id = result.first['book_id'].to_i | |
id = result.first['id'].to_i | |
copy = Copy.new(book_id) | |
copy.set_id(id) | |
copy | |
end | |
def self.all | |
results = DB.exec("SELECT * FROM copies;") | |
copies = [] | |
results.each do |result| | |
book_id = result['book_id'].to_i | |
checked_out = result['checked_out'] | |
id = result['id'].to_i | |
copy = Copy.new(book_id) | |
copy.set_id(id) | |
copy.set_checked_out(checked_out) | |
copies << copy | |
end | |
copies | |
end | |
end |
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
require 'spec_helper' | |
describe Copy do | |
it 'initializes with a book_id' do | |
copy = Copy.new(10) | |
copy.should be_an_instance_of Copy | |
end | |
it 'lets you set the id' do | |
copy = Copy.new(10) | |
copy.set_id(1) | |
copy.id.should eq 1 | |
end | |
it 'is eqaul if it has the same id' do | |
copy1 = Copy.new(10) | |
copy1.set_id(2) | |
copy2 = Copy.new(10) | |
copy2.set_id(2) | |
copy1.should eq copy2 | |
end | |
it 'lets you save the copy to the database' do | |
copy = Copy.new(10) | |
copy.save | |
Copy.all.length.should eq 1 | |
end | |
it 'sets the id when being saved' do | |
copy = Copy.new(9) | |
copy.save | |
Copy.all.first.id.should be_an_instance_of Fixnum | |
end | |
it 'lets you delete the copy from the database' do | |
copy = Copy.new(1) | |
copy.save | |
Copy.all.first.delete | |
Copy.all.should eq [] | |
end | |
it 'has a default value of no for checked_out' do | |
copy = Copy.new(9) | |
copy.checked_out.should eq 'no' | |
end | |
it 'can be checked_out' do | |
copy = Copy.new(1) | |
copy.save | |
copy.check_out | |
copy.checked_out.should eq 'yes' | |
end | |
it 'updates the checked out status on the database' do | |
copy = Copy.new(1) | |
copy.save | |
copy.check_out | |
Copy.all.first.checked_out.should eq 'yes' | |
end | |
it 'gets the copy with the matching id' do | |
copy = Copy.new(1) | |
copy.save | |
Copy.find(copy.id).should eq copy | |
end | |
end |
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
require './lib/book' | |
require './lib/copy' | |
require './lib/member' | |
require './lib/check_out' | |
require 'date' | |
require 'pg' | |
DB = PG.connect(:dbname => 'library') | |
def welcome | |
puts "\e[H\e[2J" | |
puts "Welcome to the Library App\n" | |
main_menu | |
end | |
def main_menu | |
puts "Enter 1 if you are an admin" | |
puts "Enter 2 if you are a member" | |
user_choice = gets.to_i | |
if user_choice == 1 | |
admin_menu | |
elsif user_choice == 2 | |
show_all_members | |
puts "Enter the number that is next to your name" | |
member = Member.all[gets.to_i - 1] | |
member_menu(member) | |
else | |
puts "I'm sorry I did not understand." | |
main_menu | |
end | |
end | |
def admin_menu | |
puts "Enter 1 to add a book." | |
puts "Enter 2 to delete a book." | |
puts "Enter 3 to add a new member." | |
puts "Enter 4 to delete a member." | |
# puts "Enter 5 to edit a book or change quantity." | |
puts "Enter 6 to go back" | |
user_choice = gets.to_i | |
if user_choice == 1 | |
add_book | |
elsif user_choice == 2 | |
delete_book | |
elsif user_choice == 3 | |
add_member | |
elsif user_choice == 4 | |
delete_member | |
# elsif user_choice == 5 | |
# edit_book | |
elsif user_choice == 6 | |
puts "\n\n" | |
main_menu | |
else | |
"I did not understand." | |
admin_menu | |
end | |
end | |
def add_book | |
print "Enter the Title: " | |
title = gets.chomp | |
print "\nEnter the Authors last name: " | |
last_name = gets.chomp | |
print "\nEnter the authors first name: " | |
first_name = gets.chomp | |
print "\nEnter ISBN: " | |
isbn = gets.chomp | |
print "\nHow many copies do you have? " | |
number_of_copies = gets.to_i | |
book = Book.new(title, isbn, last_name, first_name, number_of_copies) | |
book.save | |
book.save_copies(number_of_copies) | |
puts "#{number_of_copies} copies of '#{title}' have been added!" | |
admin_menu | |
end | |
def delete_book | |
show_all_books | |
puts "\nSelect a book to delete" | |
book = Book.all[gets.to_i - 1] | |
show_all_copies(book) | |
puts "\nSelect a copy to delete" | |
copy = book.copies[gets.to_i - 1] | |
copy.delete | |
puts "\nCopy number #{copy.id} of #{book.title} has been deleted." | |
admin_menu | |
end | |
def add_member | |
print "\n Enter the new member's first name: " | |
first_name = gets.chomp | |
print "\n Enter the new member's last name: " | |
last_name = gets.chomp | |
member = Member.new(first_name, last_name) | |
member.save | |
puts "New member, #{member.first_name} #{member.last_name} has been created!\n" | |
puts "ID for #{member.first_name} #{member.last_name} is: #{member.id}\n\n" | |
admin_menu | |
end | |
def delete_member | |
show_all_members | |
puts "\nSelect a member to delete" | |
member = Member.all[gets.to_i - 1] | |
member.delete | |
puts "#{member.last_name}, #{member.first_name} has been deleted." | |
admin_menu | |
end | |
def member_menu(member) | |
puts "\n\n #{member.first_name} #{member.last_name}" | |
puts "Enter 1 to check out a book." | |
puts "Enter 2 to return a book." | |
puts "Enter 3 to go back" | |
user_choice = gets.to_i | |
if user_choice == 1 | |
check_out_book(member) | |
elsif user_choice == 2 | |
return_book(member) | |
elsif user_choice == 3 | |
main_menu | |
else | |
puts "I did not understand." | |
member_menu(member) | |
end | |
end | |
def check_out_book(member) | |
show_all_books | |
puts "#{member.id}" | |
puts "\nWhich book would you like to borrow?" | |
book = Book.all[gets.to_i - 1] | |
show_all_copies(book) | |
puts "\nSelect which copy you want to check out" | |
copy = book.copies[gets.to_i - 1] | |
member.check_out(copy) | |
puts "Congrats buddy, you have checked out copy number #{copy.id} of #{book.title}." | |
member_menu(member) | |
end | |
def return_book(member) | |
puts "\n\n" | |
show_member_check_outs(member) | |
puts "Select a book to return:" | |
check_out = member.check_outs[gets.to_i - 1] | |
check_out.delete | |
puts "Book returned!" | |
member_menu(member) | |
end | |
def show_member_check_outs(member) | |
member.get_check_outs | |
member.check_outs.each_with_index do |check_out, index| | |
copy = Copy.find(check_out.copy_id) | |
book = Book.find(copy.book_id) | |
puts "#{index + 1}. #{book.title} - #{check_out.due_date.to_s} " | |
end | |
end | |
def show_all_books | |
Book.all.each_with_index do |book, index| | |
puts "#{index + 1}. #{book.title}, #{book.author_last_name}, #{book.author_first_name}, #{book.isbn}, #{book.number_of_copies}" | |
end | |
end | |
def show_all_copies(book) | |
book.get_copies | |
book.copies.each_with_index do |copy, index| | |
puts "#{index + 1}. #{copy.id}" | |
end | |
end | |
def show_all_members | |
Member.all.each_with_index do |member, index| | |
puts "#{index + 1}. #{member.last_name}, #{member.first_name}" | |
end | |
end | |
welcome |
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 Member | |
attr_reader :first_name, :last_name, :id, :check_outs | |
def initialize(first_name, last_name) | |
@first_name = first_name | |
@last_name = last_name | |
@check_outs = [] | |
end | |
def set_id(id) | |
@id = id | |
end | |
def save | |
result = DB.exec("INSERT INTO members (first_name, last_name) VALUES ('#{first_name}', '#{last_name}') RETURNING id;") | |
id = result.first['id'].to_i | |
set_id(id) | |
end | |
def delete | |
DB.exec("DELETE FROM members * WHERE id = #{@id};") | |
end | |
def self.all | |
results = DB.exec("SELECT * FROM members;") | |
members = [] | |
results.each do |result| | |
first_name = result['first_name'] | |
last_name = result['last_name'] | |
id = result['id'].to_i | |
member = Member.new(first_name, last_name) | |
member.set_id(id) | |
members << member | |
end | |
members | |
end | |
def check_out(copy) | |
check_out = CheckOut.new(copy.id, @id) | |
check_out.save | |
end | |
def get_check_outs | |
@check_outs = [] | |
results = DB.exec("SELECT * FROM check_outs WHERE member_id = #{@id};") | |
results.each do |result| | |
copy_id = result['copy_id'].to_i | |
id = result['id'].to_i | |
due_date = Date.parse(result['due_date']) | |
check_out = CheckOut.new(copy_id, @id) | |
check_out.set_due_date(due_date) | |
check_out.set_id(id) | |
@check_outs << check_out | |
end | |
end | |
end | |
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
require 'spec_helper' | |
describe Member do | |
it 'initializes with first and last name' do | |
member = Member.new('mac', 'eisenberg') | |
member.should be_an_instance_of Member | |
end | |
it 'lets you save to the database' do | |
member = Member.new('mac', 'attack') | |
member.save | |
Member.all.length.should eq 1 | |
end | |
it 'sets the id when saved' do | |
member = Member.new('big', 'mac') | |
member.save | |
Member.all.first.id.should be_an_instance_of Fixnum | |
end | |
it 'sets the id' do | |
member = Member.new('stevO', 'leevo') | |
member.set_id(770) | |
member.id.should eq 770 | |
end | |
it 'can check out a copy' do | |
member = Member.new('mike', 'jones') | |
member.save | |
copy = Copy.new(1) | |
copy.save | |
Member.all.first.check_out(copy) | |
CheckOut.all.length.should eq 1 | |
end | |
it 'can get all the check outs for a member' do | |
member = Member.new('mike', 'jones') | |
member.save | |
copy = Copy.new(1) | |
copy.save | |
member.check_out(copy) | |
member.get_check_outs | |
member.check_outs.first.copy_id.should eq copy.id | |
end | |
it 'deletes a member' do | |
member = Member.new('mike', 'jones') | |
member.save | |
member.delete | |
Member.all.should eq [] | |
end | |
end | |
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
require 'rspec' | |
require 'book' | |
require 'copy' | |
require 'member' | |
require 'check_out' | |
require 'pg' | |
DB = PG.connect(:dbname => 'library_test') | |
RSpec.configure do |config| | |
config.after(:each) do | |
DB.exec("DELETE FROM books *;") | |
DB.exec("DELETE FROM copies *;") | |
DB.exec("DELETE FROM members *;") | |
DB.exec("DELETE FROM check_outs *;") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment