Why is it that when I insert 3 objects, only the last one appears to me?

0

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

I've implemented two programs one for main and one for carro.java . So my doubt is that I can enter and query data from the cars, but if I enter data for 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;
    }
}
    
asked by anonymous 24.05.2015 / 10:47

1 answer

2

Start by creating a Stand .

Stand stand = new Stand();

Whenever the 'Insert' option is chosen you should create a new car, preview its attributes, and finally add it to the Stand .

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

Note: The insereCarro() method of the Stand class should receive a Car and not a Stand / p>

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

case 2:
    stand.consultaCarro();
    break;  

Pass System.out.println() into this 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 car registration you want to remove and call the removeCarro() method.

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