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

  • Strings in Python Indexing,Traversal

    Strings in Python and Indexing Strings in Python are sequences of characters enclosed in single quotes (‘ ‘), double quotes (” “), or triple quotes (”’ ”’ or “”” “””). They are immutable sequences of Unicode code points used to represent text. String Characteristics Creating Strings python single_quoted = ‘Hello’ double_quoted = “World” triple_quoted = ”’This is…

  • group() and groups()

    Python re group() and groups() Methods Explained The group() and groups() methods are used with match objects to extract captured groups from regex patterns. They work on the result of re.search(), re.match(), or re.finditer(). group() Method groups() Method Example 1: Basic Group Extraction python import retext = “John Doe, age 30, email: john.doe@email.com”# Pattern with multiple capture groupspattern = r'(\w+)\s+(\w+),\s+age\s+(\d+),\s+email:\s+([\w.]+@[\w.]+)’///The Pattern: r'(\w+)\s+(\w+),\s+age\s+(\d+),\s+email:\s+([\w.]+@[\w.]+)’Breakdown by Capture…

  • Bank Account Class with Minimum Balance

    Challenge Summary: Bank Account Class with Minimum Balance Objective: Create a BankAccount class that automatically assigns account numbers and enforces a minimum balance rule. 1. Custom Exception Class python class MinimumBalanceError(Exception): “””Custom exception for minimum balance violation””” pass 2. BankAccount Class Requirements Properties: Methods: __init__(self, name, initial_balance) deposit(self, amount) withdraw(self, amount) show_details(self) 3. Key Rules: 4. Testing…

  • Why Python is So Popular: Key Reasons Behind Its Global Fame

    Python’s fame and widespread adoption across various sectors can be attributed to its unique combination of simplicity, versatility, and a robust ecosystem. Here are the key reasons why Python is so popular and widely used in different industries: 1. Easy to Learn and Use 2. Versatility Python supports multiple programming paradigms, including: This versatility allows…

  • pop(), remove(), clear(), and del 

    pop(), remove(), clear(), and del with 5 examples each, including slicing where applicable: 1. pop([index]) Removes and returns the item at the given index. If no index is given, it removes the last item. Examples: 2. remove(x) Removes the first occurrence of the specified value x. Raises ValueError if not found. Examples: 3. clear() Removes all elements from the list, making it empty. Examples: 4. del Statement Deletes elements by index or slice (not a method, but a…

  • File Handling in Python

    File Handling in Python File handling is a crucial aspect of programming that allows you to read from and write to files. Python provides built-in functions and methods to work with files efficiently. Basic File Operations 1. Opening a File Use the open() function to open a file. It returns a file object. python # Syntax: open(filename,…

Leave a Reply

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