import java.text.NumberFormat;
import java.nio.file.*;
import java.io.*;
import java.util.ArrayList;
//class ProdMantApp{
class Main{
static BD bd = new BD();
public static void main(String[] args){
bd.escribir(Utility.ranI(10));
bd.leer();
reportar();
}
static void reportar(){
System.out.println("Nombre\tCant.\tPrecio");
float max = 0;
String nombreMax = "";
for(Producto p: bd.productos){
System.out.println(p);
if(max < p.precio){
max = p.precio;
nombreMax = p.nombre;
}
}
System.out.println("El precio mรกximo es: " + max + ", corresponde al producto: " + nombreMax);
}
}
class Utility{
static int ranI(int n){
return (int) (Math.random()*n)+1;
}
static float ranF(int n){
return (float)(Math.random()*n)+1;
}
}
class Producto{
String nombre;
int cantidad;
float precio;
Producto(String nombre, int cantidad, float precio){
this.nombre = nombre;
this.cantidad = cantidad;
this.precio = precio;
}
public String toString(){
return nombre + "\t" + cantidad + "\t" + NumberFormat.getNumberInstance().format(precio);
}
}
class BD{
ArrayList<Producto> productos = new ArrayList<>();
BD(){
Path uno = Paths.get("uno");
try{
if (Files.notExists(uno))
//Files.createDirectories(uno);
} catch (IOException e) {System.out.println(e);}
}
void escribir(int nReg){
try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("productos.txt", false)))){
for(int i=1; i<= nReg ; i++) {
Producto p = new Producto("P"+i, Utility.ranI(100), Utility.ranF(50));
out.println(p);
}
} catch (IOException e){ System.out.println(e);}
}
void leer(){
try (BufferedReader in = new BufferedReader(new FileReader("productos.txt"))){
String[] cols;
String nombre, cantidad, precio;
String line = in.readLine();
while(line != null) {
cols = line.split("\t");
productos.add(new Producto(cols[0], Integer.parseInt(cols[1]), Float.parseFloat(cols[2])));
line = in.readLine();
}
} catch (IOException e) {System.out.println(e);}
}
}