sexta-feira, 25 de abril de 2008

Código 1 do Dia 25.05.2008

package pessoa;

import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
*
* @author Pablo Dunke
*/
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
java.util.ArrayList colecao = new ArrayList();
boolean fim = false;
while (!fim) {
String s = JOptionPane.showInputDialog(null, "Nome", "Informe o Nome: ", 1);
if (!"".equals(s.trim())) {
String idadeAux = JOptionPane.showInputDialog(null, "Idade", "Informe a Idade", 1);
int idade = Integer.parseInt(idadeAux);
Pessoa p = new Pessoa();
p.nome = s;
p.idade = idade;
colecao.add(p);
} else {
fim = true;
}
}

for (Pessoa p : colecao) {
System.out.println(p.nome + " - " + p.idade + " Anos");
}
java.util.Collections.sort(colecao);
for (Pessoa p : colecao) {
System.out.println("\nOrdenadamente:\n");
System.out.println(p.nome + "\t" + p.idade + "anos");
}
}
}


Parte 2
package pessoa;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Adm
*/
public class Pessoa implements java.lang.Comparable {
public String nome;
public int idade;
public int compareTo(Object o) {
Pessoa outra = (Pessoa)o;
return this.nome.compareTo(outra.nome);
}
}

Sujeito a erros.

Nenhum comentário: