Assignment
Assignment
# 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()
Property Driver
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
# 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()