37 lines
1.3 KiB
Python
37 lines
1.3 KiB
Python
from serializable import Serializable
|
|
from items.item import Item
|
|
|
|
class Inventory(Serializable):
|
|
def __init__(self, max_capacity:int = 5):
|
|
super().__init__()
|
|
self.items:list[Item] = []
|
|
self.max_capacity = max_capacity # Maximum umber of items
|
|
|
|
def current_capacity(self):
|
|
return len(self.items)
|
|
|
|
def list_items(self):
|
|
s_items = ''
|
|
for item in self.items:
|
|
s_items += item.__str__() + '; '
|
|
return s_items
|
|
|
|
def add_item(self, added_item:Item):
|
|
if self.current_capacity() == self.max_capacity:
|
|
return f'The inventory is full!'
|
|
else:
|
|
self.items.append(added_item)
|
|
return f'{added_item.name} added to inventory. Current number of items: {self.current_capacity()}/{self.max_capacity}'
|
|
|
|
def remove_item(self, item_id:str):
|
|
searched_item = self.get_item(item_id=item_id)
|
|
if searched_item:
|
|
return f'{searched_item.name} was removed from the inventory.'
|
|
raise ValueError(f'Item #{item_id} is not present within the inventory!')
|
|
|
|
def get_item(self, item_id:str):
|
|
for item in self.items:
|
|
if item.id == item_id:
|
|
return item
|
|
# The item was not found
|
|
return None |