repl.it
@bennypr0fane/

Tome Rater

Python

Codecademy capstone project

fork
loading
Files
  • main.py
  • populate.py
main.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
def get_keys_by_value(dictionary, value_to_find):
    list_of_keys = []
    list_of_items = dictionary.items()
    for item  in list_of_items:
        if item[1] == value_to_find:
            list_of_keys.append(item[0])
    return  list_of_keys


class User(object):
    def __init__(self, name, email):
        self.name = name
        self.email = email
        self.books = {}
        self.average_rating = self.get_average_rating(self.books)

    def get_email(self):
        return self.email

    def change_email(self, address):
        self.email = address
        print("E-mail address has been updated\n")
        return address

    def __repr__(self):
        return "user: {name}\ne-mail: {email}\nbooks read: {books}".format(name=self.name, email = self.email, books=self.books)

    def __eq__(self, other_user):
        if self.name == other_user.name and self.email == other_user.email:
            print("identical users!\n")
            return True
        
    def read_book(self, book, rating=None):
        self.books[book.title] = rating
        if rating:
            book.add_rating(rating)
        
    def get_average_rating(self, books):
        ratings_total = 0
        for book in self.books:
            ratings_total += self.books[book]
        if len(self.books) > 0:
          average = ratings_total / len(self.books)
        else:
          average = None
        return average

class Book:
    def __init__(self, title, isbn):
        self.title = title
        self.isbn = isbn
        self.ratings = []
        
    
    def get_title(self):
        return self.title
    
    def get_isbn(self):
        return self.isbn
    
    def set_isbn(self, new_isbn):
        self.isbn = new_isbn
        print("ISBN has been updated\n")
    
    def add_rating(self, rating=0):
        self.ratings = []
        if 0 <= rating < 5:
            self.ratings.append(rating)
        else:
            print("Invalid rating. Valid ratings are numbers are from 0 to 4.\n")
     
    def get_average_rating(self):
        if len(self.ratings) < 1:
          return
        ratings_total = 0
        for rating in self.ratings:
            ratings_total += rating or 0
        average = ratings_total / len(self.ratings)
        return average
          
    def __eq__(self, other_book):
        if self.title == other_book.title and self.isbn == other_book.isbn:
            print("identical books!\n")
            return True
     
    def __hash__(self):
        return hash((self.title, self.isbn))

    def __repr__(self):
        return "{title}\nrating: {rating}".format(title=self.title, rating=self.get_average_rating())


class Fiction(Book):
    def __init__(self, title, author, isbn):
        super().__init__(title, isbn)
        self.author = author
    
    def get_author(self):
        return self.author
    
    def __repr__(self):
        return "{title} by {author}\nrating: {rating}".format(title=self.title, author=self.author, rating=self.get_average_rating())


class NonFiction(Book):
    def __init__(self, title, subject, level, isbn):
        super().__init__(title, isbn)
        self.subject = subject
        self.level = level

    def get_subject(self):
        return subject

    def get_level(self):
        return level

    def __repr__(self):
        return "{title}, a {level} book on {subject}\nrating: {rating}".format(title=self.title, level=self.level, subject=self.subject, rating = self.get_average_rating())
 
 
