Add remove_item_by_name to item_list_manager.py

This commit is contained in:
Roland Thomas Jr 2024-06-09 21:55:42 -04:00
parent 679ce2eddf
commit 060e94326a
Signed by: roland
GPG Key ID: 7C3C2B085A4C2872
1 changed files with 12 additions and 2 deletions

View File

@ -45,12 +45,22 @@ class ItemManager:
else: else:
print(f"Item with ID {item_id} not found.") print(f"Item with ID {item_id} not found.")
def search_by_name(self, name: str) -> Optional[Item]: def remove_item_by_name(self, name: str):
return self.items_by_name.get(name) if name in self.items_by_name:
item_to_remove = self.items_by_name.pop(name)
self.ordered_items = [item for item in self.ordered_items if item.name != name]
if item_to_remove.id in self.items_by_id:
del self.items_by_id[item_to_remove.id]
print(f"Item removed: {item_to_remove}")
else:
print(f"Item with name {name} not found.")
def search_by_id(self, item_id: str) -> Optional[Item]: def search_by_id(self, item_id: str) -> Optional[Item]:
return self.items_by_id.get(item_id) return self.items_by_id.get(item_id)
def search_by_name(self, name: str) -> Optional[Item]:
return self.items_by_name.get(name)
def get_all_items(self) -> List[Item]: def get_all_items(self) -> List[Item]:
return self.ordered_items return self.ordered_items