Commit iniziale

This commit is contained in:
blackilykat 2024-12-17 18:08:16 +01:00
commit 7e467b8f0d
9 changed files with 244 additions and 0 deletions

144
Classe.java Normal file
View File

@ -0,0 +1,144 @@
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;
}
}

19
Data.java Normal file
View File

@ -0,0 +1,19 @@
public class Data {
public final int giorno;
public final int mese;
public final int anno;
public Data(int giorno, int mese, int anno) {
this.giorno = giorno;
this.mese = mese;
this.anno = anno;
}
@Override
public boolean equals(Object other) {
if(!(other instanceof Data altra)) return false;
return this.giorno == altra.giorno
&& this.mese == altra.mese
&& this.anno == altra.anno;
}
}

15
Insegnante.java Normal file
View File

@ -0,0 +1,15 @@
public class Insegnante extends Personale {
public final Materia materia;
public Classe classeCoordinata;
public Insegnante(String cognome, int anzianita, Materia materia) {
this.cognome = cognome;
this.anzianita = anzianita;
this.materia = materia;
}
public void firmaRegistro(Data giorno) {
registraPresenza(giorno);
}
}

8
Materia.java Normal file
View File

@ -0,0 +1,8 @@
public class Materia {
public final String nome;
public Materia(String nome) {
this.nome = nome;
}
}

14
Persona.java Normal file
View File

@ -0,0 +1,14 @@
import java.util.HashMap;
import java.util.Map;
public abstract class Persona {
protected Map<Data, Boolean> presenze = new HashMap<>();
public boolean registratoPresenza(Data giorno) {
return presenze.getOrDefault(giorno, false);
}
protected void registraPresenza(Data giorno) {
presenze.put(giorno, true);
}
}

8
Personale.java Normal file
View File

@ -0,0 +1,8 @@
public abstract class Personale extends Persona {
protected String cognome;
public int anzianita;
public String getCognome() {
return cognome;
}
}

10
PersonaleATA.java Normal file
View File

@ -0,0 +1,10 @@
public class PersonaleATA extends Personale {
public PersonaleATA(String cognome, int anzianita) {
this.cognome = cognome;
this.anzianita = anzianita;
}
public void passCartellino(Data giorno) {
registraPresenza(giorno);
}
}

26
Studente.java Normal file
View File

@ -0,0 +1,26 @@
public class Studente extends Persona {
public Materia[] materieStudiate;
private String nome;
public Classe classe;
public boolean rappresentante;
public Studente(String nome) {
this.nome = nome;
}
public void passaBadge(Data giorno) {
registraPresenza(giorno);
}
public String getNome() {
return nome;
}
public Classe getClasse() {
return classe;
}
public boolean getRappresentante() {
return rappresentante;
}
}

BIN
uml.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 145 KiB