Browse Source

Further integration of classes

Following classes were added: Purchase, Rental, HouseRental, HousePurchase,
ApartmentRental, ApartmentPurchase, Agent
Furthermore printing an empty newline was added at the end of display() methods
in each class where the method exists
master
T. Meissner 10 years ago
parent
commit
db8f802191
1 changed files with 113 additions and 2 deletions
  1. +113
    -2
      python_3_oop/chapter03/real_estate.py

+ 113
- 2
python_3_oop/chapter03/real_estate.py View File

@ -41,11 +41,12 @@ class Apartment(Property):
print("APARTMENT DETAILS") print("APARTMENT DETAILS")
print("laundry: %s" % self.laundry) print("laundry: %s" % self.laundry)
print("has balcony: %s" % self.balcony) print("has balcony: %s" % self.balcony)
print()
@staticmethod @staticmethod
def prompt_init(): def prompt_init():
parent_init = Property.prompt_init() parent_init = Property.prompt_init()
laundry = get_valid_input("What laundry facilities does the property"
laundry = get_valid_input("What laundry facilities does the property "
"have?", Apartment.valid_laundries) "have?", Apartment.valid_laundries)
balcony = get_valid_input("Does the property have a balcony?", balcony = get_valid_input("Does the property have a balcony?",
Apartment.valid_balconies) Apartment.valid_balconies)
@ -70,10 +71,11 @@ class House(Property):
def display(self): def display(self):
super().display() super().display()
print("HOSE DETAILS")
print("HOUSE DETAILS")
print ("# of stories: {}".format(self.num_stories)) print ("# of stories: {}".format(self.num_stories))
print ("garage: {}".format(self.garage)) print ("garage: {}".format(self.garage))
print ("fenced yard: {}".format(self.fenced)) print ("fenced yard: {}".format(self.fenced))
print()
@staticmethod @staticmethod
def prompt_init(): def prompt_init():
@ -89,6 +91,115 @@ class House(Property):
return parent_init return parent_init
class Purchase:
def __init__(self, price = "", taxes = "", **kwargs):
super().__init__(**kwargs)
self.price = price
self.taxes = taxes
def display(self):
super().display()
print("PURCHASE DETAILS")
print("selling price: {}".format(self.price))
print("estimated taxes: {}".format(self.taxes))
print()
@staticmethod
def prompt_init():
return dict(
price = input("What's the selling price? "),
taxes = input("What are the estimated taxes? ")
)
class Rental:
def __init__(self, furnished = "", utilities = "", rent = "", **kwargs):
super().__init__(**kwargs)
self.furnished = furnished
self.utilities = utilities
self.rent = rent
def display(self):
super().display()
print("RENTAL DETAILS")
print("rent: {}".format(self.rent))
print("estimated utilities: {}".format(self.utilities))
print("furnished: {}".format(self.furnished))
print()
@staticmethod
def prompt_init():
return dict(
rent = input("What is the monthly rent? "),
utilities = input("What are the estimated utilities? "),
furnished = get_valid_input("Is the property furnished?",
("yes", "no"))
)
class HouseRental(Rental, House):
@staticmethod
def prompt_init():
init = House.prompt_init()
init.update(Rental.prompt_init())
return init
class ApartmentRental(Rental, Apartment):
@staticmethod
def prompt_init():
init = Apartment.prompt_init()
init.update(Rental.prompt_init())
return init
class HousePurchase(Purchase, House):
@staticmethod
def prompt_init():
init = House.prompt_init()
init.update(Purchase.prompt_init())
return init
class ApartmentPurchase(Purchase, Apartment):
@staticmethod
def prompt_init():
init = Apartment.prompt_init()
init.update(Purchase.prompt_init())
return init
class Agent:
type_map = {
("house", "rental") : HouseRental,
("house", "purchase") : HousePurchase,
("apartment", "rental") : ApartmentRental,
("apartment", "purchase") : ApartmentPurchase
}
def __init__(self):
self.property_list = []
def display_properties(self):
for property in self.property_list:
property.display()
def add_property(self):
property_type = get_valid_input("What type of property?",
("house", "apartment")).lower()
payment_type = get_valid_input("What payment type?",
("purchase", "rental")).lower()
PropertyClass = self.type_map[(property_type, payment_type)]
init_args = PropertyClass.prompt_init()
self.property_list.append(PropertyClass(**init_args))
def get_valid_input(input_string, valid_options): def get_valid_input(input_string, valid_options):
input_string += " ({}) ".format(", ".join(valid_options)) input_string += " ({}) ".format(", ".join(valid_options))


Loading…
Cancel
Save