class TomeRater:
    def __init__(self, name):
        self.name = name
        self.users = {}
        self.books = {}
        self.isbns = []
    
    def __repr__(self):
        return "**{name} book rating database**\nNumber of users: {users}\nNumber of books: {books}".format(name=self.name, users=len(self.users), books=len(self.books))
    
    def create_book(self, title, isbn):
        if isbn in self.isbns:
            print("This book already exists in our database.")
        else:
            self.isbns.append(isbn)
            print("New book added.")
            return Book(title, isbn)
    
    def create_fiction(self, title, author, isbn):
        if isbn in self.isbns:
            print("This book already exists in our database.")
        else:
            self.isbns.append(isbn)
            print("New fiction book added")
            return Fiction(title, author, isbn)
    
    def create_non_fiction(self, title, subject, level, isbn):
        if isbn in self.isbns:
            print("This book already exists in our database.")
        else:
            self.isbns.append(isbn)
            print("New non-fiction book added")
            return NonFiction(title, subject, level, isbn)
    
    def add_book_to_user(self, book, email, rating=0):
        if email not in self.users:
            print("No user with email {email}!\n".format(email=email))
        else:    
            user = self.users.get(email)
            user.read_book(book, rating)
            book.add_rating(rating)
            print("Added \'{}\' to user {}.".format(book.title, email))
            if book not in self.books:
                self.books[book] = 1
            else:
                self.books[book] += 1
    
    def add_user(self, name, email, user_books=None):
        if email in self.users.keys():
            print("User with email {email} already exists.".format(email=email))
            return
        elif "@" not in email:
            print("Please enter a valid e-mail address.")
            return
        tlds = [".com", ".edu", ".org"]
        for i in tlds:
            if i in email:
                self.users.update({email: User(name, email)})
                print("New user added")
                if user_books:
                    for book in user_books:
                        self.add_book_to_user(book, email)
                return User(name, email)
        else:
            print("Please enter a valid e-mail address.")
 
    def print_catalog(self):
        if list(self.books.keys()) == []:
                print("No books yet.")
        else:
            print("**Catalog:**")
            for book in self.books:
                print(book)
            print("**End of catalog**\n")
            
    def print_users(self):
        if list(self.users.values()) == []:
                print("No users yet.")
        else:
            print("**List of users:**")
            for user in self.users.values():
                print(user)
            print("**End of user list**\n")
    
    def most_read_book(self):
        print("Most read book:")
        most_read = 0
        for book in self.books:
            if self.books[book] > most_read:
                most_read = self.books[book]
        return get_keys_by_value(self.books, most_read)
 
    def highest_rated_book(self):
        print("Highest rated book:")
        highest_av_rating = 0
        best_book = None
        for book in self.books:
            average_rating = book.get_average_rating()
            if average_rating > highest_av_rating:
                highest_av_rating = average_rating
                best_book = book
        return best_book
    
    def most_positive_user(self):
        print("Most positive user:")
        highest_rating = 0
        most_positive = None
        for user in self.users.values():
            if len(user.books) > 2:
                average_rating = user.get_average_rating(user.books)
                if average_rating > highest_rating:
                    highest_rating = average_rating
                    most_positive = user
        print("Average rating: " + str(highest_rating))
        return most_positive


Tome_Rater = TomeRater("Good Reads")

#Create some books:
book1 = Tome_Rater.create_book("Society of Mind", 12345678)
novel1 = Tome_Rater.create_fiction("Alice In Wonderland", "Lewis Carroll", 12345)
novel1.set_isbn(9781536831139)
nonfiction1 = Tome_Rater.create_non_fiction("Automate the Boring Stuff", "Python", "beginner", 1929452)
nonfiction2 = Tome_Rater.create_non_fiction("Computing Machinery and Intelligence", "AI", "advanced", 11111938)
novel2 = Tome_Rater.create_fiction("The Diamond Age", "Neal Stephenson", 10101010)
novel3 = Tome_Rater.create_fiction("There Will Come Soft Rains", "Ray Bradbury", 10001000)

#Create users:
Tome_Rater.add_user("Alan Turing", "[email protected]")
Tome_Rater.add_user("David Marr", "[email protected]")

#Add a user with three books already read:
Tome_Rater.add_user("Marvin Minsky", "[email protected]", user_books=[book1, novel1, nonfiction1])

#Add books to a user one by one, with ratings:
Tome_Rater.add_book_to_user(book1, "[email protected]", 1)
Tome_Rater.add_book_to_user(novel1, "[email protected]", 3)
Tome_Rater.add_book_to_user(nonfiction1, "[email protected]", 3)
Tome_Rater.add_book_to_user(nonfiction2, "[email protected]", 4)
Tome_Rater.add_book_to_user(novel3, "[email protected]", 1)

Tome_Rater.add_book_to_user(novel2, "[email protected]", 2)
Tome_Rater.add_book_to_user(novel3, "[email protected]", 2)
Tome_Rater.add_book_to_user(novel3, "[email protected]", 4)

Tome_Rater.print_catalog()
Tome_Rater.print_users()

print(Tome_Rater.most_positive_user())
print(Tome_Rater.highest_rated_book())
print(Tome_Rater.most_read_book())