|
| 1 | +import java.io.FileWriter; |
| 2 | +import java.io.FileReader; |
| 3 | +import java.io.File; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.BufferedReader; |
| 6 | +import java.io.BufferedWriter; |
| 7 | +import java.util.Scanner; |
| 8 | +import java.util.List; |
| 9 | +import java.util.ArrayList; |
| 10 | + |
| 11 | +public class simonguzman { |
| 12 | + |
| 13 | + public static void main(String[] args) { |
| 14 | + testFile(); |
| 15 | + salesManager(); |
| 16 | + } |
| 17 | + |
| 18 | + |
| 19 | + static void salesManager(){ |
| 20 | + Scanner sc = new Scanner(System.in); |
| 21 | + int opcion; |
| 22 | + do{ |
| 23 | + menu(); |
| 24 | + System.out.println("Ingrese una opcion:"); |
| 25 | + opcion = sc.nextInt(); |
| 26 | + sc.nextLine(); |
| 27 | + opcionsMenu(opcion, sc); |
| 28 | + }while(opcion != 5); |
| 29 | + } |
| 30 | + |
| 31 | + static void menu(){ |
| 32 | + System.out.println("**************** GESTION DE VENTAS ****************"); |
| 33 | + System.out.println("1. Añadir productos"); |
| 34 | + System.out.println("2. Consultar productos"); |
| 35 | + System.out.println("3. Actualizar productos"); |
| 36 | + System.out.println("4. Eliminar productos"); |
| 37 | + System.out.println("5. Salir"); |
| 38 | + } |
| 39 | + |
| 40 | + static void opcionsMenu(int opcion, Scanner sc){ |
| 41 | + switch (opcion) { |
| 42 | + case 1: |
| 43 | + addProducts(sc); |
| 44 | + break; |
| 45 | + case 2: |
| 46 | + consultProducts(); |
| 47 | + break; |
| 48 | + case 3: |
| 49 | + updateProducts(sc); |
| 50 | + break; |
| 51 | + case 4: |
| 52 | + deleteProducts(sc); |
| 53 | + break; |
| 54 | + case 5: |
| 55 | + exit(); |
| 56 | + break; |
| 57 | + default: |
| 58 | + System.out.println("ERROR: Opcion no valida..."); |
| 59 | + break; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + static void addProducts(Scanner sc){ |
| 64 | + try (BufferedWriter writer = new BufferedWriter(new FileWriter("ventas.txt", true))){ |
| 65 | + System.out.println("Ingrese el nombre del producto: "); |
| 66 | + String nameProduct = sc.next(); |
| 67 | + System.out.println("Ingrese la cantidad vendida: "); |
| 68 | + int cantProduct = sc.nextInt(); |
| 69 | + System.out.println("Ingrese el precio del producto: "); |
| 70 | + double priceProduct = sc.nextDouble(); |
| 71 | + sc.nextLine(); |
| 72 | + writer.write(nameProduct + " , "+cantProduct+" , "+priceProduct); |
| 73 | + writer.newLine(); |
| 74 | + System.out.println("Producto añadido correctamente"); |
| 75 | + } catch (IOException e) { |
| 76 | + System.out.println("ERROR: No se ha podido añadir el producto"+e.getMessage()); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + static void consultProducts(){ |
| 81 | + try (BufferedReader reader = new BufferedReader(new FileReader("ventas.txt"))){ |
| 82 | + String line; |
| 83 | + while ((line = reader.readLine()) != null) { |
| 84 | + System.out.println(line); |
| 85 | + } |
| 86 | + } catch (IOException e) { |
| 87 | + System.out.println("ERROR: No se pudo consultar los productos..."+e.getMessage()); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + static void updateProducts(Scanner sc){ |
| 92 | + List<String> products = new ArrayList<>(); |
| 93 | + String line; |
| 94 | + boolean productFound = false; |
| 95 | + |
| 96 | + try (BufferedReader reader = new BufferedReader(new FileReader("ventas.txt"))){ |
| 97 | + while((line = reader.readLine()) != null){ |
| 98 | + products.add(line.trim()); |
| 99 | + } |
| 100 | + } catch (IOException e) { |
| 101 | + System.out.println("ERROR: No se pudo leer el archivo..."+e.getMessage()); |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + System.out.println("Ingrese el producto a actualizar: "); |
| 106 | + String productName = sc.next().trim(); |
| 107 | + |
| 108 | + for(int i = 0; i < products.size(); i++){ |
| 109 | + String[] details = products.get(i).split(", "); |
| 110 | + if(details[0].trim().equalsIgnoreCase(productName)){ |
| 111 | + System.out.println("Ingrese la nueva cantidad vendida; "); |
| 112 | + int productCant = sc.nextInt(); |
| 113 | + System.out.println("Ingrese el nuevo precio del producto: "); |
| 114 | + double productPrice = sc.nextDouble(); |
| 115 | + sc.nextLine(); |
| 116 | + |
| 117 | + products.set(i, productName + " , "+productCant+" , "+productPrice); |
| 118 | + productFound = true; |
| 119 | + break; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + if(productFound){ |
| 124 | + try (BufferedWriter writer = new BufferedWriter(new FileWriter("ventas.txt"))){ |
| 125 | + for(String product : products){ |
| 126 | + writer.write(product); |
| 127 | + writer.newLine(); |
| 128 | + } |
| 129 | + System.out.println("Producto actualizado correctamente..."); |
| 130 | + } catch (IOException e) { |
| 131 | + System.out.println("ERROR: No se pudo actualizar el producto..."+e.getMessage()); |
| 132 | + } |
| 133 | + }else{ |
| 134 | + System.out.println("Producto no encontrado."); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + static void deleteProducts(Scanner sc){ |
| 139 | + List<String> products = new ArrayList<>(); |
| 140 | + String line; |
| 141 | + boolean productFound = false; |
| 142 | + |
| 143 | + try (BufferedReader reader = new BufferedReader(new FileReader("ventas.txt"))){ |
| 144 | + while ((line = reader.readLine()) != null) { |
| 145 | + products.add(line.trim()); |
| 146 | + } |
| 147 | + } catch (Exception e) { |
| 148 | + System.out.println("ERROR: No se pudo leer el archivo..."+e.getMessage()); |
| 149 | + return; |
| 150 | + } |
| 151 | + |
| 152 | + System.out.println("Ingrese el producto a eliminar: "); |
| 153 | + String productName = sc.next().trim(); |
| 154 | + |
| 155 | + for (int i = 0; i < products.size(); i++){ |
| 156 | + String[] details = products.get(i).split(", "); |
| 157 | + if(details[0].trim().equalsIgnoreCase(productName)){ |
| 158 | + products.remove(i); |
| 159 | + productFound = true; |
| 160 | + break; |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + if (productFound){ |
| 165 | + try (BufferedWriter writer = new BufferedWriter(new FileWriter("ventas.txt"))){ |
| 166 | + for(String product : products){ |
| 167 | + writer.write(product); |
| 168 | + writer.newLine(); |
| 169 | + } |
| 170 | + System.out.println("Producto eliminado correctamente."); |
| 171 | + } catch (IOException e) { |
| 172 | + System.out.println("ERROR: El producto no pudo ser eliminado..."+e.getMessage()); |
| 173 | + } |
| 174 | + }else{ |
| 175 | + System.out.println("No se encontro el producto"); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + static void exit(){ |
| 180 | + File file = new File("ventas.txt"); |
| 181 | + if(file.delete()){ |
| 182 | + System.out.println("Fichero eliminado."); |
| 183 | + }else{ |
| 184 | + System.out.println("ERROR: No se pudo eliminar el archivo."); |
| 185 | + } |
| 186 | + System.out.println("Saliendo del programa..."); |
| 187 | + } |
| 188 | + |
| 189 | + static void testFile(){ |
| 190 | + createAndWriteFile(); |
| 191 | + readFile(); |
| 192 | + deleteFile(); |
| 193 | + } |
| 194 | + |
| 195 | + //Crear y escribrir el fichero |
| 196 | + static void createAndWriteFile(){ |
| 197 | + try (FileWriter writer = new FileWriter("simonguzman.txt")){ |
| 198 | + writer.write("Nombre: Simon\n"); |
| 199 | + writer.write("Edad: 22\n"); |
| 200 | + writer.write("Lenguaje de programacion favorito: Java\n"); |
| 201 | + } catch (IOException e) { |
| 202 | + System.out.println("ERROR: No se puede crear o escribir el archivo...."+e.getMessage()); |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + //Leer el fichero |
| 207 | + static void readFile(){ |
| 208 | + try (BufferedReader reader = new BufferedReader(new FileReader("simonguzman.txt"))){ |
| 209 | + String line; |
| 210 | + while ((line = reader.readLine()) != null) { |
| 211 | + System.out.println(line); |
| 212 | + } |
| 213 | + } catch (IOException e) { |
| 214 | + System.out.println("ERROR: No se pudo leer el archivo..."+e.getMessage()); |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + static void deleteFile(){ |
| 219 | + java.io.File file = new java.io.File("simonguzman.txt"); |
| 220 | + if(file.delete()){ |
| 221 | + System.out.println("Archivo eliminado correctamente"); |
| 222 | + }else{ |
| 223 | + System.out.println("ERROR: No se puede eliminar el archivo..."); |
| 224 | + } |
| 225 | + } |
| 226 | +} |
0 commit comments