commit 7e467b8f0d5e299d72cfe69207a73b4003519c44 Author: blackilykat Date: Tue Dec 17 18:08:16 2024 +0100 Commit iniziale diff --git a/Classe.java b/Classe.java new file mode 100644 index 0000000..2cc042d --- /dev/null +++ b/Classe.java @@ -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; + } +} diff --git a/Data.java b/Data.java new file mode 100644 index 0000000..374e45e --- /dev/null +++ b/Data.java @@ -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; + } +} diff --git a/Insegnante.java b/Insegnante.java new file mode 100644 index 0000000..faa8e5c --- /dev/null +++ b/Insegnante.java @@ -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); + } + +} diff --git a/Materia.java b/Materia.java new file mode 100644 index 0000000..fea5dc6 --- /dev/null +++ b/Materia.java @@ -0,0 +1,8 @@ +public class Materia { + public final String nome; + + public Materia(String nome) { + this.nome = nome; + } + +} diff --git a/Persona.java b/Persona.java new file mode 100644 index 0000000..021ee70 --- /dev/null +++ b/Persona.java @@ -0,0 +1,14 @@ +import java.util.HashMap; +import java.util.Map; + +public abstract class Persona { + protected Map presenze = new HashMap<>(); + + public boolean registratoPresenza(Data giorno) { + return presenze.getOrDefault(giorno, false); + } + + protected void registraPresenza(Data giorno) { + presenze.put(giorno, true); + } +} diff --git a/Personale.java b/Personale.java new file mode 100644 index 0000000..92dfd81 --- /dev/null +++ b/Personale.java @@ -0,0 +1,8 @@ +public abstract class Personale extends Persona { + protected String cognome; + public int anzianita; + + public String getCognome() { + return cognome; + } +} diff --git a/PersonaleATA.java b/PersonaleATA.java new file mode 100644 index 0000000..3283cd4 --- /dev/null +++ b/PersonaleATA.java @@ -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); + } +} diff --git a/Studente.java b/Studente.java new file mode 100644 index 0000000..eb107e2 --- /dev/null +++ b/Studente.java @@ -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; + } +} diff --git a/uml.png b/uml.png new file mode 100644 index 0000000..ac32ba9 Binary files /dev/null and b/uml.png differ