31 lines
1.2 KiB
Python
31 lines
1.2 KiB
Python
from .serializable import Serializable
|
|
from .item import Item
|
|
|
|
class Inventory(Serializable):
|
|
def __init__(self, max_capacity:float = 50.0):
|
|
super().__init__()
|
|
self.items:list[Item] = []
|
|
self.max_capacity = max_capacity # Weight (kg)
|
|
|
|
def current_capacity(self):
|
|
return sum([item.weight for item in 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 added_item.weight + self.current_capacity() > self.max_capacity:
|
|
return f'{added_item.name} is too heavy to fit inside the inventory: {added_item.weight + self.current_capacity() - self.max_capacity}kg in surplus!'
|
|
else:
|
|
self.items.append(added_item)
|
|
return f'{added_item.name} added to inventory. Current items load: {self.current_capacity()}kg'
|
|
|
|
def remove_item(self, r_item):
|
|
if r_item in self.items:
|
|
self.items.remove(r_item)
|
|
return f'{r_item.name} was removed from the inventory'
|
|
else:
|
|
return f'{r_item.name} is not present within the inventory' |