|
| 1 | +package MauroDevRetos; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | +import java.util.Scanner; |
| 6 | +import java.io.BufferedReader; |
| 7 | +import java.io.BufferedWriter; |
| 8 | +import java.io.File; |
| 9 | +import java.io.FileReader; |
| 10 | +import java.io.FileWriter; |
| 11 | +import java.io.IOException; |
| 12 | + |
| 13 | +public class reto_10 { |
| 14 | + public static List<productos> listaproductos = new ArrayList<>(); |
| 15 | + public static Scanner input = new Scanner(System.in); |
| 16 | + public static String fileName2 = "producto.txt"; |
| 17 | + |
| 18 | + public static void main(String[] args) { |
| 19 | + |
| 20 | + String githubUsername = "julian98789"; |
| 21 | + String fileName = githubUsername + ".txt"; |
| 22 | + |
| 23 | + String name = "julian"; |
| 24 | + int age = 19; |
| 25 | + String favoriteProgrammingLanguage = "Java"; |
| 26 | + |
| 27 | + // Crear el archivo y escribir en él |
| 28 | + try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))) { |
| 29 | + writer.write("Nombre: " + name + "\n"); |
| 30 | + writer.write("Edad: " + age + "\n"); |
| 31 | + writer.write("Lenguaje de programación favorito: " + favoriteProgrammingLanguage + "\n"); |
| 32 | + } catch (IOException e) { |
| 33 | + e.printStackTrace(); |
| 34 | + } |
| 35 | + |
| 36 | + // Leer e imprimir el contenido del archivo |
| 37 | + File file = new File(fileName); |
| 38 | + try (java.util.Scanner scanner = new java.util.Scanner(file)) { |
| 39 | + while (scanner.hasNextLine()) { |
| 40 | + System.out.println(scanner.nextLine()); |
| 41 | + } |
| 42 | + } catch (IOException e) { |
| 43 | + e.printStackTrace(); |
| 44 | + } |
| 45 | + |
| 46 | + // Borrar el archivo |
| 47 | + if (file.delete()) { |
| 48 | + System.out.println("El archivo " + fileName + " ha sido borrado exitosamente."); |
| 49 | + } else { |
| 50 | + System.out.println("Error al borrar el archivo " + fileName); |
| 51 | + } |
| 52 | + |
| 53 | + boolean entrar = true; |
| 54 | + while (entrar) { |
| 55 | + System.out.println("\n ******* productos *******\n"); |
| 56 | + System.out.println("1. agregar producto"); |
| 57 | + System.out.println("2. consultar producto"); |
| 58 | + System.out.println("3. actualizar producto"); |
| 59 | + System.out.println("4. eliminar producto"); |
| 60 | + System.out.println("5. calcular venta total "); |
| 61 | + System.out.println("6. salir \n"); |
| 62 | + |
| 63 | + int opcion = input.nextInt(); |
| 64 | + switch (opcion) { |
| 65 | + case 1: |
| 66 | + agregarProducto(); |
| 67 | + break; |
| 68 | + case 2: |
| 69 | + consultarProductos(); |
| 70 | + break; |
| 71 | + case 3: |
| 72 | + actualizarProducto(); |
| 73 | + |
| 74 | + break; |
| 75 | + case 4: |
| 76 | + eliminarProducto(); |
| 77 | + |
| 78 | + break; |
| 79 | + case 5: |
| 80 | + calcularVentaTotal(); |
| 81 | + |
| 82 | + break; |
| 83 | + case 6: |
| 84 | + File file2 = new File(fileName2); |
| 85 | + if (file2.delete()) { |
| 86 | + System.out.println("El archivo " + fileName2 + " ha sido borrado exitosamente."); |
| 87 | + } else { |
| 88 | + System.out.println("Error al borrar el archivo " + fileName2); |
| 89 | + } |
| 90 | + entrar = false; |
| 91 | + break; |
| 92 | + |
| 93 | + default: |
| 94 | + break; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | + private static void agregarProducto() { |
| 104 | + |
| 105 | + System.out.println("Cantidad vendida: "); |
| 106 | + int cantidad = input.nextInt(); |
| 107 | + input.nextLine(); |
| 108 | + |
| 109 | + System.out.println("Nombre del producto: "); |
| 110 | + String name = input.nextLine(); |
| 111 | + |
| 112 | + System.out.println("Precio: "); |
| 113 | + int precio = input.nextInt(); |
| 114 | + input.nextLine(); |
| 115 | + |
| 116 | + productos agrgarProductos = new productos(name, cantidad, precio); |
| 117 | + listaproductos.add(agrgarProductos); |
| 118 | + |
| 119 | + try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName2, true))) { |
| 120 | + writer.write("Nombre: " + name + "\n"); |
| 121 | + writer.write("Cantidad vendida: " + cantidad + "\n"); |
| 122 | + writer.write("Precio: " + precio + "\n"); |
| 123 | + writer.write("-------------------------\n"); |
| 124 | + } catch (IOException e) { |
| 125 | + e.printStackTrace(); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + private static void consultarProductos() { |
| 130 | + File file = new File(fileName2); |
| 131 | + try (java.util.Scanner scanner = new java.util.Scanner(file)) { |
| 132 | + while (scanner.hasNextLine()) { |
| 133 | + System.out.println(scanner.nextLine()); |
| 134 | + } |
| 135 | + } catch (IOException e) { |
| 136 | + e.printStackTrace(); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + private static void actualizarProducto() { |
| 141 | + System.out.println("Cantidad vendida: "); |
| 142 | + int cantidad = input.nextInt(); |
| 143 | + input.nextLine(); |
| 144 | + |
| 145 | + System.out.println("Nombre del producto: "); |
| 146 | + String name = input.nextLine(); |
| 147 | + |
| 148 | + System.out.println("Precio: "); |
| 149 | + int precio = input.nextInt(); |
| 150 | + input.nextLine(); |
| 151 | + |
| 152 | + List<String> contenidoActual = new ArrayList<>(); |
| 153 | + try (BufferedReader reader = new BufferedReader(new FileReader("fileName"))) { |
| 154 | + String linea; |
| 155 | + while ((linea = reader.readLine()) != null) { |
| 156 | + contenidoActual.add(linea); |
| 157 | + } |
| 158 | + } catch (IOException e) { |
| 159 | + e.printStackTrace(); |
| 160 | + } |
| 161 | + |
| 162 | + contenidoActual.add("Nombre: " + name); |
| 163 | + contenidoActual.add("Cantidad vendida: " + cantidad); |
| 164 | + contenidoActual.add("Precio: " + precio); |
| 165 | + contenidoActual.add("-------------------------"); |
| 166 | + |
| 167 | + // Escribir el contenido actualizado de nuevo en el archivo |
| 168 | + try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName2))) { |
| 169 | + for (String linea : contenidoActual) { |
| 170 | + writer.write(linea); |
| 171 | + writer.newLine(); |
| 172 | + } |
| 173 | + } catch (IOException e) { |
| 174 | + e.printStackTrace(); |
| 175 | + } |
| 176 | + } |
| 177 | + private static void eliminarProducto() { |
| 178 | + input.nextLine(); |
| 179 | + System.out.println("Nombre del producto a eliminar: "); |
| 180 | + String nombreAEliminar = input.nextLine(); |
| 181 | + |
| 182 | + List<String> contenidoActual = new ArrayList<>(); |
| 183 | + boolean encontrado = false; |
| 184 | + |
| 185 | + try (BufferedReader reader = new BufferedReader(new FileReader(fileName2))) { |
| 186 | + String linea; |
| 187 | + while ((linea = reader.readLine()) != null) { |
| 188 | + if (linea.startsWith("Nombre: " + nombreAEliminar)) { |
| 189 | + encontrado = true; |
| 190 | + reader.readLine(); |
| 191 | + reader.readLine(); |
| 192 | + reader.readLine(); |
| 193 | + } else { |
| 194 | + contenidoActual.add(linea); |
| 195 | + } |
| 196 | + } |
| 197 | + } catch (IOException e) { |
| 198 | + e.printStackTrace(); |
| 199 | + } |
| 200 | + |
| 201 | + if (encontrado) { |
| 202 | + try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName2))) { |
| 203 | + for (String linea : contenidoActual) { |
| 204 | + writer.write(linea); |
| 205 | + writer.newLine(); |
| 206 | + } |
| 207 | + } catch (IOException e) { |
| 208 | + e.printStackTrace(); |
| 209 | + } |
| 210 | + System.out.println("El producto '" + nombreAEliminar + "' ha sido eliminado."); |
| 211 | + } else { |
| 212 | + System.out.println("El producto '" + nombreAEliminar + "' no fue encontrado."); |
| 213 | + } |
| 214 | + } |
| 215 | + private static void calcularVentaTotal() { |
| 216 | + int ventaTotal = 0; |
| 217 | + int vetaPorProducto = 0; |
| 218 | + |
| 219 | + try (BufferedReader reader = new BufferedReader(new FileReader(fileName2))) { |
| 220 | + String linea; |
| 221 | + while ((linea = reader.readLine()) != null) { |
| 222 | + if (linea.startsWith("Cantidad vendida: ")) { |
| 223 | + int cantidad = Integer.parseInt(linea.split(": ")[1]); |
| 224 | + linea = reader.readLine(); // Leer la línea del precio |
| 225 | + int precio = Integer.parseInt(linea.split(": ")[1]); |
| 226 | + ventaTotal += cantidad * precio; |
| 227 | + vetaPorProducto = cantidad * precio;; |
| 228 | + String name = (linea.split(": ")[1]); |
| 229 | + |
| 230 | + System.out.println("veta total potr ptoducto: " + name +": " + vetaPorProducto); |
| 231 | + } |
| 232 | + } |
| 233 | + } catch (IOException e) { |
| 234 | + e.printStackTrace(); |
| 235 | + } |
| 236 | + |
| 237 | + System.out.println("La venta total es: " + ventaTotal); |
| 238 | + } |
| 239 | + } |
| 240 | + |
| 241 | + |
| 242 | +class productos { |
| 243 | + private String name; |
| 244 | + private int cantidad; |
| 245 | + private int precio; |
| 246 | + |
| 247 | + public productos(String name, int cantidad, int precio) { |
| 248 | + this.name = name; |
| 249 | + this.cantidad = cantidad; |
| 250 | + this.precio = precio; |
| 251 | + } |
| 252 | +} |
0 commit comments