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 corpoDocente = new TreeSet(new Comparator() { @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 cfDocenti = new HashSet(); // 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 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 docentiInRuoloDa4Anni(String codiceMeccanografico) { // Creazione struttura dati HashSet tmp = new HashSet(); // Enumerazione con selezione dei Docenti Iterator 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 docentiNonInRuoloPerScuola() { // Creazione struttura dati HashMap tmp = new HashMap(); // Aggiunta di elementi alla struttura dati Iterator 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; } }