-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleAPI.py
More file actions
29 lines (23 loc) · 785 Bytes
/
SimpleAPI.py
File metadata and controls
29 lines (23 loc) · 785 Bytes
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
class Bookstore:
def __init__(self):
self.books = []
def add_book(self, id, title, author, year, genre):
new_book = {
"id": id,
"title": title,
"author": author,
"year": year,
"genre": genre
}
self.books.append(new_book)
return new_book
def get_book_by_id(self, id):
for book in self.books:
if book['id'] == id:
return book
return None
if __name__ == "__main__":
bookstore = Bookstore()
book1 = bookstore.add_book(1, "The Great Gatsby", "F. Scott Fitzgerald", 1925, "Fiction")
book2 = bookstore.add_book(2, "To Kill a Mockingbird", "Harper Lee", 1960, "Fiction")
print(bookstore.get_book_by_id(1)["title"])