Advertisement
Nikolcho

ivan4

Jun 27th, 2020
1,554
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class VeterinaryClinic {
  2.     constructor (clinicName, capacity) {
  3.         this.clinicName = clinicName;
  4.         this.capacity = capacity;
  5.         this.clients = [];
  6.  
  7.         this.currentWorkload = 0;
  8.         this.totalProfit = 0;
  9.     }
  10.  
  11.     newCustomer(ownerName, petName, kind, procedures) {
  12.         if (this.currentWorkload + 1 > this.capacity) {
  13.             throw new Error('Sorry, we are not able to accept more patients!');
  14.         }
  15.         let c = this.clients.find(c => c.name === ownerName);
  16.         if (c !== undefined) {
  17.             let p = c.pets.find(p => p.name === petName);
  18.             if (p !== undefined && p.procedures.length > 0) {
  19.                 throw new Error(`This pet is already registered under ${c.name} name! ${p.name} is on our lists, waiting for ${p.procedures.join(', ')}.`)
  20.             } else if (p !== undefined) {
  21.                 p.procedures = procedures;
  22.             } else {
  23.                 p = {
  24.                     name: petName,
  25.                     kind: kind,
  26.                     procedures: []
  27.                 };
  28.                 p.procedures = procedures;
  29.  
  30.                 c.pets.push(p);
  31.  
  32.                 this.currentWorkload++;
  33.             }
  34.         } else {
  35.             let newC = {
  36.                 name: ownerName,
  37.                 pets: []
  38.             };
  39.  
  40.             let p = {
  41.                 name: petName,
  42.                 kind: kind,
  43.                 procedures: []
  44.             };
  45.             p.procedures = procedures;
  46.  
  47.             newC.pets.push(p);
  48.             this.clients.push(newC);
  49.             this.currentWorkload++;
  50.         }
  51.  
  52.         return `Welcome ${petName}!`;
  53.     }
  54.  
  55.     onLeaving (ownerName, petName) {
  56.         let c = this.clients.find(c => c.name === ownerName);
  57.         if (c === undefined) {
  58.             throw new Error(`Sorry, there is no such client!`);
  59.         }
  60.         let p = c.pets.find(p => p.name === petName);
  61.         if (p === undefined || p.procedures.length === 0) {
  62.             throw new Error(`Sorry, there are no procedures for ${petName}!`);
  63.         }
  64.  
  65.         this.totalProfit += p.procedures.length * 500;
  66.         this.currentWorkload--;
  67.         p.procedures = [];
  68.  
  69.         return `Goodbye ${petName}. Stay safe!`;
  70.     }
  71.  
  72.     toString () {
  73.         let result = `${this.clinicName} is ${Math.floor((this.currentWorkload/this.capacity)*100)}% busy today!\n`;
  74.         result += `Total profit: ${this.totalProfit.toFixed(2)}$`;
  75.  
  76.         this.clients.sort((a, b) => a.name.localeCompare(b.name));
  77.         this.clients.forEach(c => {
  78.             c.pets.sort((a, b) => a.name.localeCompare(b.name));
  79.         });
  80.  
  81.         this.clients.forEach(c => {
  82.             result += `\n${c.name} with:`
  83.             c.pets.forEach(p => {
  84.                 result += `\n---${p.name} - a ${p.kind.toLowerCase()} that needs: ${p.procedures.join(', ')}`;
  85.             });
  86.         });
  87.  
  88.         return result;
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement