models.py file;
class Library(object):
def __init__(self, name, budget):
self.name = name
self.remaining_balance = budget
def funds_available(self, cost):
if self.remaining_balance - cost > 0:
return True
else:
print 'Insufficient funds'
return False
def open_new_branch(self, location, budget):
if self.funds_available(budget):
self.remaining_balance -= budget
return Branch(location, self.name, budget)
class Branch(Library):
def __init__(self, location, *args, **kwargs):
self.location = location
self.books = []
super(Branch, self).__init__(*args, **kwargs)
def funds_available(self, cost):
return super(Branch, self).funds_available(cost)
def purchase_book(self, title, isbn, genre, price):
if self.funds_available(price):
self.remaining_balance -= price
book = LibraryBook(1, title, isbn, genre)
self.books.append(book)
def loan_book(self, isbn, patron):
for book in self.books:
if book.isbn == isbn:
print '"%s" found in catalog' % book.title
if book.status == 1:
book.status = 0
book.patron = patron
print '"%s" loaned to %s' % (book.title, patron)
else:
print 'book is currently unavailable'
def place_hold(self, isbn, patron):
for book in self.books:
if book.isbn == isbn:
print '"%s" found in catalog' % book.title
if book.status == 1:
book.status = 2
book.patron = patron
print '"%s" placed on hold for %s' % (book.title, patron)
else:
print 'book is currently unavailable'
def return_book(self, isbn):
for book in self.books:
if book.isbn == isbn:
print '"%s" found in catalog' % book.title
book.status = 1
book.patron = None
print '"%s" returned' % book.title
def list_books(self):
print 'Status | Title'
print '-'*40
for book in self.books:
print '%12s | %s' % (book.get_status(), book.title)
class Book(object):
def __init__(self, title, isbn, genre):
self.title = title
self.isbn = isbn
self.genre = genre
class LibraryBook(Book):
def __init__(self, status, *args, **kwargs):
self.STATUSES = {
0: 'unavailable',
1: 'on shelf',
2: 'hold'
}
self.status = status
self.patron = None
super(LibraryBook, self).__init__(*args, **kwargs)
def get_status(self):
return self.STATUSES[self.status]
sample.py file;
from models import Library, Branch, LibraryBook
main_library = Library('Chicago Library', 100000)
edgewater_branch = main_library.open_new_branch('Edgewater', 25000)
edgewater_branch.purchase_book('History of Rome', '012345-1234', 'History', 150)
edgewater_branch.purchase_book('Scarlet Letter', '2222-1234', 'Fictional Literature', 1)
edgewater_branch.purchase_book('Starship Troopers', '3333-1234', 'Science Fiction', 15)
edgewater_branch.purchase_book('Weird Illinois', '0444-1234', 'Special Interest', 45)
edgewater_branch.purchase_book('The Holy Bible', '0555-1234', 'Religious Texts', 12)
edgewater_branch.purchase_book('The Holy Quran Translation', '0678-1234', 'Religious Texts', 12)
edgewater_branch.purchase_book('GSU Student Catalog', '0901-1234', 'Education Resources', 0)
edgewater_branch.loan_book('3333-1234', 'Dan')
edgewater_branch.loan_book('0444-1234', 'Dan')
edgewater_branch.loan_book('2222-1234', 'Dan')
edgewater_branch.place_hold('0901-1234', 'Dan')
edgewater_branch.list_books()
print '\n\n'
edgewater_branch.return_book('2222-1234')
edgewater_branch.list_books()