Method remove () does not remove object from ArrayList

1

I have a question to remove an object from a list in a job. I have an automotive class:

public class Automovel {
     String marca;
     String modelo;
     String preco;



    Automovel(String marca, String modelo, String preco) {
       this.marca = marca;
        this.modelo = modelo;
        this.preco = preco;
    }
// ja esta como geter e seters

I also have a utility class that implements this class:

package metodista.ads.carro;

import java.util.ArrayList;

/**
 *
 * @author Luiz Ricardo
 */
public class Concessionaria {

    public Concessionaria() {
    }

    ArrayList<Automovel> carros = new ArrayList<>();

    //O método adicionaVeiculo deverá adicionar um veículo ao estoque (atributo listaAutomoveis).
    public void adicionaVeiculo(Automovel automovel){
         carros.add(automovel);
    }
    // O método vendaVeiculo remove um veículo do estoque.
    public void vendaVeiculo(Automovel automovel){
        carros.remove(automovel);
    }

    //O método consultaEstoqueVeiculo devolve os veículos em estoque.
    public String consultaEstoqueVeiculo(){
        return carros.size()+"";
    }


}
//

My main class has a screen for user data entry, my doubt is in the method to remove an Auto object, I have a button that when clicked would need to take an automobile object out of stock, but it does not work:

private void btVenderActionPerformed(java.awt.event.ActionEvent evt) {     
 // construtor concessionaria        
Concessionaria con = new Concessionaria(); 

        String marca = tfMarca.getText();
        String modelo = tfModelo.getText();
        String preco = tfPreco.getText();
        Automovel auto2 = new Automovel(marca, modelo, preco);
        con.vendaVeiculo(auto2);

My main class has a screen for user data entry, my question is in the VehicleSale method to remove an Automotive object.

    
asked by anonymous 15.04.2018 / 15:47

1 answer

1

It does not work because the Automovel that the method tries to remove does not exist in the ArrayList.

The remove() method uses the default implementation of the equals() method of the object to remove, to see if it exists in the ArrayList. The default implementation only checks to see if it is the same instance.

Using it works like this:

ArrayList<Automovel> carros = new ArrayList<>();

Automovel auto1 = new Automovel("marca1", "modelo1", preco);
carros.add(auto1);
Automovel auto2 = new Automovel("marca2", "modelo1", preco);
carros.add(auto2);

carros.remove(auto1);

Since the remove() method depends on the equals() method to identify when two objects are equal (they represent the same entity), it must be overwritten according to the equality requirements.

You must determine which fields in the Automovel class identify a Automovel , which make it unique and use them in the implementation of the equals () method.

For example, if you consider that two cars are the same if they are of the same make and model, a possible implementation for the equals () method is:

@Override
public boolean equals(Object o) {
    // verifica se é o mesmo objecto
    if (this == o)
        return true;
    // verifica se é null
    if (o == null)
        return false;
    // verifica tipo e cast
    if (getClass() != o.getClass())
        return false;

    Automovel automovel = (Automovel) o;
    // Comparação atributo a atributo
    // Note que cada um dos atributos têm também de implementar correctamente o método equals()
    return  Objects.equals(marca, automovel.marca) &&
            Objects.equals(modelo, automovel.modelo);
}
    
15.04.2018 / 16:35