Customer mobile

Create a Customer class that uses accessor (getter) and mutator (setter) methods to control access to its properties.


1. Class Properties

  • name
    • Should be immutable (cannot be changed after the object is created).
  • phone_number
    • Should be mutable (can be changed after the object is created).

2. Class Methods

  • __init__(self, name, phone_number)
    • The constructor that takes name and phone_number as parameters.
    • Initializes the object’s properties with these values.
  • Accessor Methods (Getters)
    • get_name(self)
      • Returns the customer’s name.
    • get_phone_number(self)
      • Returns the customer’s phone number.
  • Mutator Method (Setter)
    • set_phone_number(self, new_phone_number)
      • Takes a new phone number as a parameter.
      • Updates the customer’s phone number.
      • (Only this property can be changed; the name cannot be modified).

3. Task Instructions

  1. Write the Customer class with the constructor and the three methods described above.
  2. Create an object (instance) of the Customer class.
  3. Test the class by:
    • Using the getter methods (get_nameget_phone_number) to retrieve the values.
    • Using the setter method (set_phone_number) to change the phone number.
    • Verifying that the phone number has been successfully updated.
class Customer:

    def __init__(self, name, phoneno):
        self.name = name
        self.phneno = phoneno

    def get_name(self):
        return self.name

    def get_phoneno(self):
        return self.phneno

    def set_phoneno(self, ph):
        self.phneno = ph


c1 = Customer('John', 7650984321)

print('Name:', c1.get_name())
print('Phone No:', c1.get_phoneno())

c1.set_phoneno(6789012345)

print('')
print('Name:', c1.get_name())
print('Phone No:', c1.get_phoneno())

Similar Posts

  • Finally Block in Exception Handling in Python

    Finally Block in Exception Handling in Python The finally block in Python exception handling executes regardless of whether an exception occurred or not. It’s always executed, making it perfect for cleanup operations like closing files, database connections, or releasing resources. Basic Syntax: python try: # Code that might raise an exception except SomeException: # Handle the exception else:…

  • Sets in Python

    Sets in Python A set in Python is an unordered collection of unique elements. Sets are mutable, meaning you can add or remove items, but the elements themselves must be immutable (like numbers, strings, or tuples). Key Characteristics of Sets: Different Ways to Create Sets in Python Here are various methods to create sets in…

  • file properties and methods

    1. file.closed – Is the file door shut? Think of a file like a door. file.closed tells you if the door is open or closed. python # Open the file (open the door) f = open(“test.txt”, “w”) f.write(“Hello!”) print(f.closed) # Output: False (door is open) # Close the file (close the door) f.close() print(f.closed) # Output: True (door is…

  • Decorators in Python

    Decorators in Python A decorator is a function that modifies the behavior of another function without permanently modifying it. Decorators are a powerful tool that use closure functions. Basic Concept A decorator: Simple Example python def simple_decorator(func): def wrapper(): print(“Something is happening before the function is called.”) func() print(“Something is happening after the function is…

  • Challenge Summary: Inheritance – Polygon and Triangle Classes

    Challenge Summary: Inheritance – Polygon and Triangle Classes Objective: Create two classes where Triangle inherits from Polygon and calculates area using Heron’s formula. 1. Polygon Class (Base Class) Properties: Methods: __init__(self, num_sides, *sides) python class Polygon: def __init__(self, num_sides, *sides): self.number_of_sides = num_sides self.sides = list(sides) 2. Triangle Class (Derived Class) Inheritance: Methods: __init__(self, *sides) area(self) python import math…

Leave a Reply

Your email address will not be published. Required fields are marked *