Method Using Array - Doubt

Asked

Viewed 43 times

1

Good night, Personal is it possible to create a method that by setting only the product attributes already add the list in the array? If yes, could you give me a hint?

This is only part of the code of a workshop service order (Work of the Facul).

insira o código aqui
    Produto[] caixa = new Produto[4];

    Produto p1 = new Produto();
    p1.setCodigo("0001");
    p1.setMarca("BMW");
    p1.setModelo("01");
    p1.setValor(220);
    p1.setProduto(TipoProduto.MOTOR);

    Produto p2 = new Produto();
    p2.setCodigo("003");
    p2.setMarca("HONDA");
    p2.setModelo("02");
    p2.setValor(300);
    p2.setProduto(TipoProduto.PNEUS);

    Produto p3 = new Produto();
    p3.setCodigo("0004");
    p3.setMarca("kAWASAKI");
    p3.setModelo("03");
    p3.setValor(1.500);
    p3.setProduto(TipoProduto.GUIDÃO);

    caixa[0] = p1;
    caixa[1] = p2;
    caixa[2] = p3;


    ordem.setListadeprodutos(caixa);

    for (int i = 0; i < caixa.length; i++) {

        System.out.println("Código :" + ordem.getListadeprodutos()[i].getCodigo());
        System.out.println("Marca :" + ordem.getListadeprodutos()[i].getMarca());
        System.out.println("Modelo :" + ordem.getListadeprodutos()[i].getModelo());
        System.out.println("Valor :" + ordem.getListadeprodutos()[i].getValor());

        System.out.println("\n");

    }
  • Good evening! Yes possible! check the post: https://stackoverflow.com/questions/4537980/equivalent-to-push-or-pop-for-arrays

  • I don’t understand if you just want to discover the method that adds the product to the box , or if you want to create a method that adds the products to the box by passing the product attributes as parameter.

  • I was in doubt if I could do it, but for what I’m searching it is possible, seriously create a method that adds 1 product at a time to the array box, passing as parameter the attributes of the product.

  • In this case, you can create a constructor in the Product class that already instates it by its attributes, this would save some lines of code, create also an array with global scope and then a method that instates a new product and plays it in the next empty position of the array. will avoid so much duplicity in your code.

  • Thanks, I was just missing a direction of how to do, I’ll try.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.