Advertisement
avaglarov

Untitled

Jan 2nd, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.41 KB | None | 0 0
  1. Array.prototype.getRandom = function() {
  2. const randomIndex = Math.floor(Math.random() * this.length);
  3. return this[randomIndex];
  4. }
  5.  
  6. const BEDS_PER_ROOM = 3;
  7. const ROOMS_CAPACITY = 10;
  8. const NEED_DAYS_FOR_HEALING = 3;
  9.  
  10. class Hospital {
  11. constructor(name) {
  12. this.name = name;
  13. this.doctors = [];
  14. this.nurses = [];
  15. this.patients = [];
  16. this.departments = [];
  17. }
  18.  
  19. addNewDepartment(department) {
  20. if (department instanceof Department) {
  21. this.departments.push(department);
  22. department.hospital = this;
  23. } else {
  24. throw new Error('invalid department input!');
  25. }
  26. }
  27.  
  28. hireDoctor(doctor) {
  29. this.doctors.push(doctor);
  30. doctor.hospital = this;
  31. }
  32.  
  33. setDoctorsToDepartments() {
  34. this.departments.forEach(department => {
  35. if (department instanceof Ortopediq) {
  36. department.doctors.push(this.doctors.filter(doctor => doctor instanceof Ortoped));
  37. }
  38.  
  39. if (department instanceof Kardiologiq) {
  40. department.doctors.push(this.doctors.filter(doctor => doctor instanceof Kardiolog));
  41. }
  42.  
  43. if (department instanceof Virusologiq) {
  44. department.doctors.push(this.doctors.filter(doctor => doctor instanceof Virusolog));
  45. }
  46. });
  47. }
  48.  
  49. hireNurse(nurse) {
  50. this.doctors.push(nurse);
  51. nurse.hospital = this;
  52. }
  53.  
  54. recievePatient(patient) {
  55. this.patients.push(patient); // priema pacienta
  56. patient.hospital = this;
  57.  
  58. const randomDoctor = this.doctors.getRandom(); //zachislqva mu se doktor
  59. patient.healingDoctor = randomDoctor;
  60. randomDoctor.patients.push(patient);
  61.  
  62. randomDoctor.makePlanForHealing(patient); //doktora dava diagnoza
  63.  
  64. randomDoctor.department.acceptPatient(patient); //pacienta se priema v otdelenie i staq
  65.  
  66. console.log('Patient' + patient.name + ' gender: ' + patient.sex + ' is hospitalized with diagnoses '
  67. + patient.diagnosis + ' Healing Doctor: ' + randomDoctor.name);
  68. }
  69.  
  70. startNewDay() {
  71. this.patients.forEach(patient => { //proverqva dali pacienta e za izpisvane
  72. if (patient.daysOfHealing >= Math.floor(Math.random() * 2) + 3) {
  73. console.log('Patient ' + patient.name + ' with diagnosis: ' + patient.diagnosis
  74. + ' was sign out from: ' + patient.hospital.name + ' hospital');
  75.  
  76. this.patients.splice(indexOf(patient), 1); // pacienta se izpisva
  77. patient.healingDoctor.signOutPatient(patient);
  78. patient.healingDoctor.department.releasePatient(patient);
  79.  
  80. } else {
  81. const randomNurse = this.nurses.getRandom(); //med. sestra dava lekartvo
  82. console.log('Nurse ' + randomNurse.name + ' give to ' + patient.name
  83. + patient.medicine + ' in room number ' + patient.room.numberOfRoom +
  84. ' in department ' + patient.room.department);
  85.  
  86. console.log('Doctor ' + patient.healingDoctor + ' has visited patient ' //vizitaciq
  87. + patient.name + ' in room number ' + patient.room.numberOfRoom +
  88. ' in department ' + patient.room.department);
  89.  
  90. patient.daysOfHealing++;
  91. }
  92. });
  93. }
  94.  
  95. printPatientsPerDoctor() {
  96. this.doctors.forEach(doctor =>
  97. console.log(doctor.name + ' : ' + doctor.patients.length + ' patients'));
  98. }
  99.  
  100. printPatientsReadyToGoHome() {
  101. this.patients.forEach(patient => {
  102. if (patient.daysOfHealing >= NEED_DAYS_FOR_HEALING) {
  103. console.log(patient.name);
  104. }
  105. });
  106. }
  107. }
  108.  
  109. /**
  110. * options for departments - Ortopediq, Kardiologiq, Virusologiq
  111. */
  112.  
  113. class Department {
  114. constructor() {
  115. this.rooms = [];
  116. this.doctors = [];
  117. }
  118.  
  119. addRoom(room) {
  120. if (this.rooms.length < ROOMS_CAPACITY) {
  121. this.rooms.push(room);
  122. room.department = this;
  123. }
  124. }
  125.  
  126. acceptPatient(patient) {
  127. this.rooms.forEach(room => {
  128. if (room.patients.length > 0) {
  129. if (room.numberOfFreeBeds > 0 && patient.sex === room.patients[0].sex) {
  130. room.patients.push(patient);
  131. patient.room = room;
  132. room.numberOfFreeBeds--;
  133. break;
  134. } else {
  135. console.log('not enough free beds in room ' + room.numberOfRoom
  136. + ' department: ' + this.name);
  137. }
  138. } else {
  139. room.patients.push(patient);
  140. patient.room = room;
  141. room.numberOfFreeBeds--;
  142. }
  143. });
  144. }
  145.  
  146. releasePatient(patient) {
  147. patient.room.patients.splice(indexOf(patient), 1);
  148. }
  149.  
  150. }
  151.  
  152. class Ortopediq extends Department {
  153. constructor() {
  154. this.name = 'ortopediq'
  155. }
  156. }
  157.  
  158. class Kardiologiq extends Department {
  159. constructor() {
  160. this.name = 'kardiologiq'
  161. }
  162. }
  163.  
  164. class Virusologiq extends Department {
  165. constructor() {
  166. this.name = 'virusologiq'
  167. }
  168. }
  169.  
  170. class Room {
  171. constructor(numberOfRoom) {
  172. this.numberOfRoom = numberOfRoom;
  173. this.numberOfFreeBeds = BEDS_PER_ROOM;
  174. this.patients = [];
  175. }
  176. }
  177.  
  178.  
  179. class Doctor {
  180. constructor(name, tel) {
  181. super(name, tel);
  182. this.patients = [];
  183. }
  184.  
  185. signOutPatient(patient) {
  186. this.patients.splice(indexOf(patient), 1);
  187. }
  188.  
  189. makePlanForHealing(patient) {
  190. if (patient.healingDoctor instanceof Ortoped) {
  191. patient.diagnosis = 'schupvane';
  192. patient.medicine = ['gips', 'bint', 'pironi'];
  193. }
  194.  
  195. if (patient.healingDoctor instanceof Kardiolog) {
  196. patient.diagnosis = 'infarkt';
  197. patient.medicine = ['magnezii', 'aspirin', 'acetizal'];
  198. }
  199.  
  200. if (patient.healingDoctor instanceof Virusolog) {
  201. patient.diagnosis = 'kashlica';
  202. patient.medicine = ['trahizan', 'tantum', 'chesyn'];
  203. }
  204. }
  205. }
  206.  
  207. class Ortoped extends Doctor {
  208. constructor(name, tel) {
  209. super(name, tel);
  210. this.specialization = 'ortoped';
  211. this.department = null;
  212. }
  213. }
  214.  
  215. class Kardiolog extends Doctor {
  216. constructor(name, tel) {
  217. super(name, tel);
  218. this.specialization = 'kardiolog';
  219. this.department = null;
  220. }
  221. }
  222.  
  223. class Virusolog extends Doctor {
  224. constructor(name, tel) {
  225. super(name, tel);
  226. this.specialization = 'virusolog';
  227. this.department = null;
  228. }
  229. }
  230.  
  231. class Nurse extends Person {
  232. constructor(name, tel, experience) {
  233. super(name, tel);
  234. this.experience = experience;
  235. }
  236.  
  237. giveMedicine(patient) {
  238. console.log(this.name + ' gave medicine to ' + patient.name);
  239. }
  240. }
  241.  
  242. class Patient extends Person {
  243. constructor(name, tel, age, sex) {
  244. super(name, tel);
  245. this.age = age;
  246. this.sex = sex;
  247. this.diagnosis = '';
  248. this.daysOfHealing = 0;
  249. this.healingDoctor = null;
  250. }
  251.  
  252. }
  253.  
  254.  
  255.  
  256.  
  257. var bolnica = new Hospital('Sofia MED');
  258.  
  259. var ortopedia = new Ortopediq();
  260. var staq = new Room(1);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement