// Agregacja z propagacją operacji import java.util.*; public class Agregacja { public static void main(String[] args) { Student st1 = new Student("Tomek","Kozlowski"), st2 = new Student("Marcin","Grad"), st3 = new Student("Lew","Rywin"); Grupa gr1 = new Grupa(652,3,st1), gr2 = new Grupa(332,15,st1); gr1.dodajStudenta(st2); gr1.dodajStudenta(st3); gr1.pokazStudentow(); System.out.println("\n================="); st1.pokazGrupy(); System.out.println("\n=================\nPo usunieciu..."); gr1.usunStudenta(st1); gr1.pokazStudentow(); System.out.println("\n================="); st1.pokazGrupy(); System.exit(0); } } class Student { private String imie,nazwisko; private Vector grupy = new Vector();// grupy, do których należy student public Student(String im, String nazw){ imie = im; nazwisko = nazw; } public void dodajGrupe(Grupa gr) { // dodanie nowej grupy dla studenta grupy.add(gr); } public void usunGrupe(Grupa gr) { // usunięcie grupy z listy grup studenta for (int i = 0; i < grupy.size(); i++) if(grupy.get(i) == gr) { grupy.remove(i); return; } } public String toString() { return "" + imie + " " + nazwisko; } public void pokazGrupy() {// wypisanie wszystkich grup,do których należy student System.out.println("Student " + this + " nalezy do grup:"); for (int i = 0; i < grupy.size(); i++) System.out.println(grupy.get(i)); } } class Grupa { private int nrGrupy, maxStudentow; // numer grupy; maksymalna ilość studentów private Vector studenci = new Vector(); // studenci należący do tej grupy public Grupa(int nrG, int maxS, Student st) { nrGrupy = nrG; maxStudentow = maxS; studenci.add(st); // dodanie studenta do grupy st.dodajGrupe(this);// dodanie tej grupy do listy grup studenta } public void dodajStudenta(Student st) { if(studenci.size() < maxStudentow) { // czy grupa nie jest juz przepelniona studenci.add(st); // dodanie studenta do grupy st.dodajGrupe(this); // dodanie tej grupy do listy grup studenta } else System.out.println("Grupa jest przepelniona..."); } public void usunStudenta(Student st) { // usunięcie studenta z grupy for (int i = 0; i < studenci.size(); i++) if(studenci.get(i) == st) { ((Student)(studenci.get(i))).usunGrupe(this); // usunięcie grupy z listy grup studenta studenci.remove(i); return; } } public String toString() { return "" + nrGrupy; } public void pokazStudentow() { // wypisanie wszystkich studentów tej grupy System.out.println("Do grupy " + this + " naleza studenci:"); for (int i = 0; i < studenci.size(); i++) System.out.println(studenci.get(i)); } }