|
| 1 | +import java.io.*; |
| 2 | +import java.util.LinkedList; |
| 3 | +import java.util.List; |
| 4 | +import java.util.Objects; |
| 5 | +import java.util.Scanner; |
| 6 | + |
| 7 | +public class Main { |
| 8 | + |
| 9 | + public static void main(String[] args) throws IOException { |
| 10 | + File file = new File("asjordi.txt"); |
| 11 | + String contenido = "Jordi Ayala\n20\nJava\n"; |
| 12 | + |
| 13 | + escribirArchivo(file, contenido, false); |
| 14 | + |
| 15 | + System.out.println("Contenido del archivo:\n" + leerArchivo(file)); |
| 16 | + |
| 17 | + boolean isDelete = file.delete(); |
| 18 | + System.out.println(isDelete ? "El archivo fue eliminado" : "El archivo no fue eliminado"); |
| 19 | + |
| 20 | + ventas(); |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * DIFICULTAD EXTRA (opcional): |
| 25 | + * Desarrolla un programa de gestión de ventas que almacena sus datos en un archivo .txt. |
| 26 | + * - Cada producto se guarda en una línea del arhivo de la siguiente manera: [nombre_producto], [cantidad_vendida], [precio]. |
| 27 | + * - Siguiendo ese formato, y mediante terminal, debe permitir añadir, consultar, |
| 28 | + * actualizar, eliminar productos y salir. |
| 29 | + * - También debe poseer opciones para calcular la venta total y por producto. |
| 30 | + * - La opción salir borra el .txt. |
| 31 | + */ |
| 32 | + |
| 33 | + static class Venta { |
| 34 | + String nombre; |
| 35 | + String cantidad; |
| 36 | + String precio; |
| 37 | + |
| 38 | + public String getNombre() { |
| 39 | + return nombre; |
| 40 | + } |
| 41 | + |
| 42 | + public void setNombre(String nombre) { |
| 43 | + this.nombre = nombre; |
| 44 | + } |
| 45 | + |
| 46 | + public String getCantidad() { |
| 47 | + return cantidad; |
| 48 | + } |
| 49 | + |
| 50 | + public void setCantidad(String cantidad) { |
| 51 | + this.cantidad = cantidad; |
| 52 | + } |
| 53 | + |
| 54 | + public String getPrecio() { |
| 55 | + return precio; |
| 56 | + } |
| 57 | + |
| 58 | + public void setPrecio(String precio) { |
| 59 | + this.precio = precio; |
| 60 | + } |
| 61 | + |
| 62 | + public String getInfo() { |
| 63 | + return nombre + ", " + cantidad + ", " + precio + "\n"; |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public String toString() { |
| 68 | + return "Venta{" + |
| 69 | + "nombre='" + nombre + '\'' + |
| 70 | + ", cantidad='" + cantidad + '\'' + |
| 71 | + ", precio='" + precio + '\'' + |
| 72 | + '}'; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + static void ventas() { |
| 77 | + File file = new File("ventas.txt"); |
| 78 | + Scanner sc = new Scanner(System.in); |
| 79 | + String opcion = ""; |
| 80 | + |
| 81 | + while (!opcion.equals("6")) { |
| 82 | + mostrarOpciones(); |
| 83 | + opcion = sc.nextLine(); |
| 84 | + |
| 85 | + switch (opcion) { |
| 86 | + case "1": |
| 87 | + Venta v = new Venta(); |
| 88 | + System.out.print("Ingrese el nombre del producto: "); |
| 89 | + v.setNombre(sc.nextLine()); |
| 90 | + System.out.print("Ingrese la cantidad: "); |
| 91 | + v.setCantidad(sc.nextLine()); |
| 92 | + System.out.print("Ingrese el precio: "); |
| 93 | + v.setPrecio(sc.nextLine()); |
| 94 | + escribirArchivo(file, v.getInfo(), true); |
| 95 | + break; |
| 96 | + case "2": |
| 97 | + System.out.println("Ventas registradas:"); |
| 98 | + System.out.println(obtenerVentas(file)); |
| 99 | + break; |
| 100 | + case "3": |
| 101 | + System.out.println("Actualizar venta"); |
| 102 | + Venta venta = new Venta(); |
| 103 | + System.out.print("Ingrese el nombre del producto a actualizar: "); |
| 104 | + venta.setNombre(sc.nextLine()); |
| 105 | + System.out.print("Ingrese la cantidad: "); |
| 106 | + venta.setCantidad(sc.nextLine()); |
| 107 | + System.out.print("Ingrese el precio: "); |
| 108 | + venta.setPrecio(sc.nextLine()); |
| 109 | + actualizarVenta(file, venta); |
| 110 | + break; |
| 111 | + case "4": |
| 112 | + System.out.println("Eliminar venta"); |
| 113 | + System.out.print("Ingrese el nombre del producto a eliminar: "); |
| 114 | + eliminarVenta(file, sc.nextLine()); |
| 115 | + break; |
| 116 | + case "5": |
| 117 | + calcularTotal(file); |
| 118 | + break; |
| 119 | + case "6": |
| 120 | + System.out.println("Saliendo del sistema de ventas..."); |
| 121 | + file.delete(); |
| 122 | + break; |
| 123 | + default: |
| 124 | + System.out.println("Opción no válida"); |
| 125 | + break; |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + static void calcularTotal(File file) { |
| 131 | + List<Venta> ventas = obtenerVentas(file); |
| 132 | + double total = 0; |
| 133 | + |
| 134 | + for (Venta v : ventas) { |
| 135 | + total += Double.parseDouble(v.getCantidad()) * Double.parseDouble(v.getPrecio()); |
| 136 | + } |
| 137 | + |
| 138 | + System.out.println("El total de ventas es: " + total); |
| 139 | + } |
| 140 | + |
| 141 | + static void eliminarVenta(File file, String nombre) { |
| 142 | + List<Venta> ventas = obtenerVentas(file); |
| 143 | + |
| 144 | + for (Venta v : ventas) { |
| 145 | + if (v.getNombre().equals(nombre)) { |
| 146 | + ventas.remove(v); |
| 147 | + System.out.println("Venta eliminada"); |
| 148 | + break; |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + file.delete(); |
| 153 | + ventas.forEach(v -> escribirArchivo(file, v.getInfo(), true)); |
| 154 | + } |
| 155 | + |
| 156 | + static void actualizarVenta(File file, Venta venta) { |
| 157 | + List<Venta> ventas = obtenerVentas(file); |
| 158 | + |
| 159 | + for (Venta v : ventas) { |
| 160 | + if (v.getNombre().equals(venta.getNombre())) { |
| 161 | + v.setNombre(venta.getNombre()); |
| 162 | + v.setCantidad(venta.getCantidad()); |
| 163 | + v.setPrecio(venta.getPrecio()); |
| 164 | + System.out.println("Venta actualizada"); |
| 165 | + break; |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + file.delete(); |
| 170 | + ventas.forEach(v -> escribirArchivo(file, v.getInfo(), true)); |
| 171 | + } |
| 172 | + |
| 173 | + static void mostrarOpciones() { |
| 174 | + System.out.println("Bienvenido al sistema de ventas"); |
| 175 | + System.out.println("Favor de seleccionar una opción:"); |
| 176 | + System.out.println("1- Agregar venta"); |
| 177 | + System.out.println("2- Mostrar ventas"); |
| 178 | + System.out.println("3- Actualizar venta"); |
| 179 | + System.out.println("4- Eliminar venta"); |
| 180 | + System.out.println("5- Calcular total de ventas"); |
| 181 | + System.out.println("6- Salir"); |
| 182 | + } |
| 183 | + |
| 184 | + static void escribirArchivo(File file, String content, boolean append) { |
| 185 | + if (!file.exists()) { |
| 186 | + try { |
| 187 | + file.createNewFile(); |
| 188 | + } catch (IOException e) { |
| 189 | + e.printStackTrace(); |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + try (Writer w = new FileWriter(file, append); |
| 194 | + BufferedWriter bw = new BufferedWriter(w)) { |
| 195 | + bw.write(content); |
| 196 | + bw.flush(); |
| 197 | + } catch (IOException e) { |
| 198 | + e.printStackTrace(); |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + static String leerArchivo(File file) { |
| 203 | + StringBuilder sb = new StringBuilder(); |
| 204 | + |
| 205 | + try (Reader r = new FileReader(file); |
| 206 | + BufferedReader br = new BufferedReader(r)) { |
| 207 | + String linea; |
| 208 | + while ((linea = br.readLine()) != null) { |
| 209 | + sb.append(linea).append("\n"); |
| 210 | + } |
| 211 | + } catch (Exception e) { |
| 212 | + e.printStackTrace(); |
| 213 | + } |
| 214 | + |
| 215 | + return sb.toString(); |
| 216 | + } |
| 217 | + |
| 218 | + static List<Venta> obtenerVentas(File file) { |
| 219 | + List<Venta> list = new LinkedList<>(); |
| 220 | + |
| 221 | + try (Reader r = new FileReader(file); |
| 222 | + BufferedReader br = new BufferedReader(r)) { |
| 223 | + String linea; |
| 224 | + while ((linea = br.readLine()) != null) { |
| 225 | + String[] partes = linea.split(","); |
| 226 | + Venta v = new Venta(); |
| 227 | + v.setNombre(partes[0].trim()); |
| 228 | + v.setCantidad(partes[1].trim()); |
| 229 | + v.setPrecio(partes[2].trim()); |
| 230 | + list.add(v); |
| 231 | + } |
| 232 | + } catch (Exception e) { |
| 233 | + e.printStackTrace(); |
| 234 | + } |
| 235 | + |
| 236 | + return list; |
| 237 | + } |
| 238 | + |
| 239 | +} |
0 commit comments