/itp-w2-bookstore-management-system

Primary LanguagePythonCreative Commons Attribution Share Alike 4.0 InternationalCC-BY-SA-4.0

Bookstore management system

You're in charge of building a simple Bookstore system. We'll give you full control over the design of this system, we'll just make sure that the interface works as expected. Discuss with your team what's the best way to design this particular system. Let's start describing it.

Creating a bookstore

The first thing we need to do is "create a bookstore". We'll then use the bookstore to add authors or books. What "is" a bookstore? Well, you know that. Honestly, we don't care. Do you want to make the bookstore a dictionary? That's fine. A list? Also fine. We just care about the interface. As long as we can add authors and books later, it's fine.

bookstore = create_bookstore("Rmotr's bookstore")

Adding authors

Once you create a bookstore, you can add authors to it. For example:

poe = add_author(bookstore, 'Edgar Allan Poe', 'US')
print(poe['name'])
print(poe['nationality'])
print(poe['id'])

Authors are represented as dictionaries and you should autogenerate an ID whenever we add an author to the library. We'll use that ID later.

Adding books

We'll also be able to add books to our bookstore. Here's one of the places we'll need the previously autogenerated author's ID. When we want to add a book to our bookstore, we'll use the following information:

  • Book's title
  • Book's ISBN
  • Book's author

The way for us to indicate the author is by specifying the author's id. Example:

bookstore = create_bookstore("Rmotr's bookstore")
poe = add_author(bookstore, 'Edgar Allan Poe', 'US')
# We have now the author's ID: poe['id']

# We now add our book:
raven = add_book(bookstore, 'The Raven', 'XXX-1', poe['id'])  # note the ID being used?
print(raven['id']  # A book also has an autogenerated ID

Please note that books also get an autogenerated ID when they're added to the bookstore

#### Getting books and authors by ID

Whenever you add books or authors to the bookstore, you should obviously keep track of those added objects. There are two utility functions to retrieve information from books or authors by id. Following our previous examples we could do:

# Poe has ID 23
# The Raven has ID 1032

poe = get_author_by_id(bookstore, 23)
raven = get_book_by_id(bookstore, 1032)

print(poe['name'])    # 'Edgar Allan Poe'
print(raven['title']) # 'The Raven'

Searching books (by title and by author)

There's also a utility function to search a book by title. It's as simple as:

book = get_book_by_title(bookstore, 'raven')
print(book['title']) # 'The Raven'

In this case, the result of get_book_by_title is the same as doing get_book_by_id passing the book's ID.

We can also search for all the books by a given author. In this case, the result will be a list of books, not just one element. See the next example:

bookstore = create_bookstore("Rmotr's bookstore")
poe = add_author(bookstore, 'Edgar Allan Poe', 'US')

# We now add a couple of books:
raven = add_book(bookstore, 'The Raven', 'XXX-1', poe['id'])
valdemar = add_book(bookstore, 'The Facts in the Case of M. Valdemar', 'XXX-2', poe['id'])

# Finally, we search books by author:
poe_books = get_books_by_author(bookstore, poe['id'])
for book in poe_books:
    print(book['title']
# We should see printed out:
# The Raven
# The Facts in the Case of M. Valdemar