Write a python program to manage a phone store (mobile shop) record using class


The Python code defines two classes: Phone and PhoneStore, allowing you to manage a phone inventory for a store. Here's a breakdown of the code:

  • The Phone class represents individual phones. Each phone has attributes for its brand, model, and price. The __init__ method initializes these attributes when a new phone object is created.
  • The PhoneStore class represents a store's inventory of phones. It has the following methods:
    • __init__: Initializes an empty list called inventory to store phone objects.
    • add_phone: Adds a phone object to the inventory list.
    • remove_phone: Removes a phone from the inventory based on its brand and model. If the phone is found and removed, a success message is printed; otherwise, a "not found" message is printed.
    • find_phone: Searches for a phone in the inventory based on its brand and model. If found, it returns the phone object; otherwise, it returns None.
    • display_inventory: Displays the current phone inventory, including brand, model, and price.
  • An instance of the PhoneStore class is created with the variable phone_store.
  • A menu is displayed to the user with the following options:
    • 1. Add Phone to Inventory
    • 2. Remove Phone from Inventory
    • 3. Find Phone in Inventory
    • 4. Display Inventory
    • 5. Quit
  • The program enters a loop where the user is prompted to enter their choice (as a string).
  • Depending on the user's choice, the corresponding method of the PhoneStore instance is called to perform the desired operation. The user can add, remove, find, or display phone inventory, or quit the program.
  • If the user provides an invalid choice, they are notified with the "Invalid choice" message and prompted to try again.

The code converts brand and model inputs to uppercase using the .capitalize() method before storing them in phone objects and when searching the inventory. This ensures that case-insensitive matching is used when adding, removing, or finding phones.

The code allows you to interactively manage the phone inventory for a store and will continue running until the user chooses to quit (option 5).


Source Code

class Phone:
    def __init__(self, brand, model, price):
        self.brand = brand
        self.model = model
        self.price = price
 
class PhoneStore:
    def __init__(self):
        self.inventory = []
 
    def add_phone(self, phone):
        self.inventory.append(phone)
 
    def remove_phone(self, brand, model):
        for phone in self.inventory:
            if phone.brand == brand and phone.model == model:
                self.inventory.remove(phone)
                print(f"Removed {brand} {model} from inventory.")
                return
        print(f"{brand} {model} not found in inventory.")
 
    def find_phone(self, brand, model):
        for phone in self.inventory:
            if phone.brand == brand and phone.model == model:
                return phone
        return None
 
    def display_inventory(self):
        print("Phone Store Inventory :")
        for phone in self.inventory:
            print(f"Brand : {phone.brand}, Model : {phone.model}, Price : ${phone.price:.2f}")
 
 
phone_store = PhoneStore()
 
print("1. Add Phone to Inventory")
print("2. Remove Phone from Inventory")
print("3. Find Phone in Inventory")
print("4. Display Inventory")
print("5. Quit")
 
while True:
    choice = input("\nEnter your Choice : ")
 
    if choice == "1":
        brand = input("Enter Phone Brand : ")
        model = input("Enter Phone Model : ")
        price = float(input("Enter Phone Price : "))
        phone = Phone(brand.capitalize(), model.capitalize(), price)
        phone_store.add_phone(phone)
        print(f"Added {brand} {model} to inventory.")
    elif choice == "2":
        brand = input("Enter Phone Brand to Remove : ")
        model = input("Enter Phone Model to Remove : ")
        phone_store.remove_phone(brand.capitalize(), model.capitalize())
    elif choice == "3":
        brand = input("Enter Phone Brand to Find : ")
        model = input("Enter Phone Model to Find : ")
        found_phone = phone_store.find_phone(brand.capitalize(), model.capitalize())
        if found_phone:
            print(f"Found {brand} {model} in inventory. Price : ${found_phone.price:.2f}")
        else:
            print(f"{brand} {model} not found in inventory.")
    elif choice == "4":
        phone_store.display_inventory()
    elif choice == "5":
        break
    else:
        print("Invalid choice. Please try again.")

Output

1. Add Phone to Inventory
2. Remove Phone from Inventory
3. Find Phone in Inventory
4. Display Inventory
5. Quit

Enter your Choice : 1
Enter Phone Brand : Realme
Enter Phone Model : c16
Enter Phone Price : 12000
Added Realme c16 to inventory.

Enter your Choice : 1
Enter Phone Brand : Redmi
Enter Phone Model : redmi20
Enter Phone Price : 10000
Added Redmi redmi20 to inventory.

Enter your Choice : 4
Phone Store Inventory:
Brand: Realme, Model: C16, Price: $12000.00
Brand: Redmi, Model: Redmi20, Price: $10000.00

Enter your Choice : 3
Enter Phone Brand to Find : realme
Enter Phone Model to Find : c16
Found realme c16 in inventory. Price : $12000.00

Enter your Choice : 2
Enter Phone Brand to Remove : redmi
Enter Phone Model to Remove : redmi20
Removed Redmi Redmi20 from inventory.

Enter your Choice : 4
Phone Store Inventory:
Brand: Realme, Model: C16, Price: $12000.00

Enter your Choice : 5

Example Programs