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 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).
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.")
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
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions