OneCompiler

libraryDB

187
'''
1. Create a class called Library
2. that manages a collection of books.
3. Include methods to add a book,

books = {
    title : {
        author:
        genre:
        year:
        total:
    }
}

4. remove a book,
5. and display all books in the library.
'''


class Library:
    def __init__(self, books):
        self.books = books

    def add(self, title, values):
        # book already exists
        if title in self.books:
            # total++
            self.books[title]["total"] += 1
        # first added
        else:
            # total = 1
            # title : values -> in books
            self.books[title] = values

    def remove(self, title, amount):
        # if book is valid = we have the book in self.books
        if title in self.books:
            # if amount is valid: total >= amount
            if self.books[title]["total"] >= amount:
                self.books[title]["total"] -= amount
            else:
                return print("The amount is not valid")
        else:
            return print("The book is not valid or not in books collection")

    def info(self):
        for title in self.books:
            print(f"Title: {title}\n"
                  f"Author: {self.books[title]['author']}\n"
                  f"Genre: {self.books[title]['genre']}\n"
                  f"Year: {self.books[title]['year']}\n"
                  f"Total: {self.books[title]['total']}\n")


books = {
    "The days gone by": {
        "author": "Utkur Hoshimov",
        "genre": "Drama",
        "year": 1998,
        "total": 3
    },
    "A is for Alibi": {
            "author": "Agatha Christi",
            "genre": "Detective",
            "year": 2001,
            "total": 4
    }
}
library1 = Library(books)
title = "B is for Burglar"
values = {
        "author": "Utkur Hoshimov",
        "genre": "Drama",
        "year": 1998,
        "total": 3
}
library1.info()
print("-----------")
library1.add(title, values)
library1.info()
print("-----------")
library1.remove("A is for Alibi", 1)
library1.remove(title, 5)
library1.info()
print("-----------")