// Store data about the student and provide the student's interface to the // system. import java.util.*; public abstract class Student { public Student() {} public Student (String aName, DirectorOfStudies aDos) { name = aName; director = aDos; } // All students have to do 6 modules; subclasses may impose extra conditions. boolean validateChoices() { return (modules.size() == 6); } void registerForModule (Module m) { modules.add(m); m.registerStudent(this); } boolean deregisterFromModule (Module m) { m.deregisterStudent(this); return (modules.remove(m)); } // really simple menu interface for now public void interact(java.io.BufferedReader d) throws java.io.IOException { System.out.println (name + " is currently registered for:"); Iterator i = modules.iterator(); Module m; while (i.hasNext()) { m = (Module)i.next(); System.out.println (m.name); } int entry = 0; String menu = "Enter:\n" + "0 to register for a module;\n" + "1 to deregister from a module;\n" + "2 to check the acceptability of your current set of courses\n" + "3 to quit:\n"; while (entry != 3) { System.out.print(menu); entry = Integer.parseInt(d.readLine()); switch(entry) { case 0: chooseModule(d); break; case 1: unchooseModule(d); break; case 2: if (validateChoices()) { System.out.println("Valid"); } else { System.out.println("Invalid"); } break; case 3: break; default: System.out.println("Unrecognised option."); } } } public abstract void chooseModule(java.io.BufferedReader d) throws java.io.IOException; // Currently no warning if you deregister from something you're not // registered for in the first place -- silly? public void unchooseModule(java.io.BufferedReader d) throws java.io.IOException { System.out.println("Deregister from which module?"); Module m = Registry.getModule(d.readLine()); if (null != m) { if (!deregisterFromModule(m)) { System.out.println ("You weren't registered for that!"); } } else { System.out.println ("Not a module!"); } } public String name; protected HashSet modules = new HashSet(); public DirectorOfStudies director; }