Skip to content

Commit 6058690

Browse files
authored
Merge pull request mouredev#6988 from Josegs95/main
#11 y #12 - Java
2 parents 1bf4c6f + e7c3b53 commit 6058690

File tree

2 files changed

+483
-0
lines changed

2 files changed

+483
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
import java.io.*;
2+
import java.nio.charset.StandardCharsets;
3+
import java.nio.file.Files;
4+
import java.nio.file.StandardOpenOption;
5+
import java.util.List;
6+
import java.util.Scanner;
7+
8+
public class Josegs95 {
9+
public static void main(String[] args) {
10+
//Ficheros
11+
File file = new File("Josegs95.txt");
12+
try(FileWriter out = new FileWriter(file); Scanner in = new Scanner(file)){
13+
//FileWriter crea ya un archivo al crearse el objeto
14+
if (file.createNewFile())
15+
System.out.println("Archivo creado: " + file.getName());
16+
else
17+
System.out.println("No se ha creado el archivo " + file.getName() + " porque ya existía");
18+
19+
20+
out.write("Nombre: Josegs95" + System.lineSeparator());
21+
out.write("Edad: 29" + System.lineSeparator());
22+
out.write("Lenguaje: Java" + System.lineSeparator());
23+
out.flush();
24+
25+
while (in.hasNext()){
26+
System.out.println(in.nextLine());
27+
}
28+
29+
} catch (IOException e) {
30+
throw new RuntimeException(e);
31+
} finally {
32+
if (file.delete())
33+
System.out.println("Se ha borrado el archivo: " + file.getName());
34+
else
35+
System.out.println("No se ha podido borrar el archivo: " + file.getName());
36+
}
37+
38+
//Reto
39+
System.out.println("\n");
40+
retoFinal();
41+
}
42+
43+
static File file;
44+
public static void retoFinal(){
45+
file = new File("Ventas.txt");
46+
try(Scanner sc = new Scanner(System.in);
47+
FileWriter out = new FileWriter(file, true)){
48+
49+
loop: while(true){
50+
showMenu();
51+
System.out.print("Seleccione una opción: ");
52+
String option = sc.nextLine();
53+
switch (option){
54+
case "1": //Añadir
55+
addProduct(out, askProductDetails(sc));
56+
break;
57+
case "2": //Consultar
58+
showProduct(sc);
59+
break;
60+
case "3": //Actualizar
61+
updateProduct(askProductDetails(sc));
62+
break;
63+
case "4": //Eliminar
64+
deleteProduct(sc);
65+
break;
66+
case "5": //Calculo venta producto
67+
calculateProductSales(sc);
68+
break;
69+
case "6": //Calculo venta total
70+
calculateTotalSales();
71+
break;
72+
case "7": //Salir
73+
break loop;
74+
default:
75+
System.out.println("Error. Debe elegir una opción válida (0-7).");
76+
break;
77+
}
78+
}
79+
} catch (IOException e) {
80+
throw new RuntimeException(e);
81+
} finally {
82+
deleteFile();
83+
}
84+
85+
}
86+
87+
private static void showMenu(){
88+
System.out.println("------------------");
89+
System.out.println("1- Añadir producto");
90+
System.out.println("2- Consultar producto");
91+
System.out.println("3- Actualizar producto");
92+
System.out.println("4- Eliminar producto");
93+
System.out.println("5- Calcular venta producto");
94+
System.out.println("6- Calcular venta total");
95+
System.out.println("7- Salir");
96+
System.out.println("------------------");
97+
}
98+
99+
private static String[] askProductDetails(Scanner userInput){
100+
System.out.print("Introduzca el nombre del producto: ");
101+
String product = userInput.nextLine().toLowerCase();
102+
System.out.print("Introduzca la cantidad del producto: ");
103+
String quantity = userInput.nextLine().toLowerCase(); //Habría que controlar que es un número
104+
System.out.print("Introduzca el precio del producto: ");
105+
String price = userInput.nextLine().toLowerCase(); //Habría que controlar que es un número
106+
107+
return new String[]{product, quantity, price};
108+
}
109+
110+
private static void addProduct(FileWriter outFile, String[] product) throws IOException {
111+
//Habría que controlar que el producto no existiera ya en el archivo
112+
String productLine = product[0] + ", " + product[1] + ", " + product[2] + System.lineSeparator();
113+
114+
outFile.write(productLine);
115+
outFile.flush();
116+
}
117+
118+
private static void showProduct(Scanner userInput) throws IOException {
119+
System.out.print("Introduzca el nombre del producto: ");
120+
String product = userInput.nextLine().toLowerCase();
121+
122+
String[] productDetails;
123+
124+
List<String> lines = Files.readAllLines(file.toPath());
125+
for (String line : lines){
126+
productDetails = line.split(", ");
127+
if (productDetails[0].equals(product)){
128+
System.out.println("Producto: " + productDetails[0] + ", Cantidad: " + productDetails[1] + ", Precio: " + productDetails[2]);
129+
return;
130+
}
131+
}
132+
133+
System.out.println("No está registrado el producto: " + product);
134+
}
135+
136+
private static void updateProduct(String[] product) throws IOException {
137+
String productLine = product[0] + ", " + product[1] + ", " + product[2];
138+
139+
List<String> lines = Files.readAllLines(file.toPath());
140+
String[] productDetails;
141+
boolean isUpdated = false;
142+
for (int i = 0; i < lines.size(); i++){
143+
productDetails = lines.get(i).split(", ");
144+
if (productDetails[0].equals(product[0])){
145+
lines.set(i, productLine);
146+
isUpdated = true;
147+
break;
148+
}
149+
}
150+
151+
if (isUpdated)
152+
System.out.println("Se ha actualizado el producto: " + product[0]);
153+
else
154+
System.out.println("El producto '" + product[0] + "' no está registrado.");
155+
156+
Files.write(file.toPath(), lines, StandardCharsets.UTF_8);
157+
}
158+
159+
private static void deleteProduct(Scanner userInput) throws IOException {
160+
System.out.print("Introduzca el nombre del producto: ");
161+
String product = userInput.nextLine().toLowerCase();
162+
163+
String[] productDetails;
164+
boolean isRemoved = false;
165+
166+
List<String> lines = Files.readAllLines(file.toPath());
167+
for (int i = 0; i < lines.size(); i++){
168+
productDetails = lines.get(i).split(", ");
169+
if (productDetails[0].equals(product)){
170+
lines.remove(i);
171+
isRemoved = true;
172+
break;
173+
}
174+
}
175+
176+
if (isRemoved)
177+
System.out.println("Se ha borrado el producto: " + product);
178+
else
179+
System.out.println("El producto " + product + " no está registrado.");
180+
181+
Files.write(file.toPath(), lines, StandardCharsets.UTF_8);
182+
}
183+
184+
private static void calculateProductSales(Scanner userInput) throws IOException{
185+
System.out.print("Introduzca el nombre del producto: ");
186+
String product = userInput.nextLine().toLowerCase();
187+
188+
List<String> lines = Files.readAllLines(file.toPath());
189+
String[] productDetails;
190+
Double total = 0.0;
191+
for (String line : lines){
192+
productDetails = line.split(", ");
193+
if (productDetails[0].equals(product)){
194+
Integer quantity = Integer.parseInt(productDetails[1]);
195+
Double price = Double.parseDouble(productDetails[2]);
196+
total = quantity * price;
197+
break;
198+
}
199+
}
200+
201+
if (total != 0)
202+
System.out.printf("Venta del producto %s de %.2f€.%n", product, total);
203+
else
204+
System.out.println("El producto " + product + " no está registrado.");
205+
}
206+
207+
private static void calculateTotalSales() throws IOException {
208+
List<String> lines = Files.readAllLines(file.toPath());
209+
String[] productDetails;
210+
Double total = 0.0;
211+
for (String line : lines){
212+
productDetails = line.split(", ");
213+
Integer quantity = Integer.parseInt(productDetails[1]);
214+
Double price = Double.parseDouble(productDetails[2]);
215+
total += quantity * price;
216+
}
217+
218+
System.out.printf("Venta total de %.2f€.%n", total);
219+
}
220+
221+
private static void deleteFile(){
222+
if (file.delete())
223+
System.out.println("Se ha borrado el archivo: " + file.getName());
224+
else
225+
System.out.println("No se ha podido borrar el archivo: " + file.getName());
226+
}
227+
}

0 commit comments

Comments
 (0)