Skip to content

Commit 9bf7276

Browse files
authored
Merge pull request #8943 from JimsimroDev/main
#18-Java
2 parents fdfc4f7 + 142573b commit 9bf7276

File tree

8 files changed

+1063
-0
lines changed

8 files changed

+1063
-0
lines changed
Lines changed: 267 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
1+
import java.io.BufferedReader;
2+
import java.io.BufferedWriter;
3+
import java.io.File;
4+
import java.io.FileReader;
5+
import java.io.FileWriter;
6+
import java.io.IOException;
7+
import java.io.PrintWriter;
8+
import java.util.Scanner;
9+
10+
public class JimsimroDev {
11+
12+
private final static String DIVLINE = "::::::::::::::::::::::::::::::::::::::::::::::::::::";
13+
14+
private static void menu() {
15+
String menu = """
16+
1 ~ Create product
17+
2 ~ Consult product
18+
3 ~ Update product
19+
4 ~ Delete product
20+
5 ~ List products
21+
6 ~ Calculate total sales
22+
7 ~ Calculate product sales
23+
8 ~ Exit
24+
""";
25+
System.out.println(menu);
26+
}
27+
28+
private static void actions() {
29+
Scanner in = new Scanner(System.in);
30+
int option = 0;
31+
while (option != 8) {
32+
menu();
33+
System.out.println("Choose an option: ");
34+
option = in.nextInt();
35+
in.nextLine();
36+
switch (option) {
37+
case 1:
38+
System.out.print("Name: ");
39+
String name = in.nextLine();
40+
System.out.print("Quantity: ");
41+
int quantity = in.nextInt();
42+
System.out.print("Price: ");
43+
int price = in.nextInt();
44+
try {
45+
createProduct(name, quantity, price);
46+
} catch (IOException e) {
47+
e.printStackTrace();
48+
}
49+
break;
50+
case 2:
51+
System.out.print("Name of the product to search: ");
52+
String nameToSearch = in.nextLine();
53+
consultProduct(nameToSearch);
54+
break;
55+
case 3:
56+
System.out.print("Name: ");
57+
String nameToUpdate = in.nextLine();
58+
System.out.print("Quantity: ");
59+
int quantityToUpdate = in.nextInt();
60+
System.out.print("Price: ");
61+
int priceToUpdate = in.nextInt();
62+
updateProduct(nameToUpdate, quantityToUpdate, priceToUpdate);
63+
break;
64+
case 4:
65+
System.out.print("Name of the product to delete: ");
66+
String nameToDelete = in.nextLine();
67+
deleteProduct(nameToDelete);
68+
break;
69+
case 5:
70+
listProducts();
71+
break;
72+
case 6:
73+
calculateTotalSales();
74+
break;
75+
case 7:
76+
System.out.print("Name of the product: ");
77+
String productName = in.nextLine();
78+
calculateProductSales(productName);
79+
break;
80+
case 8:
81+
deleteFile();
82+
break;
83+
}
84+
}
85+
in.close(); // Close the scanner to avoid resource leak
86+
}
87+
88+
private static void createProduct(String name, int quantity, int price) throws IOException {
89+
File file = getFileBasedOnOs();
90+
91+
try (PrintWriter printWriter = new PrintWriter(new FileWriter(file, true))) { // 'true' for appending to the file
92+
printWriter.print(name + ", ");
93+
printWriter.print(quantity + ", ");
94+
printWriter.println(price);
95+
} catch (IOException e) {
96+
e.printStackTrace();
97+
}
98+
System.out.println("Created successfully");
99+
}
100+
101+
private static void consultProduct(String name) {
102+
File file = getFileBasedOnOs();
103+
104+
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(file))) {
105+
String line;
106+
System.out.println(DIVLINE);
107+
while ((line = bufferedReader.readLine()) != null) {
108+
if (line.contains(name)) {
109+
System.out.println(line);
110+
break;
111+
}
112+
}
113+
System.out.println(DIVLINE);
114+
} catch (IOException e) {
115+
e.printStackTrace();
116+
}
117+
}
118+
119+
private static void updateProduct(String name, int quantity, int price) {
120+
File file = getFileBasedOnOs();
121+
File tempFile = new File(file.getAbsolutePath() + ".tmp");
122+
boolean productFound = false;
123+
124+
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
125+
BufferedWriter bw = new BufferedWriter(new FileWriter(tempFile))) {
126+
127+
String line;
128+
while ((line = bufferedReader.readLine()) != null) {
129+
if (!line.contains(name)) {
130+
bw.write(line);
131+
bw.newLine();
132+
} else {
133+
bw.write(name + ", ");
134+
bw.write(quantity + ", ");
135+
bw.write(price + "\n");
136+
System.out.println("Updated successfully");
137+
productFound = true;
138+
}
139+
}
140+
} catch (IOException e) {
141+
System.out.println(e.getClass().getName() + " Error processing the file " + e.getMessage());
142+
e.printStackTrace();
143+
}
144+
if (!productFound) {
145+
System.err.println("Product " + name + " does not exist in the file " + file.getName());
146+
tempFile.delete();
147+
return;
148+
}
149+
if (!file.delete()) {
150+
return;
151+
}
152+
if (!tempFile.renameTo(file)) {
153+
System.err.println(file.getName());
154+
}
155+
}
156+
157+
private static void deleteProduct(String name) {
158+
File file = getFileBasedOnOs();
159+
File tempFile = new File(file.getAbsolutePath() + ".tmp");
160+
161+
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
162+
BufferedWriter bw = new BufferedWriter(new FileWriter(tempFile))) {
163+
164+
String line;
165+
while ((line = bufferedReader.readLine()) != null) {
166+
if (!line.contains(name)) {
167+
bw.write(line);
168+
bw.newLine();
169+
}
170+
}
171+
} catch (IOException e) {
172+
System.out.println(e.getClass().getName() + " Error processing the file " + e.getMessage());
173+
e.printStackTrace();
174+
}
175+
176+
if (!file.delete()) {
177+
return;
178+
}
179+
if (!tempFile.renameTo(file)) {
180+
System.err.println(file.getName());
181+
}
182+
}
183+
184+
private static void listProducts() {
185+
File file = getFileBasedOnOs();
186+
187+
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(file))) {
188+
String line;
189+
System.out.println(DIVLINE);
190+
while ((line = bufferedReader.readLine()) != null) {
191+
System.out.println(line);
192+
}
193+
System.out.println(DIVLINE);
194+
} catch (IOException e) {
195+
e.printStackTrace();
196+
}
197+
}
198+
199+
private static void calculateTotalSales() {
200+
File file = getFileBasedOnOs();
201+
202+
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(file))) {
203+
String line;
204+
int totalSales = 0;
205+
System.out.println(DIVLINE);
206+
while ((line = bufferedReader.readLine()) != null) {
207+
String[] parts = line.split(", ");
208+
int quantity = Integer.parseInt(parts[1]);
209+
int price = Integer.parseInt(parts[2]);
210+
211+
totalSales += (quantity * price);
212+
}
213+
System.out.printf("Total sales: %d\n", totalSales);
214+
System.out.println(DIVLINE);
215+
} catch (IOException e) {
216+
e.printStackTrace();
217+
}
218+
}
219+
220+
private static void calculateProductSales(String name) {
221+
File file = getFileBasedOnOs();
222+
223+
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(file))) {
224+
String line;
225+
int totalSales = 0;
226+
System.out.println(DIVLINE);
227+
while ((line = bufferedReader.readLine()) != null) {
228+
if (line.contains(name)) {
229+
String[] parts = line.split(", ");
230+
int quantity = Integer.parseInt(parts[1]);
231+
int price = Integer.parseInt(parts[2]);
232+
totalSales += (quantity * price);
233+
break;
234+
}
235+
}
236+
System.out.printf("Total sales of %s = %d\n", name, totalSales);
237+
System.out.println(DIVLINE);
238+
} catch (IOException e) {
239+
e.printStackTrace();
240+
}
241+
}
242+
243+
private static void deleteFile() {
244+
File file = getFileBasedOnOs();
245+
file.delete();
246+
}
247+
248+
private static File getFileBasedOnOs() {
249+
String osName = System.getProperty("os.name").toLowerCase();
250+
File file;
251+
252+
if (osName.contains("win")) {
253+
file = new File("J:\\JimsimroDev.txt");
254+
} else if (osName.contains("nix") || osName.contains("nux")) {
255+
file = new File("/mnt/j/JimsimroDev.txt");
256+
} else if (osName.contains("mac")) {
257+
file = new File("/mnt/j/JimsimroDev.txt");
258+
} else {
259+
file = new File("/mnt/j/JimsimroDev.txt"); // Operating system not recognized
260+
}
261+
return file;
262+
}
263+
264+
public static void main(String[] args) throws IOException {
265+
actions();
266+
}
267+
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
Installar JUnit para las pruebas unitarias en
3+
https://github.com/junit-team/junit4/wiki/Download-and-Install
4+
Se puede usar el .jar para proyectos creados con ant
5+
Para proyectos creados con Maven se puede usar el plugin
6+
<dependency>
7+
<groupId>junit</groupId>
8+
<artifactId>junit</artifactId>
9+
<version>4.13</version>
10+
<scope>test</scope>
11+
</dependency>
12+
Agregando esto a las dependencias del proyecto en el pom.xml
13+
*/
14+
15+
package com.jimsimrodev;
16+
17+
import java.util.Arrays;
18+
import java.util.Collection;
19+
import org.junit.jupiter.api.Assertions;
20+
import org.junit.jupiter.api.Test;
21+
import org.junit.runner.RunWith;
22+
import org.junit.runners.Parameterized;
23+
import java.time.LocalDate;
24+
import static java.time.format.DateTimeFormatter.ofPattern;
25+
import static com.jimsimrodev.Suma.suma;
26+
import static org.junit.Assert.assertEquals;
27+
import static org.junit.Assert.assertFalse;
28+
import static org.junit.Assert.assertNotNull;
29+
30+
import static java.time.format.DateTimeFormatter.ofPattern;
31+
32+
import java.util.Arrays;
33+
import java.util.Collection;
34+
import java.time.LocalDate;
35+
36+
/**
37+
* Suma
38+
*/
39+
public class Suma {
40+
public static double suma(double a, double b) {
41+
return a + b;
42+
}
43+
44+
/**
45+
* @return
46+
*/
47+
public static Collection<String[]> data() {
48+
return Arrays.asList(
49+
new String[][] {
50+
{ "Name: ", "JimsimroDev" },
51+
{ "age: ", "28" },
52+
{ "birthDate: ", LocalDate.now().format(ofPattern("dd/MM/yyyy")) },
53+
{ "programinLenguages: ", "Java, Python, C++, C#" },
54+
});
55+
}
56+
57+
public static void print(double resultado) {
58+
System.out.printf("La suma es: %s%n", resultado);
59+
}
60+
61+
// Esta clase debe ir en el paquete de pruebas unitarias
62+
63+
@RunWith(Parameterized.class)
64+
public class SumaTest {
65+
private final String key;
66+
private final String value;
67+
68+
public SumaTest(String key, String value) {
69+
this.key = key;
70+
this.value = value;
71+
}
72+
73+
@Test
74+
public void sumaTest() {
75+
76+
// Para operaciones con decimales
77+
Assertions.assertEquals(6.0, suma(4.0, 2.0), 0.0001);
78+
assertEquals(-2.0, suma(5.0, -7.0), 0.0001);
79+
assertEquals(0.0, suma(0.0, 0.0), 0.0001);
80+
assertEquals(4.6, suma(2.5, 2.1), 0.0001);
81+
assertEquals(4.1, suma(2.1, 2.0), 0.0001);
82+
assertEquals(5.0, suma(2.5, 2.5), 0.0001);
83+
assertEquals(4.5, suma(2, 2.5), 0.0001);
84+
// assertEquals(5.0, suma("2.5", 2.5), 0.0001);//error
85+
}
86+
87+
// Pruebas parametrizables
88+
@Parameterized.Parameters
89+
public static Collection<String[]> data() {
90+
return Arrays.asList(
91+
new String[][] {
92+
{ "name: ", "jimsimrodev" },
93+
{ "age: ", "28" },
94+
{ "birthDate: ", LocalDate.now().format(ofPattern("dd/MM/yyyy")) },
95+
{ "programinlenguages: ", "java, python, c++, c#" },
96+
});
97+
}
98+
99+
@Test
100+
public void testfieldsexist() {
101+
assertNotNull("La clave no debe ser nula", key);
102+
assertFalse("La clave no debe estar vacía", key.trim().isEmpty());
103+
assertNotNull("El valor no debe ser nulo", value);
104+
assertFalse("El valor no debe estar vacío", value.trim().isEmpty());
105+
106+
}
107+
108+
}
109+
110+
public static void main(String[] args) {
111+
print(suma(4, 2));
112+
print(suma(-4.0, 2.0));
113+
114+
for (String[] data : data()) {
115+
for (String string : data) {
116+
System.out.print(string);
117+
}
118+
System.out.println(" ");
119+
}
120+
}
121+
}

0 commit comments

Comments
 (0)