Link to home
Start Free TrialLog in
Avatar of Kevin Smith
Kevin Smith

asked on

creating class and class constructor exercise in python

Stumped on this exercise."Create a class named Apartment that holds an apartment number, number of bedrooms, number of baths, and rent amount.  Create a constructor that accepts values for each of the data fields.  Also, create a get method for each field.  Write an application that creates at least five Apartment objects (The data to create the Apartment objects will come from a file).  Then prompt the user to enter a minimum number of bedrooms required, a minimum number of baths required, and a maximum rent the user is willing to pay.  Display data for all the Apartment objects that meet the user’s criteria or an appropriate message if no such apartments are available." Here is my bare bones attempt. Any tips or examples of a similar scenario ?
    import sys
    sys.path.append("/Users/ksmith/Desktop/Apartments")
    #random text file with apartment information in it separated by a comma
    
    import Apartments
    
    apartmentData = open('apartments.txt')
    
    apartmentLines = apartmentData.readlines()
    
    
    for line in apartmentLines:
        lineData = line.split(',')
        apt=lineData[0]
        rent=lineData[1]
        rooms=lineData[2]
        bath=lineData[3]
        
    
    bedrooms=input('Enter minimum number of bedrooms: ')
    baths=input('Enter minimum number of baths: ')
    rent=input('Enter maximum amount of rent: ')
    
    
    rent=Apartments.Apartments(rent)
    
    apartmentData.close()
    
    #below different py file with class
    
    class Apartments:
        apartNum=
        bedNum=
        bathNum=
        rent=
        
        def setRemt(self,x):
            self.x = x
        def getMovement(self):
            return self.x
      

Open in new window

Avatar of Louis LIETAER
Louis LIETAER
Flag of France image

hi, the following code is part of your solution, I leave you manage the prompt, and the access to file.

class Apartments:
    def __init__(self, number, bedrooms, baths, rent_amount):
        self.number = number
        self.bedrooms = bedrooms
        self.baths = baths
        self.rent_amount = rent_amount

    @property
    def number(self):
        return self.__number

    @number.setter
    def number(self, val):
        self.__number = val

    @property
    def bedrooms(self):
        return self.__bedrooms

    @bedrooms.setter
    def bedrooms(self, val):
        self.__bedrooms = val

    @property
    def baths(self):
        return self.__baths

    @baths.setter
    def baths(self, val):
        self.__baths = val

    @property
    def rent_amount(self):
        return self.__rent_amount

    @rent_amount.setter
    def rent_amount(self, val):
        self.__rent_amount = val


def selection(Apps, minbed, minbath, maxrent):
    for sample in Apps:
        if sample.bedrooms >= minbed and sample.baths >= minbath and sample.rent_amount < maxrent:
            print(sample.number,sample.bedrooms,sample.baths,sample.rent_amount)


list_of_app = [Apartments(101, 5, 2, 3000), Apartments(102, 4, 1, 2000), Apartments(103, 6, 3, 3500)]

selection(list_of_app, 6, 1, 5000)
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.