|
| 1 | +# _____________________________________ |
| 2 | +# https://github.com/kenysdev |
| 3 | +# 2024 - Python |
| 4 | +# _____________________________________ |
| 5 | +# #50 PLANIFICADOR DE OBJETIVOS DE AÑO NUEVO |
| 6 | +# ------------------------------------ |
| 7 | +""" |
| 8 | +* EJERCICIO: |
| 9 | + * El nuevo año está a punto de comenzar... |
| 10 | + * ¡Voy a ayudarte a planificar tus propósitos de nuevo año! |
| 11 | + * |
| 12 | + * Programa un gestor de objetivos con las siguientes características: |
| 13 | + * - Permite añadir objetivos (máximo 10) |
| 14 | + * - Calcular el plan detallado |
| 15 | + * - Guardar la planificación |
| 16 | + * |
| 17 | + * Cada entrada de un objetivo está formado por (con un ejemplo): |
| 18 | + * - Meta: Leer libros |
| 19 | + * - Cantidad: 12 |
| 20 | + * - Unidades: libros |
| 21 | + * - Plazo (en meses): 12 (máximo 12) |
| 22 | + * |
| 23 | + * El cálculo del plan detallado generará la siguiente salida: |
| 24 | + * - Un apartado para cada mes |
| 25 | + * - Un listado de objetivos calculados a cumplir en cada mes |
| 26 | + * (ejemplo: si quiero leer 12 libros, dará como resultado |
| 27 | + * uno al mes) |
| 28 | + * - Cada objetivo debe poseer su nombre, la cantidad de |
| 29 | + * unidades a completar en cada mes y su total. Por ejemplo: |
| 30 | + * |
| 31 | + * Enero: |
| 32 | + * [ ] 1. Leer libros (1 libro/mes). Total: 12. |
| 33 | + * [ ] 2. Estudiar Git (1 curso/mes). Total: 1. |
| 34 | + * Febrero: |
| 35 | + * [ ] 1. Leer libros (1 libro/mes). Total: 12. |
| 36 | + * ... |
| 37 | + * Diciembre: |
| 38 | + * [ ] 1. Leer libros (1 libro/mes). Total: 12. |
| 39 | + * |
| 40 | + * - Si la duración es menor a un año, finalizará en el mes |
| 41 | + * correspondiente. |
| 42 | + * |
| 43 | + * Por último, el cálculo detallado debe poder exportarse a .txt |
| 44 | + * (No subir el fichero) |
| 45 | +""" |
| 46 | + |
| 47 | +import os |
| 48 | +from calendar import month_name as calendar_months |
| 49 | +from datetime import datetime |
| 50 | + |
| 51 | +class ObjectivePlanner: |
| 52 | + def __init__(self): |
| 53 | + self.goals = [] |
| 54 | + self.months = list(calendar_months)[1:] |
| 55 | + self.pending_monthly = {} |
| 56 | + |
| 57 | + def add(self): |
| 58 | + if len(self.goals) >= 10: |
| 59 | + print("\nMáximo de 10 objetivos alcanzado.") |
| 60 | + return |
| 61 | + |
| 62 | + try: |
| 63 | + goal = { |
| 64 | + 'name': input("Meta: ").strip(), |
| 65 | + 'quantity': int(input("Cantidad: ")), |
| 66 | + 'units': input("Unidades: ").strip(), |
| 67 | + 'months': min(int(input("Plazo (en meses): ")), 12) |
| 68 | + } |
| 69 | + |
| 70 | + if all(goal.values()): |
| 71 | + goal_id = len(self.goals) |
| 72 | + self.goals.append(goal) |
| 73 | + monthly, extra = divmod(goal['quantity'], goal['months']) |
| 74 | + self.pending_monthly[goal_id] = [monthly + (1 if m < extra else 0) |
| 75 | + for m in range(goal['months'])] |
| 76 | + print("\nObjetivo añadido exitosamente.") |
| 77 | + else: |
| 78 | + print("\nDatos inválidos.") |
| 79 | + |
| 80 | + except ValueError: |
| 81 | + print("\nError: Ingrese valores numéricos válidos.") |
| 82 | + |
| 83 | + def calculate_plan(self): |
| 84 | + if not self.goals: |
| 85 | + return None |
| 86 | + |
| 87 | + plan = {} |
| 88 | + for goal_id, goal in enumerate(self.goals): |
| 89 | + monthly_quantities = self.pending_monthly[goal_id] |
| 90 | + |
| 91 | + for month, quantity in enumerate(monthly_quantities): |
| 92 | + if quantity > 0: |
| 93 | + month_name = self.months[month] |
| 94 | + plan.setdefault(month_name, []).append( |
| 95 | + f"[ ] {goal['name']} ({quantity} " |
| 96 | + f"{goal['units']}/mes). Total: {goal['quantity']}." |
| 97 | + ) |
| 98 | + |
| 99 | + return {month: tasks for month, tasks in plan.items() if tasks} |
| 100 | + |
| 101 | + def save_plan(self, plan): |
| 102 | + if not plan: |
| 103 | + print("\nNo hay planificación para guardar.") |
| 104 | + return |
| 105 | + |
| 106 | + filename = f"plan_{datetime.now():%Y%m%d_%H%M}.txt" |
| 107 | + try: |
| 108 | + with open(filename, "w", encoding="utf-8") as f: |
| 109 | + for month, tasks in plan.items(): |
| 110 | + f.write(f"{month}:\n") |
| 111 | + for task in tasks: |
| 112 | + f.write(f" {task}\n") |
| 113 | + |
| 114 | + f.write("\n") |
| 115 | + |
| 116 | + print(f"\nPlan guardado en {filename}.") |
| 117 | + |
| 118 | + except IOError: |
| 119 | + print("\nError al guardar el archivo.") |
| 120 | + |
| 121 | + def display_plan(self): |
| 122 | + if plan := self.calculate_plan(): |
| 123 | + for month, tasks in plan.items(): |
| 124 | + print(f"\n{month}:") |
| 125 | + print('\n'.join(f" {task}" for task in tasks)) |
| 126 | + |
| 127 | + def run(self): |
| 128 | + os.system("cls" if os.name == "nt" else "clear") |
| 129 | + menu = { |
| 130 | + "1": lambda: self.add(), |
| 131 | + "2": lambda: self.display_plan(), |
| 132 | + "3": lambda: self.save_plan(self.calculate_plan()), |
| 133 | + "4": lambda: exit("\n¡Adiós!") |
| 134 | + } |
| 135 | + |
| 136 | + while True: |
| 137 | + print("\nGestor de Objetivos:") |
| 138 | + print("1. Añadir objetivo") |
| 139 | + print("2. Calcular plan detallado") |
| 140 | + print("3. Guardar planificación") |
| 141 | + print("4. Salir") |
| 142 | + |
| 143 | + if action := menu.get(input("\nSeleccione una opción: ").strip()): |
| 144 | + action() |
| 145 | + else: |
| 146 | + print("\nOpción inválida.") |
| 147 | + |
| 148 | +if __name__ == "__main__": |
| 149 | + ObjectivePlanner().run() |
0 commit comments