1
I’m having a doubt to remove an object from a list on a job. I have a car 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 dealership 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 input user data, my doubt is in the method to remove an Automovel object, I have a button that when being clicked would need to take an auto object from 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 doubt is in the way of saleDownload to remove an Automovel object.
First you need to define a shape and identify each car (as an id for example), without this it is impossible to do what you want.
– user28595
Another thing, your code has a logic error: if you want to remove an existing car, why create a new dealership and a new car when removing? This doesn’t make any sense to what you want to do, so it’s important to provide a [mcve] because the error is not only in that code
– user28595
I can define a way to start the counter indicating q and spoiler the maximum amount of cars in stock, then add and remove, passed p the index of the list ?
– Ricardo Santos