import java.io.*; import java.net.*; import java.util.Scanner; import java.sql.*; public class Caja { public static void main(String[] args ) { try { int port = Integer.parseInt(args[1]); Socket s = new Socket(args[0], port); Scanner in =new Scanner(s.getInputStream()); Cambio cambio = new Cambio(in.nextDouble()); System.out.println("Cambio inicial="+cambio.getPrecio()); Venta venta =new Venta(cambio); venta.start(); while(true) cambio.setPrecio(in.nextDouble()); } catch (Exception e) { e.printStackTrace(); } } } class Cambio { private double precio; public Cambio (double p){ precio=p; } public synchronized double getPrecio(){ return precio; } public synchronized void setPrecio(double p){ precio=p; } } class Venta extends Thread { Cambio cambio; Scanner in; DB db; public Venta(Cambio c) { cambio = c; in = new Scanner (System.in); db = new DB(); } public void run() { double amount; double total; while (true) { amount = in.nextDouble(); if (amount <0) System.exit(0); total = amount*cambio.getPrecio(); System.out.println(total); db.storeInDB((int)cambio.getPrecio(), (int) total); } } } class DB { private Statement sentencia; public DB() { Connection conexion; try { // se carga el Driver JDBC Class.forName("org.mariadb.jdbc.Driver"); } catch( Exception e ) { System.out.println( "No se pudo cargar el driver" ); e.printStackTrace(); System.exit(-1); } try { conexion = DriverManager.getConnection("jdbc:mariadb://localhost/Ventas","root", ""); System.out.println( "Conexión establecida" ); sentencia = conexion.createStatement(); }catch( Exception e ) { System.out.println( e );} } public void storeInDB(int precio, int total) { try { sentencia.executeUpdate( "INSERT INTO Valpo " + "VALUES("+precio+","+total+")" ); } catch( Exception e ) {System.out.println( e ); return; } } }