/* **********************************
CLASSE DI PROVA PER LISTA CHE
IMPLEMENTA OGGETTI
********************************** */
import java.io.*;
public class ProvaListaComparable {
public static void main(String[] args) {
//creo due liste destinate a contenere oggetti di tipo Person e Studenti
ListaComparable lPerson = new ListaComparable();
ListaComparable lStud = new ListaComparable();
//riempio lista di 10 nodi con oggetti "Person" casuali
for (int k=0; k<10; k++)
lPerson.InsertOrdered(
new Person("Nome " + (char)(k+65), (int)(Math.random()*50+1)));
//riempio lista di 10 nodi con oggetti "Studenti" casuali
for (int k=0; k<10; k++)
lStud.InsertOrdered(
new Studenti("Nome " + (char)(k+65), (int)(Math.random()*5000+1)));
//Stampa delle liste con il metodo getNextElem()
System.out.println("\nLista Persone ordinata per nome:");
try {
for (;;) System.out.println((Person)(lPerson.getNextElem()));
} catch (BottomOfListException e) {
System.out.println("Fine Lista!!");
} catch (EmptyListException e) {
System.out.println("Lista vuota!!");
}
System.out.println("\nLista Studenti ordinata per matricola:");
try {
//senza fine?? apparentemente!
for (;;) System.out.println((Studenti)(lStud.getNextElem()));
} catch (BottomOfListException e) {
System.out.println("Fine Lista!!");
} catch (EmptyListException e) {
System.out.println("Lista vuota!!");
}
}
}
//Classe Person con criterio di confronto nel Nome
class Person implements Comparable {
private String nome;
private int eta;
//Costruttore
public Person() {
this("", 0);
}
public Person(String nome, int eta) {
this.nome = nome;
this.eta = eta;
}
public int compareTo(Object p) {
return nome.compareTo(((Person)p).nome);
}
public boolean equals(Object p) {
if (p == null)
throw new NullPointerException();
return nome.equals(((Person)p).nome);
}
public String toString() {
return "Persona: " + nome + " " + eta;
}
}
//Classe Studenti con criterio di confronto nella matricola
class Studenti implements Comparable {
private String nome;
private int matr;
//Costruttore
public Studenti() {
this("", 0);
}
public Studenti(String nome, int matr) {
this.nome = nome;
this.matr = matr;
}
public int compareTo(Object p) {
if (matr < ((Studenti)p).matr) return -1;
else if (matr == ((Studenti)p).matr) return 0;
else return 1;
}
public boolean equals(Object p) {
if (p == null)
throw new NullPointerException();
return (matr == ((Studenti)p).matr);
}
public String toString() {
return "Studente: " + nome + " " + matr;
}
}