|
| 1 | +import java.io.IOException; |
| 2 | +import java.nio.file.Files; |
| 3 | +import java.nio.file.Path; |
| 4 | +import java.nio.file.StandardOpenOption; |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Scanner; |
| 8 | + |
| 9 | +public class danhingar { |
| 10 | + |
| 11 | + public static void main(String[] args) throws IOException { |
| 12 | + try { |
| 13 | + String content = """ |
| 14 | + Nombre: Daniel |
| 15 | + Edad: 27 |
| 16 | + Lenguaje: Java |
| 17 | + """; |
| 18 | + |
| 19 | + Path path = Path.of("danhingar.txt"); |
| 20 | + Files.writeString(path, content); |
| 21 | + |
| 22 | + Files.readAllLines(path).forEach(line -> System.out.println(line)); |
| 23 | + |
| 24 | + Files.delete(path); |
| 25 | + } catch (Exception e) { |
| 26 | + System.out.println("Se ha producido un error"); |
| 27 | + } |
| 28 | + |
| 29 | + salesManager(); |
| 30 | + } |
| 31 | + |
| 32 | + List<Product> products = new ArrayList<>(); |
| 33 | + |
| 34 | + // Extra |
| 35 | + private static void salesManager() throws IOException { |
| 36 | + Path path = Path.of("stockManager1.txt"); |
| 37 | + Files.writeString(path, ""); |
| 38 | + while (Boolean.TRUE) { |
| 39 | + Scanner sc = new Scanner(System.in); |
| 40 | + System.out.println("1. Añadir producto"); |
| 41 | + System.out.println("2. Consultar producto"); |
| 42 | + System.out.println("3. Actualizar producto"); |
| 43 | + System.out.println("4. Eliminar producto"); |
| 44 | + System.out.println("5. Mostrar productos"); |
| 45 | + System.out.println("6. Calcular venta total"); |
| 46 | + System.out.println("7. Calcular venta total por producto"); |
| 47 | + System.out.println("8. Salir"); |
| 48 | + System.out.print("Selecciona una opción: "); |
| 49 | + String option = sc.nextLine(); |
| 50 | + switch (option) { |
| 51 | + case "1": |
| 52 | + System.out.print("Nombre: "); |
| 53 | + String name = sc.nextLine(); |
| 54 | + System.out.print("Cantidad: "); |
| 55 | + String quantity = sc.nextLine(); |
| 56 | + System.out.print("Precio: "); |
| 57 | + String price = sc.nextLine(); |
| 58 | + String product = String.format("%s, %s, %s \n", name, quantity, price); |
| 59 | + Files.writeString(path, product, StandardOpenOption.APPEND); |
| 60 | + break; |
| 61 | + case "2": |
| 62 | + System.out.print("Buscar el producto: "); |
| 63 | + String searchName = sc.nextLine(); |
| 64 | + String[] result = Files.readAllLines(path).stream().map(r -> r.trim().split(",")) |
| 65 | + .filter(array -> array[0].equalsIgnoreCase(searchName)).findFirst().orElse(null); |
| 66 | + System.out.println(result != null ? String.format("%s,%s,%s", result[0], result[1], result[2]) |
| 67 | + : "No existe el producto " + searchName); |
| 68 | + break; |
| 69 | + case "3": |
| 70 | + System.out.print("Nombre: "); |
| 71 | + String name1 = sc.nextLine(); |
| 72 | + System.out.print("Cantidad: "); |
| 73 | + String quantity1 = sc.nextLine(); |
| 74 | + System.out.print("Precio: "); |
| 75 | + String price1 = sc.nextLine(); |
| 76 | + String content = ""; |
| 77 | + for (String line : Files.readAllLines(path)) { |
| 78 | + if (line.trim().split(",")[0].equalsIgnoreCase(name1)) { |
| 79 | + content = content.concat(String.format("%s, %s, %s \n", name1, quantity1, price1)); |
| 80 | + } else { |
| 81 | + content = content.concat(line + "\n"); |
| 82 | + } |
| 83 | + } |
| 84 | + Files.writeString(path, content); |
| 85 | + break; |
| 86 | + case "4": |
| 87 | + System.out.print("Eliminar el producto: "); |
| 88 | + String deleteName = sc.nextLine(); |
| 89 | + String lines = Files.readAllLines(path).stream().map(r -> r.trim().split(",")) |
| 90 | + .filter(array -> !array[0].equalsIgnoreCase(deleteName)) |
| 91 | + .map(x -> String.format("%s, %s, %s \n", x[0], x[1], x[2])).reduce(String::concat) |
| 92 | + .orElse(""); |
| 93 | + Files.writeString(path, lines); |
| 94 | + break; |
| 95 | + case "5": |
| 96 | + Files.readAllLines(path).forEach(line -> System.out.println(line)); |
| 97 | + break; |
| 98 | + case "6": |
| 99 | + Double totalSales = Files.readAllLines(path).stream().map(l -> l.trim().split(",")) |
| 100 | + .map(h -> Double.parseDouble(h[1]) * Double.parseDouble(h[2])) |
| 101 | + .mapToDouble(Double::doubleValue).sum(); |
| 102 | + System.out.println("Ventas totales: " + totalSales); |
| 103 | + break; |
| 104 | + case "7": |
| 105 | + System.out.print("Nombre el producto: "); |
| 106 | + String nameProduct = sc.nextLine(); |
| 107 | + Files.readAllLines(path).stream().map(l -> l.trim().split(",")) |
| 108 | + .filter(array -> array[0].equalsIgnoreCase(nameProduct)) |
| 109 | + .forEach(element-> System.out.println("Ventas totales de " +nameProduct+" : " + Double.parseDouble(element[1]) * Double.parseDouble(element[2]))); |
| 110 | + break; |
| 111 | + case "8": |
| 112 | + sc.close(); |
| 113 | + Files.delete(path); |
| 114 | + System.exit(0); |
| 115 | + break; |
| 116 | + |
| 117 | + default: |
| 118 | + System.out.println("Opción no válida"); |
| 119 | + break; |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments