public class Classe { private Studente[] studenti = new Studente[0]; private Studente[] rappresentanti = new Studente[0]; private Insegnante coordinatore = null; public void aggiungiStudente(Studente studente) { if(this.studenti.length >= 4) { throw new IllegalStateException("Non ci possono essere più di 4 studenti in una classe"); } if(studente.classe != null) { throw new IllegalArgumentException("Lo studente " + studente.getNome() + " è già in una classe"); } Studente[] vecchiaArray = this.studenti; this.studenti = new Studente[vecchiaArray.length + 1]; // si potrebbe usare System.arraycopy per copiare i valori nell'array più velocemente for(int i = 0; i < vecchiaArray.length; i++) { this.studenti[i] = vecchiaArray[i]; } // vecchiaArray.length == studenti.length - 1 this.studenti[vecchiaArray.length] = studente; studente.classe = this; } public void rimuoivStudente(Studente studente) { if(this.studenti.length <= 2) { throw new IllegalStateException("Non ci possono essere meno di 2 studenti in una classe"); } int indice = -1; for(int i = 0; i < this.studenti.length; i++) { if(this.studenti[i] == studente) { indice = i; break; } } if(indice == -1) { throw new IllegalArgumentException("Lo studente " + studente.getNome() + " non è presente nella classe"); } Studente[] vecchiaArray = this.studenti; this.studenti = new Studente[this.studenti.length - 1]; for(int i = 0; i < this.studenti.length; i++) { this.studenti[i] = vecchiaArray[i >= indice ? i+1 : i]; } studente.classe = null; } public void setCoordinatore(Insegnante insegnante) { if(insegnante.classeCoordinata != null && insegnante.classeCoordinata != this) { throw new IllegalArgumentException("L'insegnante " + insegnante.getCognome() + " è già coordinatore di un'altra classe"); } if(this.coordinatore != null) { this.coordinatore.classeCoordinata = null; } this.coordinatore = insegnante; insegnante.classeCoordinata = this; } public Insegnante getCoordinatore() { return this.coordinatore; } public Studente[] getStudenti() { // creo una copia per evitare che l'array venga modificato inaspettatamente Studente[] copia = new Studente[this.studenti.length]; for(int i = 0; i < this.studenti.length; i++) { copia[i] = this.studenti[i]; } return copia; } public Studente[] getRappresentanti() { Studente[] copia = new Studente[this.rappresentanti.length]; for(int i = 0; i < this.rappresentanti.length; i++) { copia[i] = this.rappresentanti[i]; } return copia; } public void aggiungiRappresentante(Studente studente) { if(studente.rappresentante) { throw new IllegalArgumentException("Lo studente è già un rappresentante"); } boolean presente = false; for(int i = 0; i < this.studenti.length; i++) { if(this.studenti[i] == studente) { presente = true; break; } } if(!presente) { throw new IllegalArgumentException("Lo studente " + studente.getNome() + " non è nella classe"); } Studente[] vecchiaArray = this.rappresentanti; this.rappresentanti = new Studente[vecchiaArray.length + 1]; for(int i = 0; i < vecchiaArray.length; i++) { this.rappresentanti[i] = vecchiaArray[i]; } this.rappresentanti[vecchiaArray.length] = studente; studente.rappresentante = true; } public void rimuoviRappresentante(Studente studente) { if(!studente.rappresentante) { throw new IllegalArgumentException("Lo studente non è un rappresentante"); } int indice = -1; for(int i = 0; i < this.rappresentanti.length; i++) { if(this.rappresentanti[i] == studente) { indice = i; break; } } if(indice == -1) { throw new IllegalArgumentException("Lo studente " + studente.getNome() + " non è rappresentante di questa classe"); } Studente[] vecchiaArray = this.rappresentanti; this.rappresentanti = new Studente[vecchiaArray.length - 1]; for(int i = 0; i < this.rappresentanti.length; i++) { this.rappresentanti[i] = vecchiaArray[i >= indice ? i+1 : i]; } studente.rappresentante = false; } }