-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbook.py
More file actions
63 lines (52 loc) · 2.17 KB
/
Copy pathbook.py
File metadata and controls
63 lines (52 loc) · 2.17 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
class Book:
def __init__(self, title: str, author: str, isbn: str, genre: str,
copies_total: int, copies_available: int, published_year: int):
if not title or not str(title).strip():
raise ValueError("Book title cannot be empty.")
if not author or not str(author).strip():
raise ValueError("Book author cannot be empty.")
if not isbn or not str(isbn).strip():
raise ValueError("Book ISBN cannot be empty.")
if copies_total < 0 or copies_available < 0:
raise ValueError("Copy counts cannot be negative.")
if copies_available > copies_total:
raise ValueError("copies_available cannot exceed copies_total.")
self.title = title
self.author = author
self.isbn = isbn
self.genre = genre
self.copies_total = copies_total
self.copies_available = copies_available
self.published_year = published_year
def is_available(self) -> bool:
return self.copies_available > 0
def matches(self, query: str) -> bool:
query_lower = query.lower().strip()
return (
query_lower in self.title.lower()
or query_lower in self.author.lower()
or query_lower in self.genre.lower()
)
def to_dict(self) -> dict:
return {
"title": self.title,
"author": self.author,
"isbn": self.isbn,
"genre": self.genre,
"copies_total": self.copies_total,
"copies_available": self.copies_available,
"published_year": self.published_year,
}
@classmethod
def from_dict(cls, data: dict) -> "Book":
return cls(
title=data["title"],
author=data["author"],
isbn=data["isbn"],
genre=data["genre"],
copies_total=data["copies_total"],
copies_available=data["copies_available"],
published_year=data["published_year"],
)
def __repr__(self) -> str:
return f"Book(title={self.title!r}, isbn={self.isbn!r}, available={self.copies_available}/{self.copies_total})"