|
| 1 | + |
| 2 | +import java.io.FileWriter; |
| 3 | + |
| 4 | +public class simonguzman { |
| 5 | + public static void main(String[] args) { |
| 6 | + incorrectSrp(); |
| 7 | + } |
| 8 | + |
| 9 | + /****************************** ejemplo sin srp(Incorrecto) ******************************/ |
| 10 | + public static void incorrectSrp(){ |
| 11 | + Invoice invoice = new Invoice("Laptop",2,1200.00); |
| 12 | + invoice.printInvoice(); |
| 13 | + invoice.saveToFile(); |
| 14 | + } |
| 15 | + |
| 16 | + static class Invoice{ |
| 17 | + private String product; |
| 18 | + private int quantity; |
| 19 | + private double price; |
| 20 | + |
| 21 | + public Invoice(){ |
| 22 | + |
| 23 | + } |
| 24 | + |
| 25 | + public Invoice(String product, int quantity, double price){ |
| 26 | + this.product = product; |
| 27 | + this.quantity = quantity; |
| 28 | + this.price = price; |
| 29 | + } |
| 30 | + |
| 31 | + public double calculateTotal(){ |
| 32 | + return quantity * price; |
| 33 | + } |
| 34 | + |
| 35 | + public void printInvoice(){ |
| 36 | + System.out.println("Producto: "+product); |
| 37 | + System.out.println("Cantidad: "+quantity); |
| 38 | + System.out.println("Precio: "+price); |
| 39 | + System.out.println("Total: "+calculateTotal()); |
| 40 | + } |
| 41 | + |
| 42 | + public void saveToFile(){ |
| 43 | + try (FileWriter writer = new FileWriter("invoice.txt")){ |
| 44 | + writer.write("Producto: " + product + "\n"); |
| 45 | + writer.write("Cantidad: " + quantity + "\n"); |
| 46 | + writer.write("Precio Unitario: " + price + "\n"); |
| 47 | + writer.write("Total: " + calculateTotal() + "\n"); |
| 48 | + } catch (Exception e) { |
| 49 | + System.out.println("Error al guardar la factura: "+e.getMessage()); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | +} |
0 commit comments