Library

Backgroud

Library is a popular place to get a book when you need one. You might use it to get school books, or just to borrow a book to read. But have you ever wondered how does the registration app works? Or what happends when you borrow a book?

In a file called library.py, implement a program in Python where you would simulate that app, you should do this by rewriting the distobution code bellow. The program should have two classes Book and Library.

Understanding

Each book has a title, author and a genre.

A book can be borrowed, and returned. Also you can display info about each book would be structured as title by author.

In a Library you can add a book to its collection. Find a book by its title and author. Find all books that are available, borrowed or a spesific genre.

Before You Begin

Execute cd by itself in your terminal window. You should find that your terminal window’s prompt resembles the below:

1
$ 
Next execute
1
mkdir library 
to make a folder called library in your codespace. Then execute
1
cd library 
to change the directories into that folder. You should now see your terminal prompt as library/ . You can now execute
code library.py
to make a file called
library.py` where you'll write your programm.

Specification

Book Class

Library Class - __init__ should have an atribute to store all books called books. - add_book method should append the book given to books. - find_book method should find the book given title and author and return it, else should return None - list_available_books method should list all available books, if there are none it should return an empty list. - list_borrowed_books method should list all borrowed books, if there are none it should return an empty list. - list_genre_books method should list all books with the given genre, if there are none it should return an empty list.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
books = []

def Book(title, author, genre):
    return {"title": title, "author": author, "genre": genre, "is_available": True}

def add_book(book):
    books.append(book)

def find_book(title, author):
    for b in books:
        if b["title"].lower() == title.lower() and b["author"].lower() == author.lower():
            return b
    return None

def list_available_books():
    available = []
    for b in books:
        if b["is_available"]:
            available.append(b)
    return available

def list_borrowed_books():
    available = []
    for b in books:
        if not b["is_available"]:
            available.append(b)
    return available

def list_genre_books(genre):
    temp = []
    for b in books:
        if b["genre"].lower() == genre.lower():
            temp.append(b)
    return temp

def borrow(book):
    for b in books:
        if b["title"].lower() == book["title"].lower() and b["author"].lower() == book["author"].lower() and b["is_available"]:
            b["is_available"] = False

def return_book(book):
    for b in books:
        if b["title"].lower() == book["title"].lower() and not b["is_available"]:
            b["is_available"] = True 

def display_info(book):
    return f"{book["title"]} by {book["author"]}"

Rewrite the code into a Class version of it.

Hints

Class methods and instances https://docs.python.org/3/tutorial/classes.html#class-objects

How To Test

Here’s how to test your code manually:

Open your library.py file, create two Book instances (you can use this books as input: "The night, The fool and The Dead by Steve Cole, Scifi", "The Name of the Wind by Patrick Rothfuss, Fantasy"). Create a library instence.

Use the borrow method on a book.

Use the return_book method on a book.

If you run into an error saying your file cannot be opened, retrace your steps to be sure that you are inside your library folder and have saved your library.py file there.

You can execute the below to check your code using check50, a program that CS50 will use to test your code:

Coming soon

Visit the URL that check50 outputs to see the input check50 handed to your program, what output it expected, and what output your program actually gave.

How to Submit

Coming soon