Ragazzi vi posto prima le classi che ho implementato:
codice:public class Squadra implements Iterable<Giocatore>{ private String nomeSquadra; private List<Giocatore> giocatori; public Squadra(String nomeSq){ this.nomeSquadra = nomeSq; giocatori = new ArrayList<Giocatore>(); } //metodi public String getNome(){ return this.nomeSquadra; } public void addGiocatore(Giocatore g){ if(!giocatori.contains(g)) giocatori.add(g); } public Iterator<Giocatore> iterator() { return giocatori.iterator(); } public void sort(){ Collections.sort(giocatori); } public String toString(){ return getNome(); } public void stampa(){ for(Giocatore g : giocatori){ System.out.println(g.toString() + " " + getNome()); } } }La classe Test è la seguente:codice:public abstract class Giocatore implements Comparable<Giocatore> { String nome,cognome; double altezza; Squadra squadra; public Giocatore(String nome, String cognome, double alt, Squadra sq){ this.nome = nome; this.cognome = cognome; this.altezza = alt; this.squadra = sq; } //metodi public String getNome(){ return this.nome; } public String getCognome(){ return this.cognome; } public double getAltezza(){ return this.altezza; } public Giocatore[] ordina(Giocatore[] array){ Giocatore g = null; for(int j=0;j<array.length;j++){ for(int i=0;i<array.length;i++){ if(array[j].altezza>array[i].altezza){ g = array[j]; array[j] = array[i]; array[i] = g; } } } return array; } public String toString(){ String s = getNome() + " " + getCognome() + " " + getAltezza() + " " + squadra.getNome(); return s; } }
codice:public static void main(String[] args) throws Exception { // TODO Auto-generated method stub FileReader fr = new FileReader("Squadre.txt"); BufferedReader br = new BufferedReader(fr); String s = br.readLine(); ArrayList<Giocatore> giocatori = new ArrayList<Giocatore>(); Squadra squadre = new Squadra(""); while(s!=null){ String[] array = s.split(","); if(s.charAt(s.length()-1)=='R'){ Riserva r = null; String nome = array[0]; String cognome = array[1]; String altezza = array[2]; double alt = Double.parseDouble(altezza); String squadra = array[3]; Squadra sq = new Squadra(squadra); r = new Riserva(nome,cognome,alt,sq); giocatori.add(r); squadre.addGiocatore(r); } else if (s.charAt(s.length()-1)=='T'){ Titolare t = null; String nome = array[0]; String cognome = array[1]; String altezza = array[2]; double alt = Double.parseDouble(altezza); String squadra = array[3]; Squadra sq = new Squadra(squadra); t = new Titolare(nome,cognome,alt,sq); giocatori.add(t); squadre.addGiocatore(t); } s = br.readLine(); } // STAMPO I GIOCATORI Giocatore[] g = new Giocatore[giocatori.size()]; Giocatore[] gioca = giocatori.toArray(g); //ORDINO for(int i=0;i<gioca.length;i++){ gioca = gioca[i].ordina(g); } System.out.println("GIOCATORI"); for(int i=0;i<gioca.length;i++){ if(gioca[i] instanceof Titolare) System.out.println(gioca[i].toString() + " titolare"); else System.out.println(gioca[i].toString() + " riserve"); } System.out.println(); System.out.println("TITOLARI"); for(int i=0;i<gioca.length;i++){ if(gioca[i] instanceof Titolare){ System.out.println(gioca[i].toString()); } } System.out.println(); System.out.println("RISERVE"); for(int i=0;i<gioca.length;i++){ if(gioca[i] instanceof Riserva){ System.out.println(gioca[i].toString()); } } }

Rispondi quotando