/*********************************************************************/ /* An example O2 Database */ /*********************************************************************/ /* the built-in Date class */ import schema o2kit class Date; /*********************************************************************/ /* The Person class */ /*********************************************************************/ class Person inherit Object public type tuple(name: string, age: integer, DOB: Date) method public init, public isEmp: boolean end; method public init in class Person; method body init in class Person {People += unique set (self); *People2 += unique set (self); }; method public isEmp: boolean in class Person; method body isEmp in class Person {o2 Emp e = new Emp; if (e->class_of == self->class_of) return true; else return false; }; /*********************************************************************/ /* The Emp class */ /*********************************************************************/ class Emp inherit Person public type tuple(sal: integer) method public init end; method public init in class Emp; method body init in class Emp {People += unique set(self); Employees += unique set(self); }; /*********************************************************************/ /* The Student class */ /*********************************************************************/ class Student inherit Person public type tuple(gpa: real) end; /*********************************************************************/ /* The WorkStudy class */ /*********************************************************************/ class WorkStudy inherit Student, Emp public type tuple(days: list(string)) method public init end; method public init in class WorkStudy; method body init in class WorkStudy {Employees += unique set (self); }; /*********************************************************************/ /* The Department class */ /*********************************************************************/ class Department inherit Object public type tuple(name: string, num: integer, emps: set(Emp), chair: Emp, majors: set(Student)) method public view_name, public fire_emp: Emp, public set_name(input_name: string) end; method public view_name in class Department; method body view_name in class Department {display (self->name); }; method public fire_emp: Emp in class Department; method body fire_emp in class Department {int error; o2 string eName; o2 Emp theEmp, nextEmp; error = input (eName); for (nextEmp in self->emps) if (nextEmp->name == eName) theEmp = nextEmp; self->emps -= set (theEmp); return (theEmp); }; method public set_name(input_name: string) in class Department; method body set_name in class Department {self->name = input_name; }; /*********************************************************************/ /* The RichDept class */ /*********************************************************************/ class RichDept inherit Department public type tuple(budget: integer) method public fire_emp: Emp /* Note the indication of an overridden method */ modify method public fire_emp: Emp in class RichDept; end; method public fire_emp: Emp in class RichDept; method body fire_emp in class RichDept {int error; o2 string eName; o2 Emp nextEmp, theEmp; error = input (eName); for (nextEmp in self->emps) if (nextEmp->name == eName) theEmp = nextEmp; display (theEmp); self->emps -= set(theEmp); return (theEmp); }; /*********************************************************************/ /* The Company class */ /*********************************************************************/ class Company inherit Object public type tuple(name: string, address: tuple(number: integer, street: string, city: tuple(name: string, mayor: Person), zip: integer), incDate: Date) end; /*********************************************************************/ /* Some non-tuple-structured classes */ /*********************************************************************/ class UPSet inherit Object public type unique set(Person) end; class USSet inherit UPSet public type unique set(Student) end; /*********************************************************************/ /* Named, top-level DB objects */ /*********************************************************************/ name BestEmp :Emp; name Companies :list(Company); name Departments :set(Department); name Employees :unique set(Emp); name People :unique set(Person); constant name People2 :UPSet; name People3 :unique set(Person); /*********************************************************************/ /* An application program */ /*********************************************************************/ program public transfer in application Personnel; program body transfer in application Personnel { o2 Department d, oldDept, newDept; o2 string oldDname, newDname; input (oldDname); input (newDname); /* find old department */ /* find new department */ for (d in Departments) { if (d->name == oldDname) oldDept = d; if (d->name == newDname) newDept = d; } transaction; newDept->emps += set (oldDept->fire_emp); commit; };