Advertisement
SimeonTs

SUPyF2 Objects/Classes-Exericse - 08. Vehicle

Oct 19th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.53 KB | None | 0 0
  1. """
  2. Objects and Classes - Exericse
  3. Check your code: https://judge.softuni.bg/Contests/Practice/Index/1734#7
  4.  
  5. SUPyF2 Objects/Classes-Exericse - 08. Vehicle (not included in final score)
  6.  
  7. Problem:
  8. Create a class Vehicle. The __init__ method should receive: type, model, price. You should also set the owner to None.
  9. The class should have the following methods:
  10. • buy(money, owner) - if the person has enough money and the vehicle has no owner, returns:
  11. "Successfully bought a {type}. Change {change}" and sets the owner to the given one.
  12. If the money is not enough, return: "Sorry, not enough money".
  13. If the car already has an owner, return: "Car already sold"
  14. • sell() - if the car has an owner, set it to None again. Otherwise, return: "Vehicle has no owner"
  15. • __repr__() - returns "{model} {type} is owned by: {owner}" if the vehicle has an owner.
  16. Otherwise, return: "{model} {type} is on sale: {price}"
  17.  
  18. Example:
  19. Test Code:
  20. vehicle_type = "car"
  21. model = "BMW"
  22. price = 30000
  23. vehicle = Vehicle(vehicle_type, model, price)
  24. vehicle.buy(15000, "Peter")
  25. vehicle.buy(35000, "George")
  26. print(vehicle)
  27. vehicle.sell()
  28. print(vehicle)
  29.  
  30. Output:
  31. Sorry, not enough money
  32. Successfully bought a car. Change: 5000.00
  33. BMW car is owned by: George
  34. BMW car is on sale: 30000
  35. """
  36.  
  37.  
  38. class Vehicle:
  39.     def __init__(self, type: str, model: str, price: float):
  40.         self.type = type
  41.         self.model = model
  42.         self.price = price
  43.         self.owner = None
  44.  
  45.     def buy(self, money, owner):
  46.         if money >= self.price and self.owner is None:
  47.             self.owner = owner
  48.             return f"Successfully bought a {self.type}. Change: {(money - self.price):.2f}"
  49.         elif money < self.price:
  50.             return f"Sorry, not enough money"
  51.         elif self.owner:
  52.             return f"Car already sold"
  53.  
  54.     def sell(self):
  55.         if not self.owner:
  56.             return "Vehicle has no owner"
  57.         self.owner = None
  58.  
  59.     def __repr__(self):
  60.         if self.owner:
  61.             return f"{self.model} {self.type} is owned by: {self.owner}"
  62.         return f"{self.model} {self.type} is on sale: {self.price}"
  63.  
  64.  
  65. vehicle_type = "car"
  66. model = "BMW"
  67. price = 30000
  68. vehicle = Vehicle(vehicle_type, model, price)
  69. vehicle.buy(15000, "Peter")
  70. vehicle.buy(35000, "George")
  71. print(vehicle)
  72. vehicle.sell()
  73. print(vehicle)
  74.  
  75.  
  76. vehicle_type = "car"
  77. model = "BMW"
  78. price = 30000
  79. vehicle = Vehicle(vehicle_type, model, price)
  80. vehicle.buy(15000, "Peter")
  81. vehicle.buy(35000, "George")
  82. print(vehicle)
  83. vehicle.sell()
  84. print(vehicle)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement