107 lines
3.5 KiB
Java
107 lines
3.5 KiB
Java
package totale.p240618;
|
|
|
|
import java.util.Set;
|
|
import java.util.HashSet;
|
|
import java.util.TreeSet;
|
|
|
|
import java.util.Iterator;
|
|
import java.util.Map;
|
|
import java.util.Comparator;
|
|
import java.util.HashMap;
|
|
|
|
public class CorpoDocente {
|
|
|
|
/*
|
|
Mantiene in memoria tutta la lista docenti
|
|
*/
|
|
private TreeSet<Docente> corpoDocente = new TreeSet<Docente>(new Comparator<Docente>() {
|
|
@Override
|
|
public int compare(Docente d1, Docente d2) {
|
|
int cmp = Integer.compare(d1.anniRuolo(), d2.anniRuolo());
|
|
if (cmp != 0) return cmp;
|
|
return d1.CF().compareTo(d2.CF());
|
|
}
|
|
});
|
|
|
|
/*
|
|
Mantiene in memoria tutti i CF aggiunti in modo da rispettare unicità.
|
|
*/
|
|
private HashSet<String> cfDocenti = new HashSet<String>();
|
|
|
|
// Metodo 1
|
|
public boolean aggiungi(
|
|
String CF,
|
|
int anniRuolo,
|
|
String meccanografico
|
|
) {
|
|
// Input Check
|
|
if (CF == null || meccanografico == null) throw new NullPointerException();
|
|
if (anniRuolo < 0) throw new IllegalArgumentException();
|
|
// Se non presente, si aggiunge il CF
|
|
if (cfDocenti.contains(CF)) return false;
|
|
// Aggiunta dell'oggetto
|
|
cfDocenti.add(CF);
|
|
return corpoDocente.add(new Docente(CF, anniRuolo, meccanografico));
|
|
}
|
|
|
|
// Metodo 2
|
|
public boolean presente(String CF) {
|
|
// Input check
|
|
if (CF == null) throw new NullPointerException();
|
|
// Controllo la presenza
|
|
return cfDocenti.contains(CF);
|
|
}
|
|
|
|
// Metodo 3
|
|
public boolean cancella(String CF) {
|
|
// Input check
|
|
if (CF == null) throw new NullPointerException();
|
|
// Controllo la presenza
|
|
if (!cfDocenti.contains(CF)) return false;
|
|
// Se presente lo rimuovo
|
|
Iterator<Docente> iterator = corpoDocente.iterator();
|
|
while (iterator.hasNext()) {
|
|
Docente currentDocente = iterator.next();
|
|
if (!currentDocente.CF().equals(CF)) continue;
|
|
iterator.remove();
|
|
cfDocenti.remove(CF);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// Metodo 4
|
|
public Set<Docente> docentiInRuoloDa4Anni(String codiceMeccanografico) {
|
|
// Creazione struttura dati
|
|
HashSet<Docente> tmp = new HashSet<Docente>();
|
|
// Enumerazione con selezione dei Docenti
|
|
Iterator<Docente> it = corpoDocente.iterator();
|
|
while (it.hasNext()) {
|
|
Docente currentDocente = it.next();
|
|
if (!currentDocente.meccanografico().equals(codiceMeccanografico)) continue;
|
|
int ar = currentDocente.anniRuolo();
|
|
if (ar == 0 || ar >= 4) continue;
|
|
tmp.add(currentDocente);
|
|
}
|
|
return tmp;
|
|
}
|
|
|
|
// Metodo 5
|
|
public Map<String, Integer> docentiNonInRuoloPerScuola() {
|
|
// Creazione struttura dati
|
|
HashMap<String, Integer> tmp = new HashMap<String, Integer>();
|
|
// Aggiunta di elementi alla struttura dati
|
|
Iterator<Docente> it = corpoDocente.iterator();
|
|
while (it.hasNext()) {
|
|
Docente currentDocente = it.next();
|
|
// Se il docente non è ancora in ruolo
|
|
if (currentDocente.anniRuolo() != 0) continue;
|
|
String codiceMeccanografico = currentDocente.meccanografico();
|
|
if (tmp.get(codiceMeccanografico) == null) tmp.put(codiceMeccanografico, 1);
|
|
else tmp.put(codiceMeccanografico, tmp.get(codiceMeccanografico) + 1);
|
|
}
|
|
return tmp;
|
|
}
|
|
|
|
}
|