1
+ /** #50 - JavaScript -> Jesus Antonio Escamilla */
2
+
3
+ /**
4
+ * PLANIFICADOR DE OBJETIVOS DE AÑO NUEVO.
5
+ * Utilizando la Terminal para pedir datos y agregarlos.
6
+ */
7
+
8
+ const fs = require ( 'fs' ) ;
9
+
10
+ class GoalManager {
11
+ constructor ( ) {
12
+ this . goals = [ ] ;
13
+ }
14
+
15
+ addGoal ( name , quantity , unit , months ) {
16
+ if ( this . goals . length >= 10 ) {
17
+ console . log ( "\nNo puedes añadir más de 10 objetivos." ) ;
18
+ return ;
19
+ }
20
+
21
+ if ( months > 12 || months <= 0 ) {
22
+ console . log ( "\nEl plazo debe ser entre 1 y 12 meses." ) ;
23
+ return ;
24
+ }
25
+
26
+ this . goals . push ( {
27
+ name,
28
+ quantity,
29
+ unit,
30
+ months
31
+ } ) ;
32
+
33
+ console . log ( `\nObjetivo añadido: ${ name } (${ quantity } ${ unit } , ${ months } meses).` ) ;
34
+ }
35
+
36
+ generateDetailedPlan ( ) {
37
+ if ( this . goals . length === 0 ) {
38
+ console . log ( "\nNo hay objetivos para planificar." ) ;
39
+ return ;
40
+ }
41
+
42
+ let plan = "\nPlanificación detallada:\n\n" ;
43
+ const monthsNames = [
44
+ "Enero" ,
45
+ "Febrero" ,
46
+ "Marzo" ,
47
+ "Abril" ,
48
+ "Mayo" ,
49
+ "Junio" ,
50
+ "Julio" ,
51
+ "Agosto" ,
52
+ "Septiembre" ,
53
+ "Octubre" ,
54
+ "Noviembre" ,
55
+ "Diciembre" ,
56
+ ] ;
57
+
58
+ for ( let i = 0 ; i < 12 ; i ++ ) {
59
+ let currentMothGoals = [ ] ;
60
+ this . goals . forEach ( ( goal , index ) => {
61
+ if ( i < goal . months ) {
62
+ const monthlyQuantity = Math . ceil ( goal . quantity / goal . months ) ;
63
+ currentMothGoals . push (
64
+ `[ ] ${ index + 1 } . ${ goal . name } (${ monthlyQuantity } ${ goal . unit } /mes). Total: ${ goal . quantity } .\n`
65
+ ) ;
66
+ }
67
+ } ) ;
68
+
69
+ if ( currentMothGoals . length > 0 ) {
70
+ plan += `\n${ monthsNames [ i ] } :\n` ;
71
+ plan += currentMothGoals . join ( "\n" + "\n\n" ) ;
72
+ }
73
+ }
74
+
75
+ return plan ;
76
+ }
77
+
78
+ savePlanToFile ( filename = "detailed_plan.txt" ) {
79
+ const plan = this . generateDetailedPlan ( ) ;
80
+ if ( ! plan ) return ;
81
+
82
+ fs . writeFile ( filename , plan , ( err ) => {
83
+ if ( err ) {
84
+ console . log ( "\nError al guardar el archivo:" , err ) ;
85
+ } else {
86
+ console . log ( `\nPlanificación guardada en el archivo: ${ filename } ` ) ;
87
+ }
88
+ } ) ;
89
+ }
90
+ }
91
+
92
+ // Ejemplo para usarlo
93
+ const manager = new GoalManager ( ) ;
94
+
95
+ manager . addGoal ( "Leer libros" , 12 , "libros" , 12 ) ;
96
+ manager . addGoal ( "Estudiar Git" , 1 , "curso" , 6 ) ;
97
+ manager . addGoal ( "Correr" , 500 , "km" , 12 ) ;
98
+
99
+ const detailedPlan = manager . generateDetailedPlan ( ) ;
100
+ if ( detailedPlan ) {
101
+ console . log ( detailedPlan ) ;
102
+ }
103
+ manager . savePlanToFile ( ) ;
0 commit comments