Skip to content

wwcodekl/itp-w2-bookstore-management-system

 
 

Repository files navigation

Bookstore management system

Today you're in charge of building a simple Bookstore system. It will store authors and books, just like a bookstore would! We will be representing these items using dictionaries. It's important when writing code for this that you make sure that the interface works as expected. That means reading and understanding the unit tests in tests/test_bookstore.py to dictate how you should design your code.

There are two hints in this file directory... please don't use them unless you get stuck for a while.

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. In this project, we will model the bookstore after a dictionary. We need it to store the bookstore's name, all the authors in it, and all the the books in it. You will have to choose how you want to store this information.

bookstore = create_bookstore("Rmotr's bookstore")

If you look at the empty functions in bookstore.py, every one of them after create_bookstore receives bookstore as the first parameter. For this project, a bookstore is created in the unit tests and treated like a global dictionary in your tests. Remember how your bookstore is a dictionary? Dictionaries are mutable. That means if we pass your bookstore dictionary to each of the other functions, if the function changes or adds a value to the dictionary, that change remains after the function ends. Use this knowledge to store and access books and authors in your functions. :)

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. The add_author function receives the bookstore dictionary, author name, and nationality. You should autogenerate an ID whenever we add an author to the library. The ID is unique and can be whatever you want as long as it is different for every author. We'll use that ID later. All this information is stored in the author dictionary. Note the keys must match what is in the unit tests. Make sure to store your author in your bookstore dictionary and pay attention if this function returns something or not (you can tell from this example above).

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:

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

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

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

This is similar to how it was done in authors.

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

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 89.9%
  • Makefile 10.1%