Classe Conta.java:
public class Conta implements Comparable{
private String nome;
private int numero;
private long cpf;
public int getNumero() {
return numero;
}
public void setNumero(int numero) {
this.numero = numero;
}
public long getCpf() {
return cpf;
}
public void setCpf(long cpf) {
this.cpf = cpf;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public Conta() {
// TODO Auto-generated constructor stub
}
public Conta(String nome, int numero, long cpf) {
this.nome = nome;
this.numero = numero;
this.cpf = cpf;
}
@Override
public boolean equals(Object object){
if (object==null) {
return false;
}
if (!(object instanceof Conta)) {
return false;
}
Conta pessoa =(Conta) object;
return pessoa.getNumero() == this.numero;
}
public int compareTo(Object o){
if (this.getNumero() < ((Conta) o).getNumero()) {
return -1;
}
if (this.getNumero() > ((Conta) o).getNumero()) {
return 1;
}
return 0;
}
public String toString(){
return this.getNome() + " - " + this.getCpf() + " - " + this.getNumero() + "";
}
}
Classe ComparaPorNome.java
import java.util.Comparator;
public class ComparaPorNome implements Comparator{
@Override
public int compare(Conta o1, Conta o2) {
return o1.getNome().compareTo(o2.getNome());
}
}
Classe ComparaPorCpf.java
import java.util.Comparator;
public class ComparaPorCpf implements Comparator {
@Override
public int compare(Conta o1, Conta o2) {
// TODO Auto-generated method stub
if (o1.getCpf() < o2.getCpf()) {
return -1;
}
if (o1.getCpf() > o2.getCpf()) {
return 1;
}
return 0;
}
}
Classe ColecaoArray .java que é classe principal que invoca a
ordenação. Alguns comentarios estão presentes devido aos testes do
cálculo de tempo de ordenação.
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Arrays;
public class ColecaoArray {
 public static void main(String[] args) {
 Conta objs[] = new Conta[15];
 //Conta aux[] = new Conta[1];
 NumberFormat formater = new DecimalFormat("#00");
 String nome;
 int numero;
 long cpf;
 // ArrayList conta = new ArrayList;
 // estudantes.get(0).getNota();
 long t = System.currentTimeMillis();
 long startTime = System.nanoTime();
 long estimatedTime = 0;
 //long init;
 //long end = 0;
 //long diff;
  ColecaoArray imp = new ColecaoArray();  
  //init = System.currentTimeMillis();
 /* Coloque aqui seu codigo que demora */
  ////t = System.currentTimeMillis() - t;
 estimatedTime = System.nanoTime() - startTime;
 //end = System.currentTimeMillis();
 for (int i = 0; i < objs.length; i++) {
 numero = (int) (Math.random() * 21);
 nome = "Nome" + String.valueOf(formater.format(i + 1));
 cpf = (long) (11111111111L + (Math.random() * 88888888889L));
 objs[i] = new Conta(nome, numero, cpf);
 }
  imp.imprimir(objs);
 Arrays.sort(objs);
       imp.imprimir(objs);
 System.out.println();
 ComparaPorCpf cpCpf = new ComparaPorCpf();
 Arrays.sort(objs, cpCpf);
  imp.imprimir(objs);
 ComparaPorNome cpPorNome = new ComparaPorNome();
 Arrays.sort(objs, cpPorNome);
  imp.imprimir(objs);
  //System.out.println(t + " milisegundos");
 System.out.println(estimatedTime/1000 +" milisegundos");
 //diff = end - init;
 //System.out.println("Demorou " + (diff / 1000) + " segundos");
 // ... the code being measured ...
 // Conta[0].ComparaPorCpf(Conta[1]);
 }
 public void imprimir(Conta objs[]) {
 for (int i = 0; i < objs.length; i++) {
 System.out.print(objs[i].getNome() + "-" + objs[i].getCpf() + "----"
 + objs[i].getNumero());
 System.out.println();
 }
 System.out.println();
 }
}
 
Nenhum comentário:
Postar um comentário