Learning by doing: Reading books and trying to understand the (code) examples
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

209 lines
6.0 KiB

  1. # python 3 oop - chapter 3 case study
  2. # simple real estate application
  3. class Property:
  4. def __init__(self, square_feet='', beds='', baths='', **kwargs):
  5. super().__init__(**kwargs)
  6. self.square_feet = square_feet
  7. self.num_bedrooms = beds
  8. self.num_baths = baths
  9. def display(self):
  10. print("PROPERTY DETAILS")
  11. print("================")
  12. print("square footage: {}".format(self.square_feet))
  13. print("bedrooms: {}".format(self.num_bedrooms))
  14. print("bathrooms: {}".format(self.num_baths))
  15. print()
  16. @staticmethod # decorator form of the staticmethod constructor function
  17. def prompt_init():
  18. return dict(square_feet=input("Enter the square feet: "),
  19. beds=input("Enter number of bedrooms: "),
  20. baths=input("Enter number of baths: "))
  21. class Apartment(Property):
  22. # class variables
  23. valid_laundries = ("coin", "ensuite", "none")
  24. valid_balconies = ("yes", "no", "solarium")
  25. def __init__(self, balcony="", laundry="", **kwargs):
  26. super().__init__(**kwargs)
  27. self.balcony = balcony
  28. self.laundry = laundry
  29. def display(self):
  30. super().display()
  31. print("APARTMENT DETAILS")
  32. print("laundry: %s" % self.laundry)
  33. print("has balcony: %s" % self.balcony)
  34. print()
  35. @staticmethod
  36. def prompt_init():
  37. parent_init = Property.prompt_init()
  38. laundry = get_valid_input("What laundry facilities does the property "
  39. "have?", Apartment.valid_laundries)
  40. balcony = get_valid_input("Does the property have a balcony?",
  41. Apartment.valid_balconies)
  42. parent_init.update({
  43. "laundry": laundry,
  44. "balcony": balcony
  45. })
  46. return parent_init
  47. class House(Property):
  48. # class variables
  49. valid_garage = ("attached", "detached", "none")
  50. valid_fenced = ("yes", "no")
  51. def __init__(self, num_stories="", garage="", fenced="", **kwargs):
  52. super().__init__(**kwargs)
  53. self.garage = garage
  54. self.fenced = fenced
  55. self.num_stories = num_stories
  56. def display(self):
  57. super().display()
  58. print("HOUSE DETAILS")
  59. print("# of stories: {}".format(self.num_stories))
  60. print("garage: {}".format(self.garage))
  61. print("fenced yard: {}".format(self.fenced))
  62. print()
  63. @staticmethod
  64. def prompt_init():
  65. parent_init = Property.prompt_init()
  66. fenced = get_valid_input("Is the yard fenced?", House.valid_fenced)
  67. garage = get_valid_input("Is there a garage?", House.valid_garage)
  68. num_stories = input("How many stories? ")
  69. parent_init.update({
  70. "fenced": fenced,
  71. "garage": garage,
  72. "num_stories": num_stories
  73. })
  74. return parent_init
  75. class Purchase:
  76. def __init__(self, price="", taxes="", **kwargs):
  77. super().__init__(**kwargs)
  78. self.price = price
  79. self.taxes = taxes
  80. def display(self):
  81. super().display()
  82. print("PURCHASE DETAILS")
  83. print("selling price: {}".format(self.price))
  84. print("estimated taxes: {}".format(self.taxes))
  85. print()
  86. @staticmethod
  87. def prompt_init():
  88. return dict(
  89. price=input("What's the selling price? "),
  90. taxes=input("What are the estimated taxes? ")
  91. )
  92. class Rental:
  93. def __init__(self, furnished="", utilities="", rent="", **kwargs):
  94. super().__init__(**kwargs)
  95. self.furnished = furnished
  96. self.utilities = utilities
  97. self.rent = rent
  98. def display(self):
  99. super().display()
  100. print("RENTAL DETAILS")
  101. print("rent: {}".format(self.rent))
  102. print("estimated utilities: {}".format(self.utilities))
  103. print("furnished: {}".format(self.furnished))
  104. print()
  105. @staticmethod
  106. def prompt_init():
  107. return dict(
  108. rent=input("What is the monthly rent? "),
  109. utilities=input("What are the estimated utilities? "),
  110. furnished=get_valid_input("Is the property furnished?",
  111. ("yes", "no"))
  112. )
  113. class HouseRental(Rental, House):
  114. @staticmethod
  115. def prompt_init():
  116. init = House.prompt_init()
  117. init.update(Rental.prompt_init())
  118. return init
  119. class ApartmentRental(Rental, Apartment):
  120. @staticmethod
  121. def prompt_init():
  122. init = Apartment.prompt_init()
  123. init.update(Rental.prompt_init())
  124. return init
  125. class HousePurchase(Purchase, House):
  126. @staticmethod
  127. def prompt_init():
  128. init = House.prompt_init()
  129. init.update(Purchase.prompt_init())
  130. return init
  131. class ApartmentPurchase(Purchase, Apartment):
  132. @staticmethod
  133. def prompt_init():
  134. init = Apartment.prompt_init()
  135. init.update(Purchase.prompt_init())
  136. return init
  137. class Agent:
  138. type_map = {
  139. ("house", "rental"): HouseRental,
  140. ("house", "purchase"): HousePurchase,
  141. ("apartment", "rental"): ApartmentRental,
  142. ("apartment", "purchase"): ApartmentPurchase
  143. }
  144. def __init__(self):
  145. self.property_list = []
  146. def display_properties(self):
  147. for property in self.property_list:
  148. property.display()
  149. def add_property(self):
  150. property_type = get_valid_input("What type of property?",
  151. ("house", "apartment")).lower()
  152. payment_type = get_valid_input("What payment type?",
  153. ("purchase", "rental")).lower()
  154. property_class = self.type_map[(property_type, payment_type)]
  155. init_args = property_class.prompt_init()
  156. self.property_list.append(property_class(**init_args))
  157. def get_valid_input(input_string, valid_options):
  158. input_string += " ({}) ".format(", ".join(valid_options))
  159. response = ""
  160. while response.lower() not in valid_options:
  161. response = input(input_string)
  162. return response