0% found this document useful (0 votes)
3 views12 pages

Assignment

The document defines two classes, Property and PropertyList, for managing real estate properties. The Property class includes methods for initializing property details, calculating annual taxes, and comparing properties, while the PropertyList class manages a collection of properties, allowing for adding, selling, sorting, and saving/loading properties from a file. The document also includes a driver program that provides a user interface for interacting with the property management system.

Uploaded by

Ibrahim Malanja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views12 pages

Assignment

The document defines two classes, Property and PropertyList, for managing real estate properties. The Property class includes methods for initializing property details, calculating annual taxes, and comparing properties, while the PropertyList class manages a collection of properties, allowing for adding, selling, sorting, and saving/loading properties from a file. The document also includes a driver program that provides a user interface for interacting with the property management system.

Uploaded by

Ibrahim Malanja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

class Property:

# declare a magic method ( constructor ) to and takes 4 parameters


def __init__ (self,location='',price=0.0,ptype='', size=0): # assigning value to determine
the data type
self.__location = location
self.__price = price
self.__ptype = ptype
self.__size = size
# if conditions to determine Type string although , I did create a method for doing that as
you can see below
if self.__ptype.lower() == 'a':
self.__ptype = 'apartment'
elif self.__ptype.lower() == 'b':
self.__ptype = 'bungalow'
elif self.__ptype.lower() == 'c':
self.__ptype = 'condominium'
# simple getter method
def getLocation(self):
return self.__location
def getPrice(self):
return self.__price
def getType(self):
return self.__ptype
def getSize(self):
return self.__size
#simple setter methods
def setLocation(self,location):
self.__location = location
def setPrice(self,price):
self.__price = price
def setType(self,type):
self.__ptype = type
def setSize(self,size):
self.__size = size
# method type string to determine the type of property based on first character
def typeString(self):
if self.__ptype.lower() == 'a':
self.__ptype = 'apartment'
return self.__ptype
elif self.__ptype.lower() == 'b':
self.__ptype = 'bungalow'
elif self.__ptype.lower() == 'c':
self.__ptype = 'condominium'
# annual tax method which clalculates the tax based on the type
def annualTax(self):
if self.__ptype == 'apartment':
return self.__price * 0.025
elif self.__ptype == 'bungalow' :
return self.__price * 0.045
elif self.__ptype == 'condominium':
return self.__price * 0.035
# __eq__ magic method wich returns True if two properties are equal and False if are not
equal
def __eq__(self,property20):
if self.__location == property20.__location and self.__size == property20.__size and
self.__ptype == property20.__ptype :
return 'True'
else:
return "False"
# __lt__ is a magic method which returns True if first property is less than second property
def __lt__(self,property3):
if self.__size > property3.__size:
return True
else :
return False
# __le__ is a magic method which return True if first property is less than or equal second
property
def __le__(self,property4):
if property4.__size <= self.__size :
return True
else :
return False
# __str__ is a magic method which returns a single string containing the details of a property
def __str__(self):
string = self.__ptype + ', ' + str(self.__size) + ' square feet , ' + 'At ' + self.__location
+ ', Costing ' + str(self.__price )
return (string)

# The main reason of creating this Class is to provide list to store Property Data
class PropertyList :
# A magic method takes on parameters and initilaze an empty list
def __init__(self,name):
list1 = []
self.__name = name
self.__list1 = list1
# get method for Name parameter
def getName(self):
return self.__name
# getter for the list
def getPropertyList(self):
return self.__list1
# setter for name
def setName(self,name):
self.__name = name
# The main function of this method is to add property to the list
def addProperty(self,property):
self.__list1.append(property)
return self.__list1
# The main function of this method is to count the property inside the list by using len()
def noOfProperties(self):
return len(self.__list1)

def allProperties(self):
porpertyList = self.__list1
# initialize an empty string
AllProperties = ''
# for each loop to store the data inside the empty string
for property in porpertyList:
AllProperties += str(property) + '\n'
return AllProperties
def totalPrice(self):
# initialize an empty integer
total = 0
# for loop for getting the price
for p in self.__list1:
total += p.getPrice()
return "Total price : ${:.2f} ".format(total)
#print( total)

def mostExpensiveProperty(self):
highest_price = 0
highest_Property = 0
for x in self.__list1:
if highest_price < x.getPrice():
highest_price = x.getPrice()
highest_Property = x
return highest_Property
# method takes one parameter represents the type to find the its information
def findPropertyByType(self,ptype):
# these if statements in order to provide the user shortcuts
for e in self.__list1:
if ptype.lower() == 'a':
ptype = 'apartment'
elif ptype.lower() == 'b':
ptype = 'bungalow'
elif ptype.lower() == 'c':
ptype = 'condominium'
# if statement to retrive the information
if ptype == e.getType():
myString = "Price = {}, Size = {}, Location = {} ".format(e.getPrice(),
e.getSize(), e.getLocation())
print(myString)
def sellProperty(self,location):
# try and except method to avoid broking the program due the user fault
try:
# delete the property by using indexing
self.__list1.pop(location)
except (IndexError , ValueError , OverflowError) as error :
print('OUT OF RANGE , \nPLEASE TRY AGAIN ')
# this method retrieves sorted data based on users' criteria
def sortedProperties(self,criteria):
list2 = []
if criteria == 'price' :
# using a lambda to sort the data based on attribute
self.__list1.sort(key=lambda r:r.getPrice() )
list2.extend(self.__list1)
for item in list2 :
print(str(item) + '\n')
elif criteria == 'tax':
self.__list1.sort(key=lambda r: r.annualTax() )
list2.extend(self.__list1)
for item in list2:
print(str(item) + '\n')
elif criteria == 'size':
self.__list1.sort(key=lambda r: r.getSize())
list2.extend(self.__list1)
for item in list2:
print(str(item) + '\n')

