diff --git a/exercicios/para-casa/projetoGuiado_CrisPereira/Biblioteca.py b/exercicios/para-casa/projetoGuiado_CrisPereira/Biblioteca.py new file mode 100644 index 0000000..852557b --- /dev/null +++ b/exercicios/para-casa/projetoGuiado_CrisPereira/Biblioteca.py @@ -0,0 +1,40 @@ +# Exercício Cris Pereira - semana 08 +from Livro import Livro + +class Biblioteca: + def __init__(self): + self.livros = [] + + def adicionar_livro(self, livro: Livro): + if (not isinstance(livro, Livro)): + raise TypeError(f"Esperado Livro obtido valor {livro} do tipo {type(livro)}") + self.livros.append(livro) + +# Novas funcionalidades abaixo + def exibir_livros(self): + return self.livros + + def emprestar_livro(self, titulo): + for livro in self.livros: + if livro.nome == titulo and not livro.esta_emprestado: + livro.esta_emprestado = True + + def remover_livro(self, titulo): + for livro in self.livros: + if livro.nome == titulo: + self.livros.remove(livro) + + def buscar_livro(self, titulo): + mensagem = "Livro não encontrado." + for livro in self.livros: + if livro.nome == titulo: + mensagem = livro + return mensagem + + def devolver_livro(self, titulo): + mensagem = "Livro não encontrado." + for livro in self.livros: + if livro.nome == titulo: + livro.esta_emprestado = False + mensagem = f"Livro devolvido: {livro}" + return mensagem \ No newline at end of file diff --git a/exercicios/para-casa/projetoGuiado_CrisPereira/Livro.py b/exercicios/para-casa/projetoGuiado_CrisPereira/Livro.py new file mode 100644 index 0000000..43acb7a --- /dev/null +++ b/exercicios/para-casa/projetoGuiado_CrisPereira/Livro.py @@ -0,0 +1,6 @@ +# Exercício Cris Pereira - semana 08 +class Livro: + def __init__(self, nome, autor): + self.nome = nome + self.autor = autor + self.esta_emprestado = False diff --git a/exercicios/para-casa/projetoGuiado_CrisPereira/testBiblioteca.py b/exercicios/para-casa/projetoGuiado_CrisPereira/testBiblioteca.py new file mode 100644 index 0000000..d7279e0 --- /dev/null +++ b/exercicios/para-casa/projetoGuiado_CrisPereira/testBiblioteca.py @@ -0,0 +1,110 @@ +# Testes Cris Pereira - semana 08 +from unittest import TestCase +from Biblioteca import Biblioteca +from Livro import Livro + +class TestBiblioteca(TestCase): + def setUp(self): + self.biblioteca = Biblioteca() + self.livro1 = Livro("Sister Outsider", "Audre Lorde") + self.livro2 = Livro("Memórias da Plantação", "Grada Kilomba") + + def test_init_deve_passar(self): + # Arrange / Act feitos no setup() + # Assert + self.assertIsInstance(self.biblioteca.livros, list) + + def test_adicionar_livro_deve_passar(self): + # Arrange + nome_livro = "O mito da beleza" + autor_livro = "Naomi Wolf" + livro = Livro(nome_livro, autor_livro) + + # Act + self.biblioteca.adicionar_livro(livro) + + # Assert + self.assertEqual(1, len(self.biblioteca.livros)) + + def test_adicionar_livro_nao_deve_inserir_numero(self): + # Arrange + livro = 1988 + + # Act / Assert + with self.assertRaises(TypeError): + self.biblioteca.adicionar_livro(livro) + +# Novos testes abaixo + def test_exibir_livros(self): + #Arrange + self.biblioteca.adicionar_livro(self.livro1) + self.biblioteca.adicionar_livro(self.livro2) + lista_biblioteca = [self.livro1, self.livro2] + + #Act / Assert + self.assertEqual(self.biblioteca.exibir_livros(), lista_biblioteca) + + def test_emprestar_livros(self): + #Arrange + self.biblioteca.adicionar_livro(self.livro1) + self.biblioteca.adicionar_livro(self.livro2) + + #Act + self.biblioteca.emprestar_livro("Sister Outsider") + self.biblioteca.emprestar_livro("Memórias da Plantação") + + #Assert + self.assertTrue(self.livro1.esta_emprestado) + self.assertTrue(self.livro2.esta_emprestado) + + def test_remover_livro(self): + #Arrange + self.biblioteca.adicionar_livro(self.livro1) + self.biblioteca.adicionar_livro(self.livro2) + + #Act + self.biblioteca.remover_livro("Sister Outsider") + + #Assert + self.assertIsNot(self.biblioteca.livros, self.livro1) + + + def test_buscar_livro_existente(self): + #Arrange + self.biblioteca.adicionar_livro(self.livro1) + self.biblioteca.adicionar_livro(self.livro2) + + #Act + livro_buscado = self.biblioteca.buscar_livro("Sister Outsider") + + #Assert + self.assertEqual(livro_buscado, self.livro1) + + def test_buscar_livro_inexistente(self): + #Arrange + self.biblioteca.adicionar_livro(self.livro1) + + #Act + livro_buscado = self.biblioteca.buscar_livro("Memórias da Plantação") + + #Assert + self.assertEqual(livro_buscado, "Livro não encontrado.") + + def test_devolver_livro(self): + #Arrange + self.biblioteca.adicionar_livro(self.livro1) + self.biblioteca.adicionar_livro(self.livro2) + self.biblioteca.emprestar_livro("Sister Outsider") + self.biblioteca.emprestar_livro("Memórias da Plantação") + + #Act + self.biblioteca.devolver_livro("Memórias da Plantação") + + #Assert + self.assertFalse(self.livro2.esta_emprestado) + + + + + + diff --git a/exercicios/para-casa/projetoGuiado_CrisPereira/testLivro.py b/exercicios/para-casa/projetoGuiado_CrisPereira/testLivro.py new file mode 100644 index 0000000..39b811b --- /dev/null +++ b/exercicios/para-casa/projetoGuiado_CrisPereira/testLivro.py @@ -0,0 +1,17 @@ +# Exercício Cris Pereira - semana 08 +from unittest import TestCase +from Livro import Livro + +class TestLivro(TestCase): + def test_init_deve_passar(self): + # Arrange + nome = "Calibã e a bruxa" + autor = "Silvia Federici" + + # Act + livro = Livro(nome, autor) + + # Assert + self.assertEqual(nome, livro.nome) + self.assertEqual(autor, livro.autor) + self.assertEqual(False, livro.esta_emprestado) \ No newline at end of file