cs_Julien39
Messages postés6414Date d'inscriptionmardi 8 mars 2005StatutModérateurDernière intervention29 juillet 2020371 21 nov. 2012 à 09:46
Si vraiment tu veux faire un truc propre, tu peux faire ca :
public class Facture implements Comparable<Facture> {
private int numero;
private String libelle;
public Facture(int numero, String libelle) {
this.numero = numero;
this.libelle = libelle;
}
public String getLibelle() {
return libelle;
}
@Override
public int compareTo(Facture f) {
return getLibelle().trim().substring(0, 12).compareTo(f.getLibelle().trim().substring(0, 12));
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result prime * result + ((libelle null) ? 0 : libelle.hashCode());
result = prime * result + numero;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj){
return true;
}
if (obj == null){
return false;
}
if (getClass() != obj.getClass()){
return false;
}
else {
Facture other = (Facture) obj;
return this.compareTo(other)==0;
}
}
public static void main(String[] args){
Facture f1 = new Facture(1, "00000000000001");
Facture f2 = new Facture(1, "00000000000002");
Facture f3 = new Facture(1, "00000000000500");
System.out.println(f1.equals(f2));
System.out.println(f1.equals(f3));
}
}