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

  • Method overriding

    Method overriding is a key feature of object-oriented programming (OOP) and inheritance. It allows a subclass (child class) to provide its own specific implementation of a method that is already defined in its superclass (parent class). When a method is called on an object of the child class, the child’s version of the method is…

  • Python Modules: Creation and Usage Guide

    Python Modules: Creation and Usage Guide What are Modules in Python? Modules are simply Python files (with a .py extension) that contain Python code, including: They help you organize your code into logical units and promote code reusability. Creating a Module 1. Basic Module Creation Create a file named mymodule.py: python # mymodule.py def greet(name): return f”Hello, {name}!”…

  • Curly Braces {} ,Pipe (|) Metacharacters

    Curly Braces {} in Python Regex Curly braces {} are used to specify exact quantity of the preceding character or group. They define how many times something should appear. Basic Syntax: Example 1: Exact Number of Digits python import re text = “Zip codes: 12345, 9876, 123, 123456, 90210″ # Match exactly 5 digits pattern = r”\d{5}” # Exactly…

  • Class Variables Andmethds

    Class Variables Class variables are variables that are shared by all instances of a class. They are defined directly within the class but outside of any method. Unlike instance variables, which are unique to each object, a single copy of a class variable is shared among all objects of that class. They are useful for…

  • Function Returns Multiple Values in Python

    Function Returns Multiple Values in Python In Python, functions can return multiple values by separating them with commas. These values are returned as a tuple, but they can be unpacked into individual variables. Basic Syntax python def function_name(): return value1, value2, value3 # Calling and unpacking var1, var2, var3 = function_name() Simple Examples Example 1:…

  • Indexing and Slicing in Python Lists Read

    Indexing and Slicing in Python Lists Read Indexing and slicing are fundamental operations to access and extract elements from a list in Python. 1. Indexing (Accessing Single Elements) Example 1: Basic Indexing python fruits = [“apple”, “banana”, “cherry”, “date”, “fig”] # Positive indexing print(fruits[0]) # “apple” (1st element) print(fruits[2]) # “cherry” (3rd element) # Negative indexing print(fruits[-1]) # “fig”…

Leave a Reply

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