-
Notifications
You must be signed in to change notification settings - Fork 0
/
book.py
77 lines (63 loc) · 2.07 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
74
75
76
77
class Book:
def __init__(self, title, content):
self.__title = title
self.__book_file = content.read().strip()
self.__content = [self.__book_file[i:i+200].strip() for i in range(0, len(self.__book_file), 200)]
self.__number_of_pages = len(self.__content)
self.__current_page = 1
self.__percentage_read = 0
# Getter Functions
def GetTitle(self):
return self.__title
def GetContent(self):
return self.__content
def GetCurrentPage(self):
return self.__current_page
def GetNumberOfPages(self):
return self.__number_of_pages
def GetPercentageRead(self):
# Setting initial percentage read to 0%.
if self.GetCurrentPage() <= 1:
return 0
else:
return int((self.GetCurrentPage() / self.GetNumberOfPages()) * 100)
def DisplayPage(self):
print("\n")
print(self.GetTitle())
print(self.__content[self.GetCurrentPage() - 1])
print(self.GetCurrentPage())
return ""
# Setters
def SetPercentageRead(self, new_percentage):
self.__percentage_read = new_percentage
# This works with both Rewind and Forward functions
def SetCurrentPage(self, action):
if action == "+":
self.__current_page += 1
elif action == "-":
self.__current_page -= 1
else:
self.__current_page = action
def Forward(self):
if self.GetCurrentPage() == self.__number_of_pages - 1:
print("THE END! You've reached the last page of the book!")
else:
self.SetCurrentPage("+")
self.DisplayPage()
return ""
def Rewind(self):
if self.GetCurrentPage() == 0:
print("You've already reached the first page.")
else:
self.SetCurrentPage("-")
self.DisplayPage()
return ""
# The following function was created for testing purposes only. Please ignore it.
# def FormatBook(self):
# return {
# "Title": self.GetTitle(),
# "Content": self.GetContent(),
# "Number_of_pages": self.GetNumberOfPages(),
# "Current_page": self.GetCurrentPage(),
# "Percentage_read": self.GetPercentageRead()
# }