Pyhton - ONE TO ONE RELATIONSHIP

 

P11 / ONE TO ONE RELATIONSHIP – 2

P11 / One to One Relationship – 2

 

Create a class named Address with the following private_attributes

__street

__city

__state

 

Include a constructor

__init__(self, street, city, state)

 

Include a method

__str__(self)

This method returns a string corresponding to Address details in the format specified

in the sample output.

 

Include @property decorator for state

 

Create a class named Person with the following private_attributes

__name

__age

__address (of type Address)

 

Include a constructor

__init__(self, name, age, address)

 

Include a method

__str__(self)

This method returns a string corresponding to Person details in the format specified

in the sample output.

 

Include @property decorator for name

 

Include @property decorator for address

 

Create objects of the above classes and test them.

 

Input and Output Format:

Refer Sample Input and Output for formatting specifications.

 

Sample Input and Output 1:

[All text in bold corresponds to input and the rest corresponds to output]

 

Enter name

Mahirl

Enter age

10

Enter address

Enter street

LMC Street

Enter city

Salem

Enter state

Tamilnadu

Do you want to add another person? Type yes/no

yes

Enter name

Priya

Enter age

20

Enter address

Enter street

Gandhi Street

Enter city

Coimbatore

Enter state

Tamilnadu

Do you want to add another person? Type yes/no

yes

Enter name

Sita

Enter age

30

Enter address

Enter street

Amphi Street

Enter city

Bangalore

Enter state

Karnataka

Do you want to add another person? Type yes/no

no

Search By State

Enter state

Tamilnadu

List of persons from Tamilnadu

['Mahirl', 'Priya']

 

Sample Input and Output 2:

Enter name

Rathish

Enter age

25

Enter address

Enter street

Jagan street

Enter city

Coimbatore

Enter state

Tamilnadu

Do you want to add another person? Type yes/no

no

Search By State

Enter state

Kerala

List of persons from Kerala

[]





Solution:

person.py
from Address import Address
class Person:
    def __init__(self,name, age, address):
        self.__name = name
        self.__age = age
        self.__address = address
    
    def __str__(self):
        return f'{self.__name}'
    
    @property
    def address(self):
        return self.__address

    @property
    def name(self):
        return self.__name

address.py
class Address:
    def __init__(self,street, city, state):
        self.__street = street
        self.__city = city
        self.__state = state
        
    def __str__(self):
        return f''
        
    @property
    def state(self):
        return self.__state

Main.py
from Person import Person
from Address import Address

person_list = []
choice = "yes"
while choice == "yes" :
    person_name = input("Enter name\n")
    person_age = input("Enter age\n")
    print("Enter address")
    street = input("Enter street\n")
    city = input("Enter city\n")
    state = input("Enter state\n")
    person_address = Address(street,city,state)
    person_list.append(Person(person_name, person_age,person_address))
    choice = input("Do you want to add another person? Type yes/no\n")

print("Search By State")
state = input("Enter state\n")
person_state_list = []
for person in person_list :
    if person.address.state == state :
        person_state_list.append(person.name)
print("List of persons from ",state)
print(person_state_list)
    

Comments

Popular posts from this blog

2-D Arrays

String

Conditional Statements