TP-programmation-orientee-o.../TP09/GroupeAgenda.java

89 lines
2.3 KiB
Java
Raw Normal View History

2023-06-20 19:02:09 +00:00
import java.util.ArrayList;
public class GroupeAgenda extends AgendaAbstrait {
private ArrayList<Agenda> agendas;
public GroupeAgenda(String nom) {
super(nom);
this.agendas = new ArrayList<Agenda>();
}
public void ajouter(Agenda agenda) {
agendas.add(agenda);
}
public void ajouter(GroupeAgenda groupe) {
groupe.agendas.forEach(agenda -> {
this.ajouter(agenda);
});
}
@Override
public void enregistrer(int creneau, String rdv) throws CreneauInvalideException, IllegalArgumentException, OccupeException {
// vérif tous libres
Boolean rdv_present = false;
for (Agenda agenda : this.agendas) {
try {
agenda.getRendezVous(creneau);
rdv_present = true;
break;
} catch (LibreException e) {}
}
if (rdv_present) {
throw new OccupeException();
}
// si tous libre, enregistrer le rdv
for (Agenda agenda : this.agendas) {
try {
agenda.enregistrer(creneau, rdv);
} catch (IllegalArgumentException e) {
throw e;
} catch (OccupeException e) {
throw e;
}
}
}
@Override // TODO finir
public boolean annuler(int creneau) throws CreneauInvalideException {
Boolean rep = null;
for (Agenda agenda : this.agendas) {
rep = agenda.annuler(creneau);
}
return rep;
}
@Override
public String getRendezVous(int creneau) throws CreneauInvalideException, LibreException {
String rdv0 = this.agendas.get(0).getRendezVous(creneau);
Boolean diff = false;
Boolean one_free = false;
for (Agenda agenda : this.agendas) {
try {
String rdv = agenda.getRendezVous(creneau);
if (!rdv.equals(rdv0)) {
diff = true;
break;
}
} catch (LibreException e) {
one_free = true;
}
}
if (diff) {
return null;
} else if (one_free) {
throw new LibreException();
} else {
return rdv0;
}
}
}