def saveToFile(self,FileName):
# Normal file funtion to create file or Write on it
file = open(FileName, 'w' )
# For loop in order to store the data
for pro in self.__list1:
file.write(str(pro) + '\n')
file.close()

# Normal file function to load file


def loadFromFile(self,LfileName):
# Try and except in order to avoid breaking the program due users' mistakes
try:
# Lfile which means Load File
Lfile = open(LfileName , 'r')
for line in Lfile:
self.__list1.append(line)
Lfile.close()
except FileNotFoundError:
print('\nFile is not found , Please try again and ' )
# Virtual instance to use it in propertyDriver , i will delete it from the list in the next file
# therefore it won't effect on the program , by contrary , program will be easier
prop6= Property('',5000,'a',0)
prop2 = PropertyList('name')
prop2.addProperty(prop6)
prop2.sellProperty(0)

Property Driver

# Import everything from propertyApp File


from propertyApp import *

# Delete the instance


# prop2.sellProperty(0)
# save the name of company
company = input("What's the name of your property company?")
def main():
# While True ( While without )
while True:
# assign menu to choice
choice = menu()
interface = prop2

# If statement to determine what does each choice represent

if Userchoice == 1 :
addproperty(interface)
elif Userchoice == 2 :
allProperties(interface)
elif Userchoice == 3 :
getPropertyList(interface)
elif Userchoice == 4 :
findPropertyByType(interface)
elif Userchoice == 5 :
sellProperty(interface)
elif Userchoice == 6:
sortedProperties(interface)
elif Userchoice == 7:
loadFromFile(interface)
elif Userchoice == 8 :
saveToFile(interface)
elif Userchoice == 0 :
print('Sayonara...')
break
# Try and except statement to avoid breaking the program
try:
# if statement to print an Error message if the user goes out of the range
if Userchoice > 8:
print('\nPlease type a number that represents a choice \n')
except TypeError:
print('\nPlease type a number that represents a choice \n')

def menu():
# User interface menue
print (str(company))
print ( '-' * 25 )
print('1 Add a property')
print('2 Display all properties')
print('3 Display summary information about property list')
print('4 Display properties with user-specified type')
print('5 Sell a property based on given index')
print('6 Display all properties, sorted according to Property values')
print('7 Read properties information from file')
print('8 Write properties information to file')
print('0 quit')
try : # to avoid breaking the app
option = int(input('Your choice:'))
return option
except ValueError :
print('\nPlease type a number that represents a choice \n')

# Method to handle with adding property interface which allows the user to add
properties
def addproperty(interface1):
print('Adding a new property')
location = input('Location:')
price = input('Price:')
Property_type = input("Property type ('A'partmen, 'B'ungalow, 'C'ondominium)")
Size = input('Size (in square feet, eg. 1438):')
Psample = Property(location,float(price),Property_type,Size)
interface1.addProperty(Psample )
# prop2 to is and virtual instance

print('... added successfully.')

# method which allows the user to show all Properties in the list
def allProperties(interface1):
print('Details of all properties:')
print(interface1.allProperties())
if interface1.noOfProperties() == 0 :
print('No property has been registered yet')
# method which displays summary information about property such as number of
properties and most expensive property
def getPropertyList(interface1):
print('Summary Information')
print('Number of registered properties: : ' + str(interface1.noOfProperties()))
print( interface1.totalPrice() )
print( 'Most expensive property: ' )
print(interface1.mostExpensiveProperty())
# method provies console interface for the user to display the information based on
specified type
def findPropertyByType(interface1):
user = input("Property type ('A'partmen, 'B'ungalow, 'C'ondominium):")
# If condition to print a message that tells the users No property for their type
if interface1.findPropertyByType(user.lower()) == None :
print ('No property of that specific type yet')
else :
print(interface1.findPropertyByType(user.lower()))
#method provides an interface for the user to choose an property to sell it (console
interface)
def sellProperty(interface1):
try :
user = int(input('Property to sell ? ( 1 ..
{} )'.format(interface1.noOfProperties())))
UserInput = user - 1

interface1.sellProperty(UserInput)

except ValueError :
print('PLEASE ENTER AN INTEGER NUMBER ')

def sortedProperties(interface1):
user = input('- criteria (price, tax, or size)?')
interface1.sortedProperties(user.lower())
def loadFromFile(interface1):
user = input('File name to load from ( without .txt ) ')
interface1.loadFromFile(user)

def saveToFile(interface1):
if interface1.noOfProperties() >= 1:
user = input('File name to save to ( without .txt ) :' )
interface1.saveToFile(user)
else :
print('No property has been registered yet ')
# to execute the program
main()

You might also like