Guest User

Untitled

a guest
Oct 16th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. class User(object):
  2. def __init__(self, name, email):
  3. self.name = name
  4. self.email = email
  5. self.books = {}
  6.  
  7. def get_email(self):
  8. return self.email
  9.  
  10. def change_email(self, address):
  11. self.email = address
  12. print("Your email has been updated to " + address)
  13.  
  14. def __repr__(self):
  15. return "User: {}, email: {}, books read: {}".format(self.name, self.email, self.books)
  16.  
  17. def __eq__(self, other_user):
  18. return self.name == other_user.name and self.email == other_user.email
  19.  
  20. def read_book(self, book, rating = None):
  21. #self.books = {}
  22. self.books.update({book : rating})
  23. return self.books
  24.  
  25. def get_average_rating(self):
  26. for value in self.books:
  27. average = sum(self.books)/len(self.books)
  28. return average
  29.  
  30.  
  31.  
  32. class Book(object):
  33. def __init__(self, title, isbn):
  34. self.title = title
  35. self.isbn = isbn
  36. self.ratings = []
  37.  
  38. def get_title(self):
  39. return self.title
  40.  
  41. def get_isbn(self):
  42. return self.isbn
  43.  
  44. def set_isbn(self, new_isbn):
  45. self.isbn = new_isbn
  46. print("This book's ISBN has been updated to " + str(new_isbn))
  47.  
  48. def add_rating(self, rating):
  49. if rating > 0 < 4:
  50. self.ratings.append(rating)
  51. else:
  52. print("Invalid Rating")
  53.  
  54. def __eq__(self, other_book):
  55. return self.title == other_book.title and self.isbn == other_book.isbn
  56.  
  57. def get_average_rating(self):
  58. for value in self.ratings:
  59. average = sum(self.ratings)/len(self.ratings)
  60.  
  61. def __hash__(self):
  62. return hash((self.title, self.isbn))
  63.  
  64.  
  65. class Fiction(Book):
  66. def __init__(self, title, author, isbn):
  67. super().__init__(title, isbn)
  68. self.author = author
  69.  
  70. def get_author(self):
  71. return self.author
  72.  
  73. def __repr__(self):
  74. return "{} by {}".format(self.title, self.author)
  75.  
  76. class Non_Fiction(Book):
  77. def __init__(self, title, subject, level, isbn):
  78. super().__init__(title, isbn)
  79. self.subject = subject
  80. self.level = level
  81.  
  82. def get_subject(self):
  83. return self.subject
  84.  
  85. def get_level(self):
  86. return self.level
  87.  
  88. def __repr__(self):
  89. return "{}, a {} manual on {}".format(self.title, self.level, self.subject)
  90.  
  91.  
  92.  
  93.  
  94. user1 = User("Sarah Lane", "sarahlane@gmail.com")
  95. user2 = User("Sarah Lane", "saralane@gmail.com")
  96. print(user1)
  97. print(user1.get_email())
  98. user1.change_email("sarah_help_me@gmail.com")
  99. print(user2)
  100. print(user2.__eq__(user1))
  101. book1 = Book("Coraline", 9780380807345)
  102. book2 = Book("The Witch-cult in Western Europe", 9781477456965)
  103. book2.set_isbn(123)
  104. fict1 = Fiction("Coraline", "Neil Gaiman", 9780380807345)
  105. print(fict1)
  106. nonfict1 = Non_Fiction("Society of Mind", "Artificial Intelligence", "beginner", 134567)
  107. print(nonfict1)
Add Comment
Please, Sign In to add comment