Skip to content

Commit 3fc39f1

Browse files
committed
Ejemplo con srp completado
1 parent a7c6303 commit 3fc39f1

File tree

1 file changed

+64
-1
lines changed

1 file changed

+64
-1
lines changed

Roadmap/26 - SOLID SRP/java/simonguzman.java

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,72 @@
33

44
public class simonguzman {
55
public static void main(String[] args) {
6-
incorrectSrp();
6+
//incorrectSrp();
7+
correctSrp();
78
}
9+
/****************************** ejemplo con srp(Correcto) ******************************/
10+
public static void correctSrp() {
11+
InvoiceSrp invoice = new InvoiceSrp("Laptop",2,1200.00);
812

13+
InvoicePrinter printer = new InvoicePrinter();
14+
printer.printInvoice(invoice);
15+
16+
InvoiceSaver saver = new InvoiceSaver();
17+
saver.saveToFile(invoice);
18+
}
19+
static class InvoiceSrp{
20+
private String product;
21+
private int quantity;
22+
private double price;
23+
24+
public InvoiceSrp(){
25+
26+
}
27+
28+
public InvoiceSrp(String product, int quantity, double price){
29+
this.product = product;
30+
this.quantity = quantity;
31+
this.price = price;
32+
}
33+
34+
public double calculateTotal(){
35+
return quantity * price;
36+
}
37+
38+
public String getProduct() {
39+
return product;
40+
}
41+
42+
public int getQuantity() {
43+
return quantity;
44+
}
45+
46+
public double getPrice() {
47+
return price;
48+
}
49+
}
50+
51+
static class InvoicePrinter {
52+
public void printInvoice(InvoiceSrp invoice){
53+
System.out.println("Producto: "+invoice.getProduct());
54+
System.out.println("Cantidad: "+invoice.getQuantity());
55+
System.out.println("Precio: "+invoice.getPrice());
56+
System.out.println("Total: "+invoice.calculateTotal());
57+
}
58+
}
59+
60+
static class InvoiceSaver{
61+
public void saveToFile(InvoiceSrp invoice){
62+
try (FileWriter writer = new FileWriter("invoice.txt")){
63+
writer.write("Producto: " + invoice.getProduct() + "\n");
64+
writer.write("Cantidad: " + invoice.getQuantity() + "\n");
65+
writer.write("Precio unitario: " + invoice.getPrice() + "\n");
66+
writer.write("Total: " + invoice.calculateTotal() + "\n");
67+
} catch (Exception e) {
68+
System.out.println("ERROR: no se pudo generar la factura..."+e.getMessage());
69+
}
70+
}
71+
}
972
/****************************** ejemplo sin srp(Incorrecto) ******************************/
1073
public static void incorrectSrp(){
1174
Invoice invoice = new Invoice("Laptop",2,1200.00);

0 commit comments

Comments
 (0)