/
Cuenta.cs
246 lines (194 loc) · 7.14 KB
/
Cuenta.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace A881457.Actividad03
{
class Cuenta
{
public int Codigo { get; set; }
public string Nombre { get; set; }
public string Tipo { get; set; }
public Cuenta()
{
}
public Cuenta(string linea)
{
var datos = linea.Split('|');
Codigo = int.Parse(datos[0]);
Nombre = datos[1];
Tipo = datos[2];
}
public static Cuenta IngresarNueva()
{
var cuenta = new Cuenta();
Console.WriteLine();
Console.WriteLine("Se desea ingresar una nueva cuenta al Plan de Cuentas...");
cuenta.Codigo = IngresarCodigoCuenta(obligatorio: true);
cuenta.Nombre = IngresarNombreCuenta("Ingrese el nombre de la cuenta: ");
cuenta.Tipo = IngresarTipoCuenta("Ingrese el tipo de cuenta (Activo, Pasivo o PatrimonioNeto): ");
return cuenta;
}
public object ObtenerLineaDatos()
{
return $"{Codigo}|{Nombre}|{Tipo}";
}
public bool CoincideCon(Cuenta modelo)
{
if ((modelo.Codigo != 0) && (Codigo != modelo.Codigo))
{
return false;
}
if ((!string.IsNullOrWhiteSpace(modelo.Nombre)) && (Nombre != modelo.Nombre))
{
return false;
}
if ((!string.IsNullOrWhiteSpace(modelo.Tipo)) && (Tipo != modelo.Tipo))
{
return false;
}
return true;
}
public static Cuenta ModelodeBusqueda()
{
var modelo = new Cuenta();
modelo.Codigo = IngresarCodigoCuenta(obligatorio: false);
modelo.Nombre = IngresarNombreCuenta("Ingrese el nombre de la cuenta que desea buscar: ", obligatorio: false);
modelo.Tipo = IngresarTipoCuenta("Ingrese el tipo de cuenta que desea buscar: ", obligatorio: false);
return modelo;
}
private static int IngresarCodigoCuenta(bool obligatorio = true)
{
var titulo = "Ingrese el código de la cuenta (> a 1)";
if (!obligatorio)
{
titulo += " o presione [ENTER] para continuar.";
}
Console.WriteLine(titulo);
do
{
String codigoIngreso = Console.ReadLine();
if (!obligatorio && string.IsNullOrWhiteSpace(codigoIngreso))
{
return 0;
}
if (!int.TryParse(codigoIngreso, out int codigo))
{
Console.WriteLine("No se ingresó un código válido.");
continue;
}
if (codigo < 1)
{
Console.WriteLine("No se ingresó un código válido.");
continue;
}
if (obligatorio && PlanDeCuentas.Existe(codigo))
{
Console.WriteLine("El código ingresado ya existe en el Plan de Cuentas.");
continue;
}
return codigo;
} while (true);
}
private static string IngresarNombreCuenta(string titulo, bool obligatorio = true)
{
string nombreIngreso;
do
{
Console.WriteLine(titulo);
nombreIngreso = Console.ReadLine();
if (!obligatorio && string.IsNullOrWhiteSpace(nombreIngreso))
{
return null;
}
if (string.IsNullOrWhiteSpace(nombreIngreso))
{
Console.WriteLine("No se ingresó un nombre válido. Por favor, intentelo nuevamente.");
continue;
}
if (nombreIngreso.Any(c => Char.IsDigit(c)))
{
Console.WriteLine("El nombre de la cuenta no puede contener números. Por favor, intentelo nuevamente.");
continue;
}
else
{
return nombreIngreso;
}
} while (true);
}
private static string IngresarTipoCuenta(string titulo, bool obligatorio = true)
{
string tipoIngreso;
do
{
Console.WriteLine(titulo);
tipoIngreso = Console.ReadLine();
if (!obligatorio && string.IsNullOrWhiteSpace(tipoIngreso))
{
return null;
}
if (obligatorio && string.IsNullOrWhiteSpace(tipoIngreso))
{
Console.WriteLine("No se ingresó un tipo válido.");
continue;
}
if ((tipoIngreso.ToUpper() == "ACTIVO") || (tipoIngreso.ToUpper() == "PASIVO") || (tipoIngreso.ToUpper() == "PATRIMONIONETO"))
{
return tipoIngreso;
}
else
{
Console.WriteLine("El tipo de cuenta debe ser alguno de los siguientes: Activo, Pasivo o PatrimonioNeto.");
continue;
}
} while (true);
}
public void MostrarDatos()
{
Console.WriteLine();
Console.WriteLine($"Codigo de cuenta: {Codigo}");
Console.WriteLine($"Nombre de la cuenta: {Nombre}");
Console.WriteLine($"Tipo de cuenta: {Tipo}");
Console.WriteLine();
}
public void Modificar()
{
Console.WriteLine($"Nombre de la cuenta: {Nombre}. Presione M para modificar o cualquier otra tecla para continuar.");
var key = Console.ReadKey(true);
if (key.Key == ConsoleKey.M)
{
Nombre = IngresarNombreCuenta("Ingrese el nuevo nombre de la cuenta: ");
}
Console.WriteLine($"Tipo de cuenta: {Tipo}. Presione K para modificar o cualquier otra tecla para continuar.");
var tecla = Console.ReadKey(true);
if (tecla.Key == ConsoleKey.K)
{
Tipo = IngresarTipoCuenta("Ingrese el nuevo tipo de cuenta: ");
}
PlanDeCuentas.GrabarCuenta();
}
public static string Ingreso(string titulo)
{
string ingreso;
Console.WriteLine(titulo);
do
{
ingreso = Console.ReadLine();
if (string.IsNullOrWhiteSpace(ingreso))
{
Console.WriteLine("No se ingresó una opción válida.");
break;
}
if (ingreso.Any(c => Char.IsDigit(c)))
{
Console.WriteLine("El nombre de la cuenta no puede contener números. Por favor, intentelo nuevamente.");
break;
}
} while (true);
return ingreso;
}
}
}