30 lines
775 B
Python
30 lines
775 B
Python
from persistencia.excecoes import EntidadeNaoEncontradaException
|
|
|
|
class Persistente:
|
|
def __init__(self):
|
|
self.lista = []
|
|
|
|
def inserir(self, obj):
|
|
self.lista.append(obj)
|
|
|
|
def alterar(self, id_, novo_obj):
|
|
antigo = self.buscar_por_id(id_)
|
|
self.lista.remove(antigo)
|
|
self.lista.append(novo_obj)
|
|
|
|
def remover(self, id_):
|
|
obj = self.buscar_por_id(id_)
|
|
self.lista.remove(obj)
|
|
|
|
def buscar_por_id(self, id_):
|
|
for obj in self.lista:
|
|
if obj.id == id_:
|
|
return obj
|
|
raise EntidadeNaoEncontradaException(f"ID não encontrado: {id_}")
|
|
|
|
def listar_todos(self):
|
|
return self.lista
|
|
|
|
def __str__(self):
|
|
return "\n".join(str(obj) for obj in self.lista)
|