1
+ import java .io .*;
2
+ import java .util .*;
3
+
4
+ public class eulogioep {
5
+ // Constantes para el nombre del archivo y el archivo de ventas
6
+ private static final String FILENAME = "eulogioep.txt" ;
7
+ private static final String SALES_FILENAME = "ventas.txt" ;
8
+
9
+ public static void main (String [] args ) {
10
+ // Parte 1: Creación y manipulación de archivos
11
+ crearArchivoPersonal ();
12
+
13
+ // Parte 2: Gestión de ventas (DIFICULTAD EXTRA)
14
+ gestionVentas ();
15
+ }
16
+
17
+ // Método para crear y manipular el archivo personal
18
+ private static void crearArchivoPersonal () {
19
+ try {
20
+ // Creación del archivo
21
+ FileWriter fw = new FileWriter (FILENAME );
22
+ BufferedWriter bw = new BufferedWriter (fw );
23
+
24
+ // Escritura en el archivo
25
+ bw .write ("Nombre: Eulogio" );
26
+ bw .newLine ();
27
+ bw .write ("Edad: 30" );
28
+ bw .newLine ();
29
+ bw .write ("Lenguaje de programación favorito: Java" );
30
+ bw .close ();
31
+
32
+ System .out .println ("Archivo creado y escrito con éxito." );
33
+
34
+ // Lectura y impresión del contenido
35
+ BufferedReader br = new BufferedReader (new FileReader (FILENAME ));
36
+ String linea ;
37
+ System .out .println ("Contenido del archivo:" );
38
+ while ((linea = br .readLine ()) != null ) {
39
+ System .out .println (linea );
40
+ }
41
+ br .close ();
42
+
43
+ // Borrado del archivo
44
+ File file = new File (FILENAME );
45
+ if (file .delete ()) {
46
+ System .out .println ("Archivo borrado con éxito." );
47
+ } else {
48
+ System .out .println ("No se pudo borrar el archivo." );
49
+ }
50
+ } catch (IOException e ) {
51
+ System .out .println ("Error de E/S: " + e .getMessage ());
52
+ }
53
+ }
54
+
55
+ // Método para la gestión de ventas
56
+ private static void gestionVentas () {
57
+ Scanner scanner = new Scanner (System .in );
58
+ boolean salir = false ;
59
+
60
+ while (!salir ) {
61
+ System .out .println ("\n --- Gestión de Ventas ---" );
62
+ System .out .println ("1. Añadir producto" );
63
+ System .out .println ("2. Consultar productos" );
64
+ System .out .println ("3. Actualizar producto" );
65
+ System .out .println ("4. Eliminar producto" );
66
+ System .out .println ("5. Calcular venta total" );
67
+ System .out .println ("6. Calcular venta por producto" );
68
+ System .out .println ("7. Salir" );
69
+ System .out .print ("Seleccione una opción: " );
70
+
71
+ int opcion = scanner .nextInt ();
72
+ scanner .nextLine (); // Consumir el salto de línea
73
+
74
+ switch (opcion ) {
75
+ case 1 :
76
+ anadirProducto (scanner );
77
+ break ;
78
+ case 2 :
79
+ consultarProductos ();
80
+ break ;
81
+ case 3 :
82
+ actualizarProducto (scanner );
83
+ break ;
84
+ case 4 :
85
+ eliminarProducto (scanner );
86
+ break ;
87
+ case 5 :
88
+ calcularVentaTotal ();
89
+ break ;
90
+ case 6 :
91
+ calcularVentaPorProducto (scanner );
92
+ break ;
93
+ case 7 :
94
+ salir = true ;
95
+ borrarArchivoVentas ();
96
+ break ;
97
+ default :
98
+ System .out .println ("Opción no válida." );
99
+ }
100
+ }
101
+ scanner .close ();
102
+ }
103
+
104
+ // Método para añadir un producto al archivo de ventas
105
+ private static void anadirProducto (Scanner scanner ) {
106
+ try {
107
+ FileWriter fw = new FileWriter (SALES_FILENAME , true );
108
+ BufferedWriter bw = new BufferedWriter (fw );
109
+
110
+ System .out .print ("Nombre del producto: " );
111
+ String nombre = scanner .nextLine ();
112
+ System .out .print ("Cantidad vendida: " );
113
+ int cantidad = scanner .nextInt ();
114
+ System .out .print ("Precio: " );
115
+ double precio = scanner .nextDouble ();
116
+
117
+ bw .write (String .format ("%s, %d, %.2f" , nombre , cantidad , precio ));
118
+ bw .newLine ();
119
+ bw .close ();
120
+
121
+ System .out .println ("Producto añadido con éxito." );
122
+ } catch (IOException e ) {
123
+ System .out .println ("Error al añadir producto: " + e .getMessage ());
124
+ }
125
+ }
126
+
127
+ // Método para consultar los productos en el archivo de ventas
128
+ private static void consultarProductos () {
129
+ try {
130
+ BufferedReader br = new BufferedReader (new FileReader (SALES_FILENAME ));
131
+ String linea ;
132
+ System .out .println ("\n Productos:" );
133
+ while ((linea = br .readLine ()) != null ) {
134
+ System .out .println (linea );
135
+ }
136
+ br .close ();
137
+ } catch (IOException e ) {
138
+ System .out .println ("Error al consultar productos: " + e .getMessage ());
139
+ }
140
+ }
141
+
142
+ // Método para actualizar un producto en el archivo de ventas
143
+ private static void actualizarProducto (Scanner scanner ) {
144
+ try {
145
+ List <String > lineas = new ArrayList <>();
146
+ BufferedReader br = new BufferedReader (new FileReader (SALES_FILENAME ));
147
+ String linea ;
148
+ while ((linea = br .readLine ()) != null ) {
149
+ lineas .add (linea );
150
+ }
151
+ br .close ();
152
+
153
+ System .out .print ("Nombre del producto a actualizar: " );
154
+ String nombreActualizar = scanner .nextLine ();
155
+
156
+ boolean encontrado = false ;
157
+ for (int i = 0 ; i < lineas .size (); i ++) {
158
+ String [] partes = lineas .get (i ).split (", " );
159
+ if (partes [0 ].equals (nombreActualizar )) {
160
+ System .out .print ("Nueva cantidad vendida: " );
161
+ int nuevaCantidad = scanner .nextInt ();
162
+ System .out .print ("Nuevo precio: " );
163
+ double nuevoPrecio = scanner .nextDouble ();
164
+ lineas .set (i , String .format ("%s, %d, %.2f" , nombreActualizar , nuevaCantidad , nuevoPrecio ));
165
+ encontrado = true ;
166
+ break ;
167
+ }
168
+ }
169
+
170
+ if (encontrado ) {
171
+ FileWriter fw = new FileWriter (SALES_FILENAME );
172
+ BufferedWriter bw = new BufferedWriter (fw );
173
+ for (String l : lineas ) {
174
+ bw .write (l );
175
+ bw .newLine ();
176
+ }
177
+ bw .close ();
178
+ System .out .println ("Producto actualizado con éxito." );
179
+ } else {
180
+ System .out .println ("Producto no encontrado." );
181
+ }
182
+ } catch (IOException e ) {
183
+ System .out .println ("Error al actualizar producto: " + e .getMessage ());
184
+ }
185
+ }
186
+
187
+ // Método para eliminar un producto del archivo de ventas
188
+ private static void eliminarProducto (Scanner scanner ) {
189
+ try {
190
+ List <String > lineas = new ArrayList <>();
191
+ BufferedReader br = new BufferedReader (new FileReader (SALES_FILENAME ));
192
+ String linea ;
193
+ while ((linea = br .readLine ()) != null ) {
194
+ lineas .add (linea );
195
+ }
196
+ br .close ();
197
+
198
+ System .out .print ("Nombre del producto a eliminar: " );
199
+ String nombreEliminar = scanner .nextLine ();
200
+
201
+ boolean encontrado = false ;
202
+ for (int i = 0 ; i < lineas .size (); i ++) {
203
+ String [] partes = lineas .get (i ).split (", " );
204
+ if (partes [0 ].equals (nombreEliminar )) {
205
+ lineas .remove (i );
206
+ encontrado = true ;
207
+ break ;
208
+ }
209
+ }
210
+
211
+ if (encontrado ) {
212
+ FileWriter fw = new FileWriter (SALES_FILENAME );
213
+ BufferedWriter bw = new BufferedWriter (fw );
214
+ for (String l : lineas ) {
215
+ bw .write (l );
216
+ bw .newLine ();
217
+ }
218
+ bw .close ();
219
+ System .out .println ("Producto eliminado con éxito." );
220
+ } else {
221
+ System .out .println ("Producto no encontrado." );
222
+ }
223
+ } catch (IOException e ) {
224
+ System .out .println ("Error al eliminar producto: " + e .getMessage ());
225
+ }
226
+ }
227
+
228
+ // Método para calcular la venta total
229
+ private static void calcularVentaTotal () {
230
+ try {
231
+ BufferedReader br = new BufferedReader (new FileReader (SALES_FILENAME ));
232
+ String linea ;
233
+ double ventaTotal = 0 ;
234
+ while ((linea = br .readLine ()) != null ) {
235
+ String [] partes = linea .split (", " );
236
+ int cantidad = Integer .parseInt (partes [1 ]);
237
+ double precio = Double .parseDouble (partes [2 ]);
238
+ ventaTotal += cantidad * precio ;
239
+ }
240
+ br .close ();
241
+ System .out .printf ("Venta total: %.2f\n " , ventaTotal );
242
+ } catch (IOException e ) {
243
+ System .out .println ("Error al calcular venta total: " + e .getMessage ());
244
+ }
245
+ }
246
+
247
+ // Método para calcular la venta por producto
248
+ private static void calcularVentaPorProducto (Scanner scanner ) {
249
+ try {
250
+ System .out .print ("Nombre del producto: " );
251
+ String nombreProducto = scanner .nextLine ();
252
+
253
+ BufferedReader br = new BufferedReader (new FileReader (SALES_FILENAME ));
254
+ String linea ;
255
+ boolean encontrado = false ;
256
+ while ((linea = br .readLine ()) != null ) {
257
+ String [] partes = linea .split (", " );
258
+ if (partes [0 ].equals (nombreProducto )) {
259
+ int cantidad = Integer .parseInt (partes [1 ]);
260
+ double precio = Double .parseDouble (partes [2 ]);
261
+ double ventaProducto = cantidad * precio ;
262
+ System .out .printf ("Venta de %s: %.2f\n " , nombreProducto , ventaProducto );
263
+ encontrado = true ;
264
+ break ;
265
+ }
266
+ }
267
+ br .close ();
268
+
269
+ if (!encontrado ) {
270
+ System .out .println ("Producto no encontrado." );
271
+ }
272
+ } catch (IOException e ) {
273
+ System .out .println ("Error al calcular venta por producto: " + e .getMessage ());
274
+ }
275
+ }
276
+
277
+ // Método para borrar el archivo de ventas al salir
278
+ private static void borrarArchivoVentas () {
279
+ File file = new File (SALES_FILENAME );
280
+ if (file .delete ()) {
281
+ System .out .println ("Archivo de ventas borrado con éxito." );
282
+ } else {
283
+ System .out .println ("No se pudo borrar el archivo de ventas." );
284
+ }
285
+ }
286
+ }
0 commit comments