Why do I only see the last one when I insert three objects?

Asked

Viewed 102 times

0

I need to create a class carro. The program must contain a collection of cars of the type ArrayList. Your program should allow you to add, delete and query cars to ArrayList.

I implemented two programs one for main and another to carro.java. So my doubt is that I can enter and query data of cars, but if I enter data relating to three cars shows only the last one, it points to the last car.

Class main.java:

import java.util.ArrayList;
import java.util.Scanner;

public class main {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        Carro car = new Carro();
        int op;

        do {
            System.out.println("[1] Inserir");
            System.out.println("[2] Consultar");
            System.out.println("[3] Remover");
            System.out.println("[4] Sair");
            System.out.print("Opção desejada: ");
            op = input.nextInt();
            Object matricula;

            switch (op) {
            case 1:
                input.nextLine();                       /*Pretendo Inserir dados*/
                System.out.print("Matricula: ");
                car.setMatricula(input.nextLine());
                System.out.print("Marca: ");
                car.setMarca(input.nextLine());
                System.out.print("Modelo: ");
                car.setModelo(input.nextLine());
                System.out.print("Cor: ");
                car.setCor(input.nextLine());

                break;
            case 2:
                System.out.println("Matricula: " + car.getMatricula());
                System.out.println("Marca: " + car.getMarca());
                System.out.println("Modelo: " + car.getModelo());
                System.out.println("Cor: " + car.getCor());
                break;
            case 3:
                System.out.print("Matricula: ");
                car.setMatricula("");
                car.setMarca("");
                car.setModelo("");
                car.setCor("");
                break;
            }
        } while (op != 4);
    }

    public class Stand {

        private ArrayList<Carro> listaStand = new ArrayList<Carro>();

        public void insereCarro(Stand Carro)        //aqui pretendo inserir dados carro*/
        {
            listaStand.add(Carro);
        }

    public void consultaCarro()             //Aqui pretendo fazer consulta dos carros do stand */
        {
            for (Carro c: listaStand) 
            {
                System.out.println(listaStand.get(0));
            }
        }

     public String removeCarro(String Stand) {  //*aqui pretendo remover carro pela matricula
            for (Carro c : this.listaStand) {
                if (c.getMatricula().equals(Stand))
                    c.remove();
            }
    }}}

Class carro.java

public class Carro
{
    String marca;
    String modelo;
    String matricula;
    String cor;
    private String Stand;

    public void Carro(String matricula, String marca, String modelo,  String cor)
    {
        this.matricula = matricula;
        this.marca = marca;
        this.modelo = modelo;
        this.cor = cor;
    }
    public String getMatricula() {
        return matricula;
    }

    public void setMatricula(String matricula) {
        this.matricula = matricula;
    }

    public String getMarca() {
        return marca;
    }

    public void setMarca(String marca) {
        this.marca = marca;
    }

    public String getModelo() {
        return modelo;
    }

    public void setModelo(String modelo) {
        this.modelo = modelo;
    }

    public String getCor() {
        return matricula;
    }

    public void setCor(String cor) {
        this.cor = cor;
    }


    @Override
    public String toString() {
        return " matricula=" + matricula +",marca=" + marca + ", modelo=" + modelo +  ",cor=" + cor;
    }
}
  • If instead: for (Carro c: listaStand) &#xA; {&#xA; System.out.println(listaStand.get(0));&#xA; } you do that: for (Carro c: listaStand) &#xA; {&#xA; System.out.println(c);&#xA; }?

  • 1

    Why are you closing this question?

  • 1

    @Jorgeb. I also found it strange. I’m voting to "leave it open" in the analysis queue.

  • Note: Class names must start with uppercase

1 answer

3

Start by creating a Stand.

Stand stand = new Stand();

Whenever the 'Insert' option is chosen you must create a new car, fill its attributes and, at the end, add it to the Stand.

case 1:
    carro = new carro();
    ...
    ....
    ....
    stand.insereCarro(carro);
    break;

Note: The method insereCarro() class Stand must receive a Car and not a Stand

In the 'Query' option call the method consultaCarro() class Stand

case 2:
    stand.consultaCarro();
    break;  

Pass the System.out.println() into that method:

public void consultaCarro() //Aqui pretendo fazer consulta dos carros do stand */
    {
        for (Carro c: listaStand) 
        {
            System.out.println("Matricula: " + c.getMatricula());
            System.out.println("Marca: " + c.getMarca());
            System.out.println("Modelo: " + c.getModelo());
            System.out.println("Cor: " + c.getCor());
        }
    }

In the 'Remove' option you will have to ask the user which license plate of the car you want to remove and call the method removeCarro().

case 3:
    string matriculaRemover;
    ....
    ....
    stand.removeCarro(matriculaRemover);//Remove o carro do stand
    break;

Browser other questions tagged

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