import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class Comprar {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static String leer(String message) { String s;
try { System.out.print(message);
s = br.readLine();
} catch (IOException ex) { System.out.println("Hubo un error de lectura, vuelva a intentar"); s = null;
}
if (s == null) { s = leer(message);
}
return s;
}
public static Double leerDouble(String message) { Double d;
try { d = Double.parseDouble(leer(message));
} catch (NumberFormatException ex) { System.out.println("Valor incorrecto vuelva a intentar"); d = null;
}
if (d == null) { d = leerDouble(message);
}
return d;
}
public static void main(String[] args) { System.out.println("Sistema para calcular el total de una compra..."); List<Producto> productos = new ArrayList();
do { Producto p = new Producto();
p.setNombre(leer("Introduzca el nombre del producto: ")); p.setPrecio(leerDouble("Introduzca el precio: ")); productos.add(p);
System.out.println("Producto añadido..."); } while (leer("Introduzca 1 para gregar mas productos: ").equals("1")); Double total = 0.0;
for (int i = 0; i < productos.size(); i++) { total += productos.get(i).getPrecio();
}
//Limpiamos la consola
for (int i = 0; i < 15; i++) { System.out.println();
}
System.out.println("A continuación los productos de la compra: "); productos.forEach(p -> { System.out.println(p.getNombre() + "\t" + p.getPrecio());
});
System.out.println("Cantidad de productos: " + productos.size() + "\nTotal: " + total);
}
}
class Producto {
private String nombre;
private Double precio;
/**
* @return the nombre
*/
public String getNombre() { return nombre;
}
/**
* @param nombre the nombre to set
*/
public void setNombre(String nombre) { this.nombre = nombre;
}
/**
* @return the precio
*/
public Double getPrecio() { return precio;
}
/**
* @param precio the precio to set
*/
public void setPrecio(Double precio) { this.precio = precio;
}
}