package formula1; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.Iterator; import java.util.List; public class Campionato { private ArrayList piloti=new ArrayList(); private ArrayList gp=new ArrayList(); //1.aggiungo il costruttore public Campionato(){ } /** * Crea un nuovo pilota e lo inserirsce nel campionato * @param nome il nome del pilota (Es. "Barrichello") */ public Pilota creaPilota(String nome) { // TODO Auto-generated method stub Pilota pilota =new Pilota(nome,this); piloti.add(pilota); return pilota; } /** * @return un elenco di oggetti Pilota */ public Collection getPiloti() { // TODO Auto-generated method stub return piloti; } /** * @param nome il nome del pilota * @return l'oggetto Pilota corrispondente */ public Pilota getPilota(String nome) { // TODO Auto-generated method stub boolean found=false; Pilota p=null; Iterator it=piloti.iterator(); while(it.hasNext() && !found){ p=(Pilota) it.next(); if(nome.compareTo(p.getNome())==0) found=true; } return p; } /** * @param nome il nome del gran premio (Es. "Imola") * @return l'oggetto GP corrispondente */ public GP definisciGranPremio(String nome) { // TODO Auto-generated method stub GP gran=new GP(nome); gp.add(gran); return gran; } /** * @param nome nome del GP * @return l'oggetto GP corrispondente */ public GP getGranPremio(String nome) { // TODO Auto-generated method stub boolean found=false; GP p=null; Iterator it=gp.iterator(); while(it.hasNext() && !found){ p=(GP) it.next(); if(nome.compareTo(p.getNome())==0) found=true; } return p; } /** * @param gp * @param r * @param hours * @param min * @param sec * @param cent * @return */ public Tempo setTempo(GP gp, Pilota r, int hours, int min, int sec, int cent) throws TempoNonValido{ // TODO Auto-generated method stub Tempo t=null; if(min>=0 && min<60 && sec>=0 && sec<60 && cent>=0 && cent<100){ t=new Tempo(hours,min,sec,cent); //passo al gran premio il pilota e il suo tempo gp.setarrivo(r,t); //passo al pilota il suo tempo del gran premio r.settempo(gp,t); Collections.sort(piloti, new Comparator(){ public int compare(Object o1, Object o2){ Pilota ob1=(Pilota) o1; Pilota ob2=(Pilota) o2; return ob2.getPunti()-ob1.getPunti(); } }); } else{ throw new TempoNonValido(); } return t; } /** * @return lista dei piloti ordinati in base ai punti totalizzati * ovvero al risultato del metodo getPunti() di Pilota */ public List getClassifica() { // TODO Auto-generated method stub //2. cambio il return //return null; return piloti; } /** * @param file path del file con informazioni su piloti e GP * @throws FileNotFoundException */ public void leggiInfo(String file) throws FileNotFoundException { // TODO Auto-generated method stub //3.implemento la lettura da file, che non ho avuto tempo di fare in aula BufferedReader read=new BufferedReader(new FileReader(file)); String str=null; try { str=read.readLine();} catch (IOException e) { e.printStackTrace();} String tmp=null; while(str!=null){ if(str.startsWith("G")) this.definisciGranPremio(str.substring(2)); else this.creaPilota(str.substring(2)); try { str=read.readLine();} catch (IOException e1) { e1.printStackTrace();} } } }