reka-core-20240722· unknown
from LMArena Human Preference 100K
Refactor duplicate code: # Handles product-related operations such as adding, removing,…
- User
Refactor duplicate code: # Handles product-related operations such as adding, removing, updating, sorting, and searching products. import json
class ProductManager: """A class used to manage products."""
codedef __init__(self, file_name: str = "products.json"): self.file_name = file_name self.products: list = [] self._load_json_file(file_name) def _save_to_json_file(self, file_name: str): with open(file_name, "w") as file: json.dump(self.products, file) def _load_json_file(self, file_name): try: with open(file_name, "r") as file: self.products = json.load(file) except (FileNotFoundError, json.JSONDecodeError): self.products = [] def add_product(self): """Add a new product to the list.""" new_product = { "product_id": self.get_next_product_id(), "name": input("Enter the product name: "), "price": float(input("Enter the product price: ")), "description": input("Enter the product description: "), "quantity": int(input("Enter the product quantity: ")), } self.products.append(new_product) self._save_to_json_file(self.file_name) def get_next_product_id(self): if not self.products: return 1 else: return max(product["product_id"] for product in self.products) + 1 def remove_product(self, product_id): self.products = [product for product in self.products if product["product_id"] != product_id] self._save_to_json_file(self.file_name) def update_product(self, product_id): updated_product = {} for product in self.products: if product["product_id"] == product_id: updated_product["name"] = input("Enter the new name: ") updated_product["price"] = str(float(input("Enter the new price: "))) updated_product["description"] = input("Enter the new description: ") updated_product["quantity"] = str(int(input("Enter the new quantity: "))) self.products = [product if product["product_id"] != product_id else updated_product] self._save_to_json_file(self.file_name) def sort_products(self): order = input("Do you want to sort by price (p) or name (n)? ") if order.lower() == "p": self.products.sort(key=lambda x: x["price"]) elif order.lower() == "n": self.products.sort(key=lambda x: x["name"]) def search_product(self): query = input("Enter the product ID or name to search: ") result = [ product for product in self.products if product["product_id"] == int(query) or product["name"].lower() == query.lower() ] print(result) - Assistant