-
Notifications
You must be signed in to change notification settings - Fork 0
/
book.py
73 lines (68 loc) · 2.5 KB
/
book.py
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
64
65
66
67
68
69
70
71
72
73
class Book:
"""Represents a Book with title, author, price, ISBN, genre, and year."""
def __init__(self, title: str = "", author: str = "", price: float = 0.0, isbn: str = "", genre: str = "", year: int = 0) -> None:
"""
This is the constructor for the Book class
:param title: title of the Book
:param author: author of the Book
:param price: price of the Book
:param isbn: isbn of the Book
:param genre: genre of the Book
:param year: publication year of the Book
"""
self.title = title
self.author = author
self.setPrice(price)
self.setIsbn(isbn)
self.setGenre(genre)
self.setYear(year)
def __copy__(self) -> Book:
"""
This method creates a shallow copy of the Book object
:return: shallow copy of the Book object
"""
return type(self)(self.title, self.author, self.price, self.isbn, self.genre, self.year)
# accessor methods
def getTitle(self) -> str:
return self.title
def getAuthor(self) -> str:
return self.author
def getPrice(self) -> float:
return self.price
def getIsbn(self) -> str:
return self.isbn
def getGenre(self) -> str:
return self.genre
def getYear(self) -> int:
return self.year
# mutator methods
def setTitle(self, title: str) -> None:
self.title = title
def setAuthor(self, author: str) -> None:
self.author = author
def setPrice(self, price: float) -> None:
self.price = price
def setIsbn(self, isbn: str) -> None:
self.isbn = isbn
def setGenre(self, genre: str) -> None:
self.genre = genre
def setYear(self, year: int) -> None:
self.year = year
# equals
def __eq__(self, obj) -> bool:
"""
This method checks if two Book objects are equal
:param obj: another object to compare
:return: True if equal, False otherwise
"""
if not isinstance(obj, Book):
return False
return self.title == obj.title and self.author == obj.author and self.price == obj.price and self.isbn == obj.isbn \
and self.genre == obj.genre and self.year == obj.year
# toString
def __str__(self) -> str:
"""
This method returns the Book object as a string.
:return: string representation of the Book object
"""
return f"{self.title}, {self.author}, {self.price}, {self.isbn}, {self.genre}, {self.year}"