Advertisement
Guest User

Untitled

a guest
Jan 15th, 2015
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 3.19 KB | None | 0 0
  1. import std.stdio;
  2. import std.string;
  3.  
  4. class School {
  5.     Classroom[] classes;
  6.  
  7.     void addClass(ref Classroom classroom) {
  8.         //classroom.school = this;
  9.         this.classes ~= classroom;
  10.     }
  11.  
  12.     void invite(string className, ref Teacher teacher) {
  13.         Classroom classroom = getClassByName(className);
  14.         if (classroom !is null)
  15.             classroom.teachers ~= teacher;
  16.     }
  17.  
  18.     void invite(string className, ref Student student) {
  19.         Classroom classroom = getClassByName(className);
  20.         if (classroom !is null)
  21.             classroom.students ~= student;
  22.     }
  23.  
  24.     void invite(string className, Teacher[] teachers) {
  25.         foreach(ref teacher; teachers) {
  26.             invite(className, teacher);
  27.         }
  28.     }
  29.  
  30.     void invite(string className, Student[] students) {
  31.         foreach(ref student; students) {
  32.             invite(className, student);
  33.         }
  34.     }
  35.  
  36.     Classroom getClassByName(string className) {
  37.         if (isDefined(className)) {
  38.             foreach(classroom; this.classes) {
  39.                 if (classroom.name == className)
  40.                     return classroom;
  41.             }
  42.         }
  43.         return null;
  44.     }
  45.  
  46.     Classroom newClassroom(string name, ref Teacher teacher) {
  47.         auto classroom = new Classroom(name, teacher);
  48.         addClass(classroom);
  49.         return classroom;
  50.     }
  51.  
  52.     bool isDefined(string className) {
  53.         foreach(classroom; this.classes) {
  54.             if (classroom.name == className)
  55.                 return true;
  56.         }
  57.         return false;
  58.     }
  59.  
  60.     void printClasses() {
  61.         foreach(classroom; this.classes) {
  62.             writeln(classroom);
  63.         }
  64.     }
  65. }
  66.  
  67. class Teacher: User {
  68.     string lesson;
  69.  
  70.     this(string firstName, string lastName, short age, string lesson) {
  71.         super(firstName, lastName, age);
  72.         this.role = "Teacher";
  73.         this.lesson = lesson;
  74.     }
  75. }
  76.  
  77. class Student: User {
  78.     this(string firstName, string lastName, short age) {
  79.         super(firstName, lastName, age);
  80.         this.role = "Student";
  81.     }
  82. }
  83.  
  84. class Classroom {
  85.     private School _school;
  86.     string name;
  87.  
  88.     Teacher formMaster;
  89.     Teacher[] teachers;
  90.     Student[] students;
  91.  
  92.     this(string name, ref Teacher teacher) {
  93.         this.name = name;
  94.         this.formMaster = teacher;
  95.     }
  96.  
  97.     @property void school(ref School school) {
  98.         this._school = school;
  99.     }
  100.  
  101.     override string toString() const {
  102.         string text = format("Class %s. Teacher %s. %d students: \n", this.name, this.formMaster.name, this.students.length);
  103.         foreach(student; this.students) {
  104.             text ~= "\t" ~ student.toString() ~ "\n";
  105.         }
  106.         return text;
  107.     }
  108. }
  109.  
  110. class User {
  111.     Classroom classroom;
  112.     string firstName;
  113.     string lastName;
  114.     string role;
  115.     short age;
  116.  
  117.     this(string firstName, string lastName, short age) {
  118.         this.firstName = firstName;
  119.         this.lastName = lastName;
  120.         this.age = age;
  121.     }
  122.  
  123.     @property string name() const {
  124.             return format("%s %s", this.firstName, this.lastName);
  125.     }
  126.  
  127.     override string toString() const {
  128.         return format("%s %s. Age %d.", this.role, this.name, this.age);
  129.     }
  130. }
  131.  
  132. void main() {
  133.     auto students = [
  134.         new Student("Henry", "El", 16),
  135.         new Student("Nicol", "Mi", 16),
  136.         new Student("Leo", "Ef", 11)
  137.     ];
  138.    
  139.     auto teacher = new Teacher("Marry", "Elezabet", 43, "Russian Language");
  140.  
  141.     auto school = new School();
  142.     school.newClassroom("10A", teacher);
  143.     school.newClassroom("6B", teacher);
  144.  
  145.     school.invite("10A", students[0..2]);
  146.     school.invite("6B", students[2]);
  147.  
  148.     school.printClasses();
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement