Show Java ArrayList elements

2

I'm doing an exercise where I should do a program for a point of sale. The functionalities are to add merchandise (name, quantity, price), sell merchandise and list inventory. Each merchandise is added to a ArrayList . The problem is that at the time of showing the elements of ArrayList only shows me the first element. This is my code:

INVENTORY CLASS:

import java.util.ArrayList;


public class Inventario {

      ArrayList<Mercancia> listaMercancias = new ArrayList<>();

     public  void agregarMercancia(Mercancia objMercancia){
        listaMercancias.add(objMercancia);
    }

     public String listarInventario(){
        for(Mercancia mercancias:listaMercancias){
            return "Nombre: " + mercancias.nombre + "; " + "Cantidad restante: " + mercancias.cantidad;

        }   

        return null;
    }

     public void venderMercancia(Mercancia objMercancia){

         objMercancia.cantidad -= 1;
         if (objMercancia.cantidad == 0) {
             listaMercancias.remove(objMercancia);
         }
     }

MERCANCIA CLASS:

public class Mercancia {
    String nombre;
    int cantidad;
    int precio;


    public Mercancia(String nombre, int cantidad, int precio) {
        this.nombre = nombre;
        this.cantidad = cantidad;
        this.precio = precio;
    } 


}

MAIN METHOD:

public static void main(String[] args) {


    Mercancia objMercancia1 = new Mercancia("producto1",5,500);
    Mercancia objMercancia2 = new Mercancia("producto2",6,200);
    Mercancia objMercancia3 = new Mercancia("producto3",2,300);

    Inventario objInventario = new Inventario();

    objInventario.agregarMercancia(objMercancia1);
    objInventario.agregarMercancia(objMercancia2);
    objInventario.agregarMercancia(objMercancia3);

    System.out.println(objInventario.listarInventario());
    System.out.println(objInventario.listaMercancias.size());


}

When I run the size() method, it tells me that there are 3 elements that are added.

    
asked by Marco 27.11.2017 в 17:36
source

3 answers

2

In listarInventario you make a return , with which the return occurs, you exit the method and only return the first element.

A quick change would be:

public void listarInventario(){
    for(Mercancia mercancias:listaMercancias){
        System.out.println("Nombre: " + mercancias.nombre + "; " + "Cantidad restante: " + mercancias.cantidad);

    }   
}

and in main simply:

objInventario.listarInventario();
    
answered by 27.11.2017 в 17:40
1

I advise you to return a String using the StringBuilder object, it would be something like this

 public String listarInventario(){
    StringBuilder listado = new StringBuilder();
    for(Mercancia mercancias:listaMercancias){
        listado.append("Nombre: " + mercancias.nombre + "; " + "Cantidad restante: " + mercancias.cantidad);

    }   

    return listado.toString();
}
    
answered by 27.11.2017 в 17:44
-1

First your method is returning the value when reading the first element, for that reason you only get the first value . I advise you to concatenate the values and then return the value of all the elements.

  public String listarInventario(){
        String resultado = "";

        for(Mercancia mercancias:listaMercancias){
            resultado = resultado + "Nombre: " + mercancias.nombre + "; " + "Cantidad restante: " + mercancias.cantidad  +  "\n";

        //También puedes usar el operador +=
        //resultado += "Nombre: " + mercancias.nombre + "; " + "Cantidad restante: " + mercancias.cantidad  +  "\n";

        }   

        return resultado ;
    }

In this way, you will return the value of all the results stored in listaMercancias , when you finish obtaining them in the loop, in case you do not obtain any element, you will simply return an empty string.

It is recommended to use the class StringBuilder when concatenating strings, when we have considerable information .

  public String listarInventario(){
       StringBuilder resultado = new StringBuilder(); 

        for(Mercancia mercancias:listaMercancias){
            resultado.append("Nombre: " + mercancias.nombre + "; " + "Cantidad restante: " + mercancias.cantidad);

        }   

        return resultado.toString();
    }

Even as Joel says, you can implement the toString() method in the class Mercancia

public class Mercancia {
    String nombre;
    int cantidad;
    int precio;


    public Mercancia(String nombre, int cantidad, int precio) {
        this.nombre = nombre;
        this.cantidad = cantidad;
        this.precio = precio;
    } 

     public String toString(){         
        return "Nombre: " + nombre + "; " + "Cantidad restante: " +cantidad+""; Precio: "+precio;     
     }  


}

This way you could get the same result:

  public String listarInventario(){
       StringBuilder resultado = new StringBuilder(); 

        for(Mercancia mercancias:listaMercancias){

           resultado.append(mercancias.toString());

        }   

        return resultado.toString();
    }
    
answered by 27.11.2017 в 17:43