Classe Dipendente:
codice:
public class Dipendente
{
private String nome;
private String cognome;
private double stipendio;
private String tipologia;
public Dipendente(String nom, String cogn, double sti, String tip)
{
nome = nom;
cognome = cogn;
stipendio = sti;
tipologia = tip;
}
public double getStipendio()
{
return stipendio;
}
public String getNome()
{
return nome;
}
public String getCognome()
{
return cognome;
}
public String getTipologia()
{
return tipologia;
}
public static void stampa(Dipendente b[])
{
for(int i = 0; i<3; i++)
{
System.out.println(b[i].getNome()+" - "+b[i].getCognome()+" - "+b[i].getStipendio()+" - "+b[i].getTipologia());
}
}
public static void oridinStipend(Dipendente b[], boolean crescente)
{
int i=0;
int j=b.length;
boolean ordinato=false;
int CrescDecresc;
if(crescente==true) CrescDecresc = 1;
else CrescDecresc =-1;
while(i<j && ordinato==true)
{
ordinato=true;
i++;
for(int p=j-1; p>=i; i--)
{
if(b[i].compareTo(i-1)*CrescDecresc<0)
{
ordinato= false;
Dipendente tmp = b[i];
b[i]=b[i-1];
b[i-1]=tmp;
}
}
}
}
public int compareTo(Object OtherObject)
{
int ret;
Dipendente other = (Dipendente)OtherObject;
if (stipendio>other.stipendio)ret=1;
else if (stipendio==other.stipendio)ret=0;
else ret=-1;
return ret;
}
}
Classe DipendenteTester
codice:
import java.util.Scanner;
public class DipendenteTester
{
public static void main(String[] args)
{
int scelta;
Scanner in=new Scanner(System.in);
Dipendente lista[] = new Dipendente[3];
Poparray(lista);
do
{
System.out.println("\n\n-------Menu-------\n\n"+
"1. Ordina dipendenti per stipendio(Crescente)\n"+
"2. Ordina dipendenti per stipendio(Decrescente)\n");
System.out.println("Inserisci una scelta:");
scelta=in.nextInt();
switch(scelta)
{
case 1:
System.out.println("\n\n---Ordinamento crescente (per stipendio):---\n");
Dipendente.oridinStipend(lista, true);
Dipendente.stampa(lista);
break;
case 2:
System.out.println("\n\n---Ordinamento decrescente (per stipendio):---\n");
Dipendente.oridinStipend(lista, false);
Dipendente.stampa(lista);
break;
}
}while (scelta!=3);
}
public static void Poparray(Dipendente[] b)
{
b[0] = new Dipendente("Vincenzo", "Rossi", 900, "Lavoratore a cottimo");
b[1] = new Dipendente("Antonio", "Grassi", 1500, "Impiegato");
b[2] = new Dipendente("fa", "gsfi", 2000, "Impiegato");
}
}