How to do every time you pass "true", does it print the entire instantiated object?

3
import java.util.ArrayList;


public class Loja {
    private ArrayList<Produto> ListaDeProdutos = new ArrayList<>();

   /* private ArrayList<Livro> ListaDeLivros = new ArrayList<>();
    private ArrayList<Disco> ListaDeDiscos = new ArrayList<>();*/

    public void cadastrarProduto(Produto produto){
        ListaDeProdutos.add(produto);

    }

    public void listarProdutos(){
        ListaDeProdutos.forEach(P -> System.out.println(P.toString() + "\n"));
    }



    //Lista de DISCOS

    public void listarDiscos(){
        ListaDeProdutos.forEach(P -> System.out.println(P.toString().contains("Banda"))); //eu quero que ele imprima todo o conteúdo setado, não apenas "true"


    }


}

Test

public class Teste {
    public static void main(String[] args) {
            Loja l1 = new Loja();

            l1.cadastrarProduto(new Livro("Java - Use A cabeça", 01, 120.00f, "Fulano de tal", "DevMasters", 400, "PROGRAMAÇÃO"));
            l1.cadastrarProduto(new Livro("LIVRO DE C", 02, 80.00f, "Deitel", "Dev Masters", 800, "PROGRAMAÇÃO"));
            l1.cadastrarProduto(new Livro("PYTHON", 03, 100.00f, "Lokão dazideia", "Dev Masters", 200, "PROGRAMAÇÃO"));
            l1.cadastrarProduto(new Disco("Back in Black", 04, 100.00f, "AC/DC", "Hard Rock", 15));
            l1.cadastrarProduto(new Disco("Sattelite", 05, 20.00f, "P.O.D.", "White Metal", 11));
            l1.cadastrarProduto(new Disco("Musashi", 06, 15.00f, "Rashid", "Rap", 14));


            l1.listarDiscos();
    }
}

Output

false
false
false
true
true
true
    
asked by anonymous 14.09.2018 / 14:48

2 answers

6

The question does not give many details, but it would look something like this:

for (Produto produto : ListaDeProdutos ) {
    if (produto.nome.contains("Banda")) {
        System.out.println(produto.nome);
        System.out.println(produto.preco);
        System.out.println(produto.genero);
    }
}

I placed GitHub for future reference .

I thought it best. The use of stream may seem cute, but it is slower, it has semantics different from the normal loop, although in this example it does not cause problems, but it has a series of implications that nobody understands and it is normal to cause problems , and there is almost no real gain except in some cases, leaving a code with fewer rows, which is not always good. In this case this would not even happen, so it's a waste of time.

Also do not use tostring() for this , it makes no sense to create some general logic for specific requirement. It is an abuse to use it to format data, worse still have a debug function be used with complexity O (N).

Actually there may have a conceptual error. I do not know if Loja should print something, it render is ok, printing is already a detail of the platform where it runs and maybe should be manipulated elsewhere. For exercise you can do this, but know that in real code this is not how you do it.

    
14.09.2018 / 14:57
3

The contains method returns a boolean , so the code only prints true or false . If you want to print products that satisfy a condition, you can make a simple% as suggested by Maniero . p>

Another option is to transform the list into a stream and use for to filter the products that satisfy your condition and then print them:

ListaDeProdutos.stream()
    // filtrar produtos
    .filter(p -> p.toString().contains("Banda"))
    // imprimir
    .forEach(System.out::println);

But if you want to filter the products by a specific type (in this case, filter ), you could use Disco to test whether the product is an instance of instanceof :

ListaDeProdutos.stream()
    // filtrar produtos que são Disco
    .filter(p -> p instanceof Disco)
    // imprimir
    .forEach(System.out::println);
    
14.09.2018 / 15:07