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.

98 lines
3.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. @staticmethod
  35. def prompt_init():
  36. parent_init = Property.prompt_init()
  37. laundry = get_valid_input("What laundry facilities does the property"
  38. "have?", Apartment.valid_laundries)
  39. balcony = get_valid_input("Does the property have a balcony?",
  40. Apartment.valid_balconies)
  41. parent_init.update({
  42. "laundry" : laundry,
  43. "balcony" : balcony
  44. })
  45. return parent_init
  46. class House(Property):
  47. # class variables
  48. valid_garage = ("attached", "detached", "none")
  49. valid_fenced = ("yes", "no")
  50. def __init__(self, num_stories = "", garage = "", fenced = "", **kwargs):
  51. super().__init__(**kwargs)
  52. self.garage = garage
  53. self.fenced = fenced
  54. self.num_stories = num_stories
  55. def display(self):
  56. super().display()
  57. print("HOSE DETAILS")
  58. print ("# of stories: {}".format(self.num_stories))
  59. print ("garage: {}".format(self.garage))
  60. print ("fenced yard: {}".format(self.fenced))
  61. @staticmethod
  62. def prompt_init():
  63. parent_init = Property.prompt_init()
  64. fenced = get_valid_input("Is the yard fenced?", House.valid_fenced)
  65. garage = get_valid_input("Is there a garage?", House.valid_garage)
  66. num_stories = input("How many stories? ")
  67. parent_init.update({
  68. "fenced" : fenced,
  69. "garage" : garage,
  70. "num_stories" : num_stories
  71. })
  72. return parent_init
  73. def get_valid_input(input_string, valid_options):
  74. input_string += " ({}) ".format(", ".join(valid_options))
  75. response = ""
  76. while response.lower() not in valid_options:
  77. response = input(input_string)
  78. return response