diff --git a/python-ofensivo/06_proyecto_gestor_notas/gestor_notas.py b/python-ofensivo/06_proyecto_gestor_notas/gestor_notas.py new file mode 100644 index 0000000..5d83ef0 --- /dev/null +++ b/python-ofensivo/06_proyecto_gestor_notas/gestor_notas.py @@ -0,0 +1,74 @@ +#!/usr/bin/env python3 + +import pickle +from notas import Nota + + +class GestorNotas: + + def __init__(self, archivo_notas='notas.pkl'): + + self.archivo_notas = archivo_notas + + try: + + with open(self.archivo_notas, 'rb') as f: + + self.notas = pickle.load(f) + + except FileNotFoundError: + + self.notas = [] + + def guardar_notas(self): + + with open(self.archivo_notas, 'wb') as f: + pickle.dump(self.notas, f) + + def agregar_nota(self, titulo, contenido): + + self.notas.append(Nota(titulo, contenido)) + self.guardar_notas() + + def leer_notas(self): + for i, nota in enumerate(self.notas): + print(f"- NOTA {i+1}: {nota}") + + def buscar_nota(self, texto_busqueda): + + notas_encontradas = [] + + for nota in self.notas: + + if texto_busqueda in nota.titulo or texto_busqueda in nota.contenido: + + notas_encontradas.append(nota) + + if notas_encontradas: + + print(f"\n[i] Resultado de la búsqueda\n") + + for i, nota in enumerate(notas_encontradas): + + print(f"- RESULTADO {i+1}: {nota}") + + notas_encontradas.clear() + + else: + + print(f"\n[!] No se encontraron resultados\n") + + def eliminar_nota(self, index): + + # if index >= 1 or index <= len(self.notas): + + try: + index_a_borrar = int(index - 1) + print( + f"\n[+] Eliminando nota {index}: {self.notas[index_a_borrar]}") + del self.notas[index_a_borrar] + self.guardar_notas() + + except IndexError: + + print(f"\n[!] Error: No existe la nota\n") diff --git a/python-ofensivo/06_proyecto_gestor_notas/main.py b/python-ofensivo/06_proyecto_gestor_notas/main.py new file mode 100644 index 0000000..f9bedc3 --- /dev/null +++ b/python-ofensivo/06_proyecto_gestor_notas/main.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python3 + +import os +from gestor_notas import GestorNotas + + +def main(): + + gestor = GestorNotas() + + while True: + + os.system('cls' if os.name == 'nt' else 'clear') + + print(f"---------------\nMENÚ PRINCIPAL\n---------------") + print(f"1. Crear nota") + print(f"2. Leer todas las notas") + print(f"3. Buscar por una nota") + print(f"4. Eliminar una nota") + print(f"5. Salir") + + opcion = input("\n Elige una opción: ") + + if opcion == "1": + print(f"\n[+] Crear nota\n") + titulo = input("Título: ") + contenido = input("Contenido: ") + gestor.agregar_nota(titulo, contenido) + + elif opcion == "2": + print(f"\n[i] Mostrando todas las notas\n") + notas = gestor.leer_notas() + + elif opcion == "3": + texto_busqueda = input(" Ingresa texto a buscar: ") + nota = gestor.buscar_nota(texto_busqueda) + + elif opcion == "4": + + try: + + index = int(input(" Introducir índice de la nota a eliminar: ")) + gestor.eliminar_nota(index) + + except ValueError: + + print(f"\n[!] Error: Opción no válida.\n") + + elif opcion == "5": + print(f"\n[!] Saliendo... ¡Hasta pronto!\n") + break + + else: + print(f"\n[!] Error: Opción no válida\n") + + input(f"\n[+] Pulsa para continuar...\n") + + +if __name__ == "__main__": + main() diff --git a/python-ofensivo/06_proyecto_gestor_notas/notas.pkl b/python-ofensivo/06_proyecto_gestor_notas/notas.pkl new file mode 100644 index 0000000..afab044 Binary files /dev/null and b/python-ofensivo/06_proyecto_gestor_notas/notas.pkl differ diff --git a/python-ofensivo/06_proyecto_gestor_notas/notas.py b/python-ofensivo/06_proyecto_gestor_notas/notas.py new file mode 100644 index 0000000..543d6d0 --- /dev/null +++ b/python-ofensivo/06_proyecto_gestor_notas/notas.py @@ -0,0 +1,13 @@ +#!/usr/bin/env python3 + +class Nota: + + def __init__(self, titulo, contenido): + self.titulo = titulo + self.contenido = contenido + + def __str__(self): + return f""" + Título: {self.titulo.upper()} + {self.contenido} + """