diff --git a/registrar/src/registrar/Main.java b/registrar/src/registrar/Main.java index 1206754..fcedc41 100644 --- a/registrar/src/registrar/Main.java +++ b/registrar/src/registrar/Main.java @@ -6,25 +6,128 @@ class Examples { ILo testList = new Cons( 1, - new Cons(2, new Mt()) + new Cons(2, new Cons(3, new Mt())) ); - void testTest(Tester t) { - t.checkExpect(1, 1); + void testILoUniqAppend(Tester t) { + t.checkExpect(testList.uniqAppend(2), testList); + t.checkExpect( + testList.uniqAppend(4), + new Cons( + 1, + new Cons( + 2, + new Cons( + 3, + new Cons(4, new Mt()) + ) + ) + ) + ); } } -interface ILo {} +class School { -class Cons implements ILo { + static int cid = 0; - T first; - ILo rest; + // Returns the current ID, then increments it for the next time. + static int getThenInc() { + return School.cid++; + } +} - Cons(T first, ILo rest) { +class Course { + + String name; + Teacher teacher; + ILo students; + + Course(String name, Teacher teacher, ILo students) { + this.name = name; + this.teacher = teacher; + this.teacher.assignCourse(this); + this.students = students; + } + + // Convenience constructor for initial course construction. + Course(String name, Teacher teacher) { + this(name, teacher, new Mt()); + } +} + +class Teacher { + + String name; + ILo courses; + + Teacher(String name, ILo courses) { + this.name = name; + this.courses = courses; + } + + // Convenience constructor for initial teacher construction. + Teacher(String name) { + this(name, new Mt()); + } + + // Effect: Assigns the given Course to this Teacher. + void assignCourse(Course course) { + this.courses.uniqAppend(course); + } + + // Effect: Enrolls the given Student in this Course. + void enroll(Student student) {} +} + +class Student { + + String name; + int id; + ILo courses; + + Student(String name, int id, ILo courses) { + this.name = name; + this.id = id; + this.courses = courses; + } + + // Convenience constructor for initial student construction. + Student(String name) { + this(name, School.cid, new Mt()); + } + + // Effect: Enrolls this student in the given course. + void enroll(Course course) { + course.enroll(this); + this.courses.uniqAppend(course); + } +} + +interface ILo { + // Uniquely append an element (i.e., leaves list unchanged if element is already present.) + ILo uniqAppend(A a); +} + +class Cons implements ILo { + + A first; + ILo rest; + + Cons(A first, ILo rest) { this.first = first; this.rest = rest; } + + public ILo uniqAppend(A a) { + if (this.first.equals(a)) return this; + else return new Cons(this.first, this.rest.uniqAppend(a)); + } } -class Mt implements ILo {} +class Mt implements ILo { + + public ILo uniqAppend(A a) { + return new Cons(a, this); + } +}