buenas quisiera que me ayuden como implemento el siguiente metodo en mi arraylist les dejo lo que e codificado de la clase y el arraylist el metodo dice:
se debe Comparar un coche con su anteriormente ingresado
clase coche
arraylistcoches
pueden ayudarme a como debo implementar ese metodo en mi arraylist
se debe Comparar un coche con su anteriormente ingresado
clase coche
Código [Seleccionar]
public class Coche {
// Atributos
private static int aux=0;
private final int numCarros;
private String matricula;
private String marca;
private String modelo;
private int Km;
// Getters y setters
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 int getKm() {
return Km;
}
public void setKm(int km) {
Km = km;
}
public static int getAux() {
return aux;
}
public static void setAux(int aux) {
Coche.aux = aux;
}
public int getNumCarros() {
return numCarros;
}
// constructor por defecto
public Coche() {
this.numCarros=0;
this.matricula = "";
this.marca = "";
this.modelo = "";
Km = 0;
}
// constructor
public Coche( String vmarca,String vmatricula, String vmodelo, int vkm) {
numCarros=++aux;
this.marca = vmarca;
this.matricula = vmatricula;
this.modelo = vmodelo;
Km = vkm;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Coche other = (Coche) obj;
if (Km != other.Km)
return false;
if (marca == null) {
if (other.marca != null)
return false;
} else if (!marca.equals(other.marca))
return false;
if (matricula == null) {
if (other.matricula != null)
return false;
} else if (!matricula.equals(other.matricula))
return false;
if (modelo == null) {
if (other.modelo != null)
return false;
} else if (!modelo.equals(other.modelo))
return false;
if (numCarros != other.numCarros)
return false;
return true;
}
@Override
public String toString() {
return "Auto # :"+ numCarros + "\n Marca: " + this.getMarca() + "\n Modelo: " + modelo +
"\n Matricula: " + this.getMatricula() + "\n Kilometraje: " + Km;
}
}
arraylistcoches
Código [Seleccionar]
import java.util.ArrayList;
public class ListaCoches {
private ArrayList<Coche> lista;
public ArrayList<Coche> getLista() {
return lista;
}
public void setLista(ArrayList<Coche> lista) {
this.lista = lista;
}
public ListaCoches() {
this.lista = new ArrayList<Coche>() ;
}
// metodos
public void mostrarCoches() {
for(Coche c : lista){
System.out.println(c);
}
}
public void agregarCoches(Coche c1) {
lista.add(c1);
}
public void mostrarPorMarca(String marca) {
ListaCoches listam = new ListaCoches();
for(Coche c :lista) {
if(c.getMarca().equals(marca)) {
listam.agregarCoches(c);
System.out.println(c);
}
}
}
public void mostrarPorKm(int kilometros) {
ListaCoches listak = new ListaCoches();
for(Coche c:lista) {
if(c.getKm()<kilometros) {
listak.agregarCoches(c);
System.out.println(c);
}
}
}
public Coche mayorKm() {
Coche aux=new Coche();
int numKm=0;
for(Coche c:lista) {
if(c.getKm()>numKm) {
numKm=c.getKm();
aux=c;
}
}
return aux;
}
public void ordenadosPorKm() {
int i,j;
Coche aux;
for(i=0;i<lista.size()-1;i++)
for(j=0;j<lista.size()-i-1;j++)
if(lista.get(j+1).getKm()<lista.get(j).getKm()) {
aux=lista.get(j+1);
lista.set(j+1, lista.get(j));
lista.set(j, aux);
}
mostrarCoches();
}
}
pueden ayudarme a como debo implementar ese metodo en mi arraylist