|
| 1 | +' _____________________________________ |
| 2 | +' https://github.com/kenysdev |
| 3 | +' 2024 - vb.net |
| 4 | +' _____________________________________ |
| 5 | +' 50 PLANIFICADOR DE OBJETIVOS DE AÑO NUEVO |
| 6 | +' ------------------------------------ |
| 7 | +'* EJERCICIO |
| 8 | +' * El nuevo año está a punto de comenzar... |
| 9 | +' * ¡Voy a ayudarte a planificar tus propósitos de nuevo año! |
| 10 | +' * |
| 11 | +' * Programa un gestor de objetivos con las siguientes características: |
| 12 | +' * - Permite añadir objetivos (máximo 10) |
| 13 | +' * - Calcular el plan detallado |
| 14 | +' * - Guardar la planificación |
| 15 | +' * |
| 16 | +' * Cada entrada de un objetivo está formado por (con un ejemplo): |
| 17 | +' * - Meta: Leer libros |
| 18 | +' * - Cantidad: 12 |
| 19 | +' * - Unidades: libros |
| 20 | +' * - Plazo (en meses): 12 (máximo 12) |
| 21 | +' * |
| 22 | +' * El cálculo del plan detallado generará la siguiente salida: |
| 23 | +' * - Un apartado para cada mes |
| 24 | +' * - Un listado de objetivos calculados a cumplir en cada mes |
| 25 | +' * (ejemplo: si quiero leer 12 libros, dará como resultado |
| 26 | +' * uno al mes) |
| 27 | +' * - Cada objetivo debe poseer su nombre, la cantidad de |
| 28 | +' * unidades a completar en cada mes y su total. Por ejemplo: |
| 29 | +' * |
| 30 | +' * Enero: |
| 31 | +' * [ ] 1. Leer libros (1 libro/mes). Total 12. |
| 32 | +' * [ ] 2. Estudiar Git (1 curso/mes). Total: 1. |
| 33 | +' * Febrero: |
| 34 | +' * [ ] 1. Leer libros (1 libro/mes). Total 12. |
| 35 | +' * ... |
| 36 | +' * Diciembre: |
| 37 | +' * [ ] 1. Leer libros (1 libro/mes). Total 12. |
| 38 | +' * |
| 39 | +' * - Si la duración es menor a un año, finalizará en el mes |
| 40 | +' * correspondiente. |
| 41 | +' * |
| 42 | +' * Por último, el cálculo detallado debe poder exportarse a .txt |
| 43 | +' * (No subir el fichero) |
| 44 | + |
| 45 | +Imports System |
| 46 | +Imports System.IO |
| 47 | +Imports System.Collections.Generic |
| 48 | +Imports System.Globalization |
| 49 | + |
| 50 | +Public Class ObjectivePlanner |
| 51 | + Private ReadOnly goals As New List(Of Dictionary(Of String, Object)) |
| 52 | + Private ReadOnly months As List(Of String) |
| 53 | + Private ReadOnly pendingMonthly As New Dictionary(Of Integer, List(Of Integer)) |
| 54 | + |
| 55 | + Public Sub New() |
| 56 | + months = Enumerable.Range(1, 12).[Select](Function(m) DateTimeFormatInfo.CurrentInfo.GetMonthName(m)).ToList() |
| 57 | + goals = New List(Of Dictionary(Of String, Object)) |
| 58 | + pendingMonthly = New Dictionary(Of Integer, List(Of Integer)) |
| 59 | + End Sub |
| 60 | + |
| 61 | + Private Sub Add() |
| 62 | + If goals.Count >= 10 Then |
| 63 | + Console.WriteLine(vbLf & "Máximo de 10 objetivos alcanzado.") |
| 64 | + Return |
| 65 | + End If |
| 66 | + |
| 67 | + Try |
| 68 | + Console.Write("Meta: ") |
| 69 | + Dim name As String = Console.ReadLine().Trim() |
| 70 | + |
| 71 | + Console.Write("Cantidad: ") |
| 72 | + Dim quantity As Integer = Integer.Parse(Console.ReadLine()) |
| 73 | + |
| 74 | + Console.Write("Unidades: ") |
| 75 | + Dim units As String = Console.ReadLine().Trim() |
| 76 | + |
| 77 | + Console.Write("Plazo (en meses): ") |
| 78 | + Dim monthCount As Integer = Math.Min(Integer.Parse(Console.ReadLine()), 12) |
| 79 | + |
| 80 | + If Not String.IsNullOrEmpty(name) AndAlso quantity > 0 AndAlso |
| 81 | + Not String.IsNullOrEmpty(units) AndAlso monthCount > 0 Then |
| 82 | + |
| 83 | + Dim goal As New Dictionary(Of String, Object) From { |
| 84 | + {"name", name}, |
| 85 | + {"quantity", quantity}, |
| 86 | + {"units", units}, |
| 87 | + {"months", monthCount} |
| 88 | + } |
| 89 | + |
| 90 | + Dim goalId As Integer = goals.Count |
| 91 | + goals.Add(goal) |
| 92 | + |
| 93 | + Dim monthly As Integer = quantity \ monthCount |
| 94 | + Dim extra As Integer = quantity Mod monthCount |
| 95 | + |
| 96 | + pendingMonthly(goalId) = Enumerable.Range(0, monthCount).[Select]( |
| 97 | + Function(m) monthly + If(m < extra, 1, 0)).ToList() |
| 98 | + |
| 99 | + Console.WriteLine(vbLf & "Objetivo añadido exitosamente.") |
| 100 | + Else |
| 101 | + Console.WriteLine(vbLf & "Datos inválidos.") |
| 102 | + End If |
| 103 | + |
| 104 | + Catch ex As FormatException |
| 105 | + Console.WriteLine(vbLf & "Error: Ingrese valores numéricos válidos.") |
| 106 | + End Try |
| 107 | + End Sub |
| 108 | + |
| 109 | + Private Function CalculatePlan() As Dictionary(Of String, List(Of String)) |
| 110 | + If goals.Count = 0 Then Return Nothing |
| 111 | + |
| 112 | + Dim plan As New Dictionary(Of String, List(Of String)) |
| 113 | + |
| 114 | + For goalId As Integer = 0 To goals.Count - 1 |
| 115 | + Dim goal As Dictionary(Of String, Object) = goals(goalId) |
| 116 | + Dim monthlyQuantities As List(Of Integer) = pendingMonthly(goalId) |
| 117 | + |
| 118 | + For month As Integer = 0 To monthlyQuantities.Count - 1 |
| 119 | + Dim quantity As Integer = monthlyQuantities(month) |
| 120 | + If quantity > 0 Then |
| 121 | + Dim monthName As String = months(month) |
| 122 | + |
| 123 | + Dim value As List(Of String) = Nothing |
| 124 | + |
| 125 | + If Not plan.TryGetValue(monthName, value) Then |
| 126 | + value = New List(Of String) |
| 127 | + plan(monthName) = value |
| 128 | + End If |
| 129 | + |
| 130 | + value.Add( |
| 131 | + $"[ ] {goal("name")} ({quantity} {goal("units")}/mes). " & |
| 132 | + $"Total: {goal("quantity")}.") |
| 133 | + End If |
| 134 | + Next |
| 135 | + Next |
| 136 | + |
| 137 | + Return plan.Where(Function(kvp) kvp.Value.Count > 0). |
| 138 | + ToDictionary(Function(kvp) kvp.Key, Function(kvp) kvp.Value) |
| 139 | + End Function |
| 140 | + |
| 141 | + Private Shared Sub SavePlan(plan As Dictionary(Of String, List(Of String))) |
| 142 | + If plan Is Nothing Then |
| 143 | + Console.WriteLine(vbLf & "No hay planificación para guardar.") |
| 144 | + Return |
| 145 | + End If |
| 146 | + |
| 147 | + Dim filename As String = $"plan_{DateTime.Now:yyyyMMdd_HHmm}.txt" |
| 148 | + Try |
| 149 | + Using writer As New StreamWriter(filename, False, Text.Encoding.UTF8) |
| 150 | + For Each kvp In plan |
| 151 | + writer.WriteLine($"{kvp.Key}:") |
| 152 | + For Each task In kvp.Value |
| 153 | + writer.WriteLine($" {task}") |
| 154 | + Next |
| 155 | + writer.WriteLine() |
| 156 | + Next |
| 157 | + End Using |
| 158 | + |
| 159 | + Console.WriteLine(vbLf & $"Plan guardado en {filename}.") |
| 160 | + |
| 161 | + Catch ex As IOException |
| 162 | + Console.WriteLine(vbLf & "Error al guardar el archivo.") |
| 163 | + End Try |
| 164 | + End Sub |
| 165 | + |
| 166 | + Private Sub DisplayPlan() |
| 167 | + Dim plan As Dictionary(Of String, List(Of String)) = CalculatePlan() |
| 168 | + If plan IsNot Nothing Then |
| 169 | + For Each kvp In plan |
| 170 | + Console.WriteLine(vbLf & $"{kvp.Key}:") |
| 171 | + For Each task In kvp.Value |
| 172 | + Console.WriteLine($" {task}") |
| 173 | + Next |
| 174 | + Next |
| 175 | + End If |
| 176 | + End Sub |
| 177 | + |
| 178 | + Public Sub Run() |
| 179 | + Console.Clear() |
| 180 | + |
| 181 | + While True |
| 182 | + Console.WriteLine(vbLf & "Gestor de Objetivos:") |
| 183 | + Console.WriteLine("1. Añadir objetivo") |
| 184 | + Console.WriteLine("2. Calcular plan detallado") |
| 185 | + Console.WriteLine("3. Guardar planificación") |
| 186 | + Console.WriteLine("4. Salir") |
| 187 | + |
| 188 | + Console.Write(vbLf & "Seleccione una opción: ") |
| 189 | + Dim option_ As String = Console.ReadLine().Trim() |
| 190 | + |
| 191 | + Select Case option_ |
| 192 | + Case "1" |
| 193 | + Add() |
| 194 | + Case "2" |
| 195 | + DisplayPlan() |
| 196 | + Case "3" |
| 197 | + SavePlan(CalculatePlan()) |
| 198 | + Case "4" |
| 199 | + Console.WriteLine(vbLf & "¡Adiós!") |
| 200 | + Return |
| 201 | + Case Else |
| 202 | + Console.WriteLine(vbLf & "Opción inválida.") |
| 203 | + End Select |
| 204 | + End While |
| 205 | + End Sub |
| 206 | +End Class |
| 207 | + |
| 208 | +Public Module Program |
| 209 | + Public Sub Main() |
| 210 | + Dim planner As New ObjectivePlanner() |
| 211 | + planner.Run() |
| 212 | + End Sub |
| 213 | +End Module |
0 commit comments