Skip to content

Instantly share code, notes, and snippets.

@shamikalashawn
Created February 1, 2017 03: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 shamikalashawn/eba0b25521d96466b582688a0b94adb9 to your computer and use it in GitHub Desktop.
Save shamikalashawn/eba0b25521d96466b582688a0b94adb9 to your computer and use it in GitHub Desktop.
Managing a bookstore using procedure oriented programming. Add books, add authors, and search for books and authors!
def create_bookstore(name):
authors = []
books = []
bookstore = {'name': name, 'authors': authors, 'books' : books}
return bookstore
def add_author(bookstore, name, nationality):
ID = name[:3] + nationality
author = {'name': name, 'nationality': nationality, 'id': ID}
bookstore['authors'].append(author)
return author
def get_author_by_name(bookstore, name):
for author in bookstore['authors']:
if author['name'] == name:
return author
def get_author_by_id(bookstore, author_id):
for author in bookstore['authors']:
if author['id'] == author_id:
return author
def add_book(bookstore, title, isbn, author_id):
booklist = []
ID = title[:3] + isbn
book = {'title': title, 'isbn': isbn, 'id': ID, 'author': author_id}
bookstore['books'].append(book)
return book
def get_book_by_title(bookstore, title):
for book in bookstore['books']:
if book['title'] == title:
return book
def get_book_by_id(bookstore, book_id):
for book in bookstore['books']:
if book['id'] == book_id:
return book
def get_books_by_author(bookstore, author_id):
for book in bookstore['books']:
if book['author'] == author_id:
return book
store = create_bookstore('rmotr bookstore')
poe = add_author(store, 'Edgar Allan Poe', 'US')
borges = add_author(store, 'Jorge Luis Borges', 'AR')
joyce = add_author(store, 'James Joyce', 'UK')
author = get_author_by_name(store, 'James Joyce')
author = get_author_by_id(store, poe['id'])
raven = add_book(store, 'The Raven', 'XXX-1', poe['id'])
ulysses = add_book(store, 'Ulysses', 'XXX-2', joyce['id'])
ficciones = add_book(store, 'Ficciones', 'XXX-3', borges['id'])
aleph = add_book(store, 'El Aleph', 'XXX-4', borges['id'])
book = get_book_by_title(store, 'The Raven')
book = get_book_by_id(store, ulysses['id'])
books = get_books_by_author(store, borges['id'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment