-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoop.py
More file actions
55 lines (35 loc) · 1.32 KB
/
oop.py
File metadata and controls
55 lines (35 loc) · 1.32 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
""" class Book:
def __init__(self, title, quantity, author, price):
self.title = title
self.quantity = quantity
self.author = author
self.price = price
self.discount = 0.10
def __repr__(self):
return f"Book: {self.title}, Quantity: {self.quantity}, Author: {self.author}, Price: {self.price}"
book1 = Book('River between', 12, 'Ngugi wa thiongo', 120)
book2 = Book('Utengano', 18, 'Bin Abdalla', 220)
book3 = Book('Mayai, waziri wa marathi', 28, 'Author 3', 320)
print(book1)
print(book1.author)
print(book1.discount) """
class Book:
def __init__(self, title, quantity, author, price):
self.title = title
self.quantity = quantity
self.author = author
self.__price = price
self.__discount = None
def set_discount(self, discount):
self.__discount = discount
def get_price(self):
if self.__discount:
return self.__price * (1-self.__discount)
return self.__price
def __repr__(self):
return f"Book: {self.title}, Quantity: {self.quantity}, Author: {self.author}, Price: {self.get_price()}"
single_book = Book('Two States', 1, 'Chetan Bhagat', 200)
print(single_book)
bulk_books = Book('Two States', 25, 'Chetan Bhagat', 200)
bulk_books.set_discount(0.20)
print(bulk_books)