Learn practical skills, build real-world projects, and advance your career
class xyz:
    y=2
    
    def __init__(self):
        print("Constructor called")    
    
    def show(self):
        print("Hello")
        
aditi = xyz() # Object created i.e. constructor is called
print(aditi.y)
aditi.show()

kavya = xyz() # Object created i.e. constructor is called
print(kavya.y)
kavya.y = kavya.y*8
print(kavya.y)
kavya.show()
print(aditi.y)
Constructor called 2 Hello Constructor called 2 16 Hello 2

Problem Statement

Create a Python module as ‘Customer.py’. Create a class called ‘Customer’. We will model this using a dictionary as ‘customer’. The key to this dictionary is customer_id. The value is again a dictionary which has two elements, Name and a list named ‘Subscribed list’.

Tasks

-- Implement a method add_customer which takes name, id, subscribedList as parameters.

-- Implement a method subscribed_list which takes customer_id as input. And returns the subscribed list of that customer

--Implement delete_customer which takes customer_id as input and deletes the customer

-- Implement seller_subscribe customer_id, seller_id as parameters and allows the customer to subscribe to that seller.

class Customer:
    customer = None
    
    def __init__(self):
        self.customer = {}
        print("Constructor called: ",self.customer)
        
    def add_customer(self,name, id, subscribedList=[]):
        self.customer[id] = {'name':name, 'subscribedList':subscribedList}
        print(self.customer) # Dictionary
        print("self.customer[id]: ",self.customer[id])
        print("Added customer: "+str(name))
        
    def subscribed_list(self,customer_id):
        if customer_id in self.customer.keys():
            print("Key Found.")
            cust = self.customer[customer_id]
            return cust['subscribedList']
        else:
            print("Key not found.")
            
    def delete_customer(self, customer_id):
        del self.customer[customer_id]
        print("Deleted Customer: "+str(customer_id))
        
    def seller_subscribe(self, customer_id, seller_id):
        if self.customer.keys().__contains__(customer_id):
            print("\n\n\n",self.customer)
            customer_values = self.customer.get(customer_id)
            subscribed_list = customer_values.get("subscribedList")
            subscribed_list.append(seller_id)
        print(str(customer_id)+" subscribed to "+str(subscribed_list))
        
            
            
c = Customer()
c.add_customer("Darshan",100,['Hotstar','Prime'])
print()
c.add_customer("Divyanka",101,['Zee5','Prime'])
print()
print(c.subscribed_list(100))
c.delete_customer(101)
print()
c.seller_subscribe(100,1234)
Constructor called: {} {100: {'name': 'Darshan', 'subscribedList': ['Hotstar', 'Prime']}} self.customer[id]: {'name': 'Darshan', 'subscribedList': ['Hotstar', 'Prime']} Added customer: Darshan {100: {'name': 'Darshan', 'subscribedList': ['Hotstar', 'Prime']}, 101: {'name': 'Divyanka', 'subscribedList': ['Zee5', 'Prime']}} self.customer[id]: {'name': 'Divyanka', 'subscribedList': ['Zee5', 'Prime']} Added customer: Divyanka Key Found. ['Hotstar', 'Prime'] Deleted Customer: 101 {100: {'name': 'Darshan', 'subscribedList': ['Hotstar', 'Prime']}} 100 subscribed to ['Hotstar', 'Prime', 1234]
d = {100: {'name': 'Darshan', 'subscribedList': ['Hotstar', 'Prime']},
    101: {'name': 'Divyanka', 'subscribedList': ['HotstarD', 'PrimeD']}}
d
{100: {'name': 'Darshan', 'subscribedList': ['Hotstar', 'Prime']},
 101: {'name': 'Divyanka', 'subscribedList': ['HotstarD', 'PrimeD']}}
d.get(100)
{'name': 'Darshan', 'subscribedList': ['Hotstar', 'Prime']}