Hello I have the following Java 8 classes.
I want to access the method AgregarArticulo()
but in the uml diagram it is marked in red and since main
it does not let me access that method, I can only use the order method but before ordering I need to add the items. What would be the correct way to use the method AgregarArticulos()
?
This is what eclipse marks when I want to use the method
"The method AddArticle(Article) is undefined for the type List"
public class Articulo implements Comparable<Articulo>{
protected String nombre;
protected float precio;
public Articulo() {
super();
}
public Articulo(String nombre, float precio) {
super();
this.nombre = nombre;
this.precio = precio;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public float getPrecio() {
return precio;
}
public void setPrecio(float precio) {
this.precio = precio;
}
@Override
public int compareTo(Articulo o) {
if (getPrecio() < o.getPrecio()) {
return -1;
}
if (getPrecio() > o.getPrecio()) {
return 1;
}
return 0;
}
}
public class ListaArticulos {
List<Articulo> lista = new ArrayList<Articulo>();
public ListaArticulos() {
}
public ListaArticulos(Articulo articulo) {
AgregarArticulo(articulo);
}
private void AgregarArticulo(Articulo articulo) {
lista.add(articulo);
}
public void ordenar() {
Collections.sort(lista);
for (Articulo articulo : lista)
System.out.println(articulo.toString());
}
}
public class Main {
public static void main(String[] args) {
List <Articulo> listaArticulos= new ArrayList<Articulo>();
Articulo a1= new Articulo("Coca-cola", 64);
Articulo a2= new Articulo("Coca-cola", 18);
listaArticulos.AgregarArticulo(a1); //Acá quiero usar el metodo AgregarArticulo()
}
}
EDIT:
Sorry I had not seen the whole object because a {} was missing
In order for you to access you
AgregarArticulo
have to use your classListaArticulos
:Once added to our list it is ready to show it!