Advertisement
SimeonTs

SUPyF Basics OOP Principles - 02. Book Shop

Aug 12th, 2019
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.92 KB | None | 0 0
  1. """
  2. Basics OOP Principles
  3. Check your solution: https://judge.softuni.bg/Contests/Practice/Index/1556#1
  4.  
  5. SUPyF Basics OOP Principles - 02. Book Shop
  6.  
  7. Problem:
  8. You are working in a library. You are sick of writing descriptions for books by hand,
  9. so you wish to use the computer to speed up the process. The task is simple - your program should have two classes –
  10. one for the ordinary books – Book, and another for the special ones – GoldenEditionBook.
  11. So let’s get started! We need two classes:
  12. - Book - represents a book that holds title, author and price.
  13. A book should offer information about itself in the format shown in the output below.
  14. - GoldenEditionBook - represents a special book that holds the same properties as any Book,
  15.    but its price is always 30% higher.
  16.  
  17. Constraints:
  18. - If the author’s second name is starting with a digit –  the exception’s message is: "Author not valid!"
  19. - If the title’s length is less than 3 symbols –  the exception’s message is: "Title not valid!"
  20. - If the price is zero or it is negative – the exception’s message is: "Price not valid!"
  21. - Price must be formatted to two symbols after the decimal separator
  22.  
  23. Examples:
  24.    Input:
  25.        Ivo 4ndonov
  26.        Under Cover
  27.        9999999999999999999
  28.    Output:
  29.        Author not valid!
  30.  
  31.    Input:
  32.        Petur Ivanov
  33.        Life of Pesho
  34.        20
  35.    Output:
  36.        Type: Book
  37.        Title: Life of Pesho
  38.        Author: Petur Ivanov
  39.        Price: 20.00
  40.  
  41.        Type: GoldenEditionBook
  42.        Title: Life of Pesho
  43.        Author: Petur Ivanov
  44.        Price: 26.00
  45. """
  46.  
  47. """
  48. HELP:
  49. Step 1 - Create a Book Class
  50. Create a new empty class and name it Book.
  51.  
  52. Step 2 - Define the Properties of a Book
  53. Define the Title, Author and Price properties of a Book.
  54.  
  55. Step 3 - Define a Constructor
  56. Define a constructor that accepts author, title and price arguments.
  57.  
  58. Step 4 - Perform Validations
  59. Create a field for each property (Price, Title and Author) and perform validations for each one.
  60. The getter should return the corresponding field and the setter should validate the input data before setting it.
  61. Do this for every property.
  62.  
  63. Step 5 - Override __str__()
  64. We have already mentioned that all of the classes in C# inherit the System.Object class and therefore have
  65. all its public members.
  66. Let's override (change)  the ToString() method’s behavior again according to our Book class’s data.
  67.  
  68. And voila! If everything is correct, we can now create Book objects and display information about them.
  69.  
  70. Step 6 – Create a GoldenEditionBook
  71. Create a GoldenEditionBook class that inherits Book and has the same constructor definition. However,
  72. do not copy the code from the Book class - reuse the Book class constructor.
  73. There is no need to rewrite the Price,
  74. Title and Author properties since GoldenEditionBook inherits Book and by default has them.
  75.  
  76. Step 7 - Override the Price Property
  77. Golden edition books should return a 30% higher price than the original price.
  78. In order for the getter to return a different value, we need to override the Price property.
  79. Back to the GoldenEditionBook class, let's override the Price property and change the getter body
  80. """
  81.  
  82. class Book:
  83.     def __init__(self, author, title, price):
  84.         self.author = author
  85.         self.title = title
  86.         self.price = price
  87.         self.type = "Book"
  88.  
  89.     @property
  90.     def title(self):
  91.         return self._title
  92.  
  93.     @property
  94.     def author(self):
  95.         return self._author
  96.  
  97.     @property
  98.     def price(self):
  99.         return self._price
  100.  
  101.     @title.setter
  102.     def title(self, title):
  103.         if len(str(title)) < 3:
  104.             raise Exception("Title not valid!")
  105.         else:
  106.             self._title = title
  107.  
  108.     @author.setter
  109.     def author(self, author):
  110.         data = author.split()
  111.         if len(data) > 1:
  112.             first_letter = data[1][0]
  113.             if first_letter.isdigit():
  114.                 raise Exception("Author not valid!")
  115.             else:
  116.                 self._author = author
  117.         else:
  118.             self._author = author
  119.  
  120.     @price.setter
  121.     def price(self, price):
  122.         if price <= 0:
  123.             raise Exception("Price not valid!")
  124.         else:
  125.             self._price = price
  126.  
  127.     def __str__(self):
  128.         return f"Type: {self.type}" + "\n" + f"Title: {self.title}" + "\n" + f"Author: {self.author}" + "\n" +\
  129.                f"Price: {self.price:.2f}"
  130.  
  131.  
  132. class GoldenEditionBook(Book):
  133.     def __init__(self, author, title, price):
  134.         Book.__init__(self, author, title, price)
  135.         self.price = self.price + (self.price * 0.3)
  136.         self.type = "GoldenEditionBook"
  137.  
  138.  
  139. name_author = input()
  140. name_book = input()
  141. book_price = float(input())
  142.  
  143. try:
  144.     b1 = Book(name_author, name_book, book_price)
  145.     print(b1)
  146.     print()
  147.     b2 = GoldenEditionBook(name_author, name_book, book_price)
  148.     print(b2)
  149. except Exception as e:
  150.     print(e)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement