I have the following 2 sets in my driver class. How do I attach a student to a module? For example, I need jane on module ufce1 and ufce2 and alex on ufce2 only.
Student jane = new Student("jane");
Student alex = new Student("alex");
Set<Student> students = new HashSet<Object>();
students.add(jane);
students.add(alex);
Module ufce1 = new Module("UFCE1");
Module ufce2 = new Module("UFCE2");
Set<Module> modules = new HashSet<Object>();
modules.add(ufce1);
modules.add(ufce2);
Modules class:
public class Module {
private String name;
public Module(String name) {
this.name = name;
}
public String getName() {
return name;
}
@Override
public String toString() {
return name;
}
}
Student Class:
public class Student {
private String name;
public Student(String name) {
this.name = name;
}
public String getName() {
return name;
}
@Override
public String toString() {
return name;
}
}
I tried using arrayLists but these result in duplication i.e. if a user tries adding a student onto a module they are already enrolled onto.
Module m1 = new Module("m1", "Java");
Module m2 = new Module("m2", "Software Design");
Module m3 = new Module("m3", "PHP");
Module m4 = new Module("m4", "MySQL");
Module m5 = new Module("m5", "XML");
ArrayList<Module> modules = new ArrayList<Module>();
modules.add(m5);
modules.add(m4);
modules.add(m3);
modules.add(m2);
modules.add(m1);
jane.addModule(m1);
jane.addModule(m3);
alex.addModule(m1);
alex.addModule(m2);
m1.addStudent(jane);
m3.addStudent(jane);
m1.addStudent(alex);
m2.addStudent(alex);
I assume Module is a class written by you.
if so you can have an Set in the Module class and also add addStudent() method in it.