What do I have
I have three classes. Main.java
, Producto.java
, and Supermercado.java
.
Main.java
contains the main.Producto.java
the product structure I want to print in my list.Supermercado.java
is a Singleton Pattern which represents the supermarket where my List is placed.
The code
I will show you guys only the code I think is related to the question, if you need more information please do not hesitate to ask.
Main.java
// Instanciar productos
Producto p1 = new Producto("Sal 100gr", LocalDate.now(), 0.5f, 1);
Producto p2 = new Producto("Pimienta 25gr", LocalDate.now(), 0.2f, 1);
Producto p3 = new Producto("Sal 1kg", LocalDate.now(), 2, 1);
Producto p4 = new Producto("Te limón", LocalDate.now(), 3.5f, 2);
Producto p5 = new Producto("Azucar 1kg", LocalDate.now(), 2, 1);
Producto p6 = new Producto("Azucar 5kg", LocalDate.now(), 10, 1);
Producto p7 = new Producto("Xilitol 1kg", LocalDate.now(), 15, 1);
Producto p8 = new Producto("Xilitol 1kg", LocalDate.now(), 15, 1);
// Añadir productos a la lista del super
Supermercado.getSupermercado().addProducto(p1);
Supermercado.getSupermercado().addProducto(p2);
Supermercado.getSupermercado().addProducto(p3);
Supermercado.getSupermercado().addProducto(p4);
Supermercado.getSupermercado().addProducto(p5);
Supermercado.getSupermercado().addProducto(p6);
Supermercado.getSupermercado().addProducto(p7);
Supermercado.getSupermercado().addProducto(p8);
// Printing the list
System.out.println("\nLista productos.");
Supermercado.getSupermercado().printListaProductos();
// Printing distinct items (do not repeat xilitol plz)
System.out.println("\nLista de productos sin repetir.");
Supermercado.getSupermercado().printListaProductosSinRepetir();
Supermercado.java
// Printing the list
public void printListaProductos() {
listaProducto.stream()
.forEach(p -> System.out.println("Producto " +p.getNombre()
+ '\t' + "Precio " + p.getPrecio() + "€"
+ '\t' + "Caducidad " + p.getFechaCaducidad()));
}
// Printing distinct items (do not repeat xilitol plz)
public void printListaProductosSinRepetir() {
listaProducto.stream()
.distinct()
.forEach(p -> System.out.println("Producto " +p.getNombre()
+ '\t' + "Precio " + p.getPrecio() + "€"
+ '\t' + "Caducidad " + p.getFechaCaducidad()));
}
Producto.java
private String nombre;
private int seccion;
private LocalDate fechaCaducidad;
private float precio;
// constructor
public Producto(String pNombre, LocalDate pFechaCaducidad, float pPrecio, int pSeccion) {
this.nombre = pNombre;
this.fechaCaducidad = pFechaCaducidad;
this.precio = pPrecio;
this.seccion = pSeccion;
}
public LocalDate getFechaCaducidad() {
return this.fechaCaducidad;
}
public float getPrecio() {
return this.precio;
}
public int getSeccion() {
return this.seccion;
}
public String getNombre() {
return this.nombre;
}
The Output
Lista productos.
Producto Sal 100gr Precio 0.5€ Caducidad 2016-05-27
Producto Pimienta 25gr Precio 0.2€ Caducidad 2016-05-27
Producto Sal 1kg Precio 2.0€ Caducidad 2016-05-27
Producto Te limón Precio 3.5€ Caducidad 2016-05-27
Producto Azucar 1kg Precio 2.0€ Caducidad 2016-05-27
Producto Azucar 5kg Precio 10.0€ Caducidad 2016-05-27
Producto Xilitol 1kg Precio 15.0€ Caducidad 2016-05-27
Producto Xilitol 1kg Precio 15.0€ Caducidad 2016-05-27
Lista de productos sin repetir.
Producto Sal 100gr Precio 0.5€ Caducidad 2016-05-27
Producto Pimienta 25gr Precio 0.2€ Caducidad 2016-05-27
Producto Sal 1kg Precio 2.0€ Caducidad 2016-05-27
Producto Te limón Precio 3.5€ Caducidad 2016-05-27
Producto Azucar 1kg Precio 2.0€ Caducidad 2016-05-27
Producto Azucar 5kg Precio 10.0€ Caducidad 2016-05-27
Producto Xilitol 1kg Precio 15.0€ Caducidad 2016-05-27
Producto Xilitol 1kg Precio 15.0€ Caducidad 2016-05-27
The Problem
Clearly, the .distinct()
code in printListaProductosSinRepetir()
method is not working for me. It should display this:
Lista de productos sin repetir.
Producto Sal 100gr Precio 0.5€ Caducidad 2016-05-27
Producto Pimienta 25gr Precio 0.2€ Caducidad 2016-05-27
Producto Sal 1kg Precio 2.0€ Caducidad 2016-05-27
Producto Te limón Precio 3.5€ Caducidad 2016-05-27
Producto Azucar 1kg Precio 2.0€ Caducidad 2016-05-27
Producto Azucar 5kg Precio 10.0€ Caducidad 2016-05-27
Producto Xilitol 1kg Precio 15.0€ Caducidad 2016-05-27
Without the extra Xilitol 1kg
. The items shouldn't repeat.
Why do I need this
I am trying to learn how Java 8
works with the Lambda-expressions
. So I am building myself some examples for this. The thing is that .distinct()
is not working. I couldn't find this solution in SO. Thanks in advance.