SHARE
TWEET

Solution 03.

svetlyoek Jun 26th, 2020 (edited) 1,116 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.totalProfit = 0;
  8.         this.currentWordLoad = 0;
  9.     }
  10.  
  11.     newCustomer(ownerName, petName, kind, procedures) {
  12.  
  13.         let currentOwner = this.clients.find(c => c.name == ownerName);
  14.  
  15.         if (currentOwner !== undefined) {
  16.  
  17.             var currentPet = currentOwner.pets.find(p => p.name == petName);
  18.         }
  19.  
  20.         if (this.currentWordLoad >= this.capacity) {
  21.  
  22.             throw new Error('Sorry, we are not able to accept more patients!');
  23.  
  24.         } else if (currentPet !== undefined && currentOwner !== undefined) {
  25.  
  26.             if (currentPet.procedures.length > 0) {
  27.  
  28.                 throw new Error(`This pet is already registered under ${currentOwner.name} name! ${currentPet.name} is on our lists, waiting for ${currentPet.procedures.map(p => `p`).join(', ')}.`);
  29.             }
  30.  
  31.         } else if (currentOwner === undefined) {
  32.  
  33.             let owner = {
  34.  
  35.                 name: ownerName,
  36.                 pets: []
  37.             };
  38.  
  39.             let pet = {
  40.  
  41.                 name: petName,
  42.                 kind: kind,
  43.                 procedures: procedures,
  44.             }
  45.  
  46.             this.currentWordLoad++;
  47.  
  48.             owner.pets.push(pet);
  49.  
  50.             this.clients.push(owner);
  51.  
  52.             return `Welcome ${petName}!`;
  53.  
  54.         } else {
  55.  
  56.             let newPet = {
  57.  
  58.                 name: petName,
  59.                 kind: kind,
  60.                 procedures: procedures,
  61.             }
  62.  
  63.             this.currentWordLoad++;
  64.  
  65.             currentOwner.pets.push(newPet);
  66.  
  67.             return `Welcome ${petName}!`;
  68.         }
  69.  
  70.     }
  71.  
  72.     onLeaving(ownerName, petName) {
  73.  
  74.         let currentOwner = this.clients.find(c => c.name == ownerName);
  75.         let currentPet = currentOwner.pets.find(p => p.name == petName);
  76.  
  77.         if (currentOwner === undefined) {
  78.  
  79.             throw new Error(`Sorry, there is no such client!`);
  80.  
  81.         } else if (currentPet === undefined || currentPet.procedures.length == 0) {
  82.  
  83.             throw new Error(`Sorry, there are no procedures for ${petName}`);
  84.  
  85.         } else {
  86.  
  87.             for (let i = 0; i < currentPet.procedures.length; i++) {
  88.  
  89.                 this.totalProfit += 500;
  90.             }
  91.  
  92.             currentPet.procedures = [];
  93.  
  94.             this.currentWordLoad--;
  95.  
  96.             return `Goodbye ${petName}. Stay safe!`;
  97.         }
  98.     }
  99.  
  100.     toString() {
  101.  
  102.         let result = [
  103.             `${this.clinicName} is ${Math.floor((this.currentWordLoad / this.capacity) * 100)}% busy today!`,
  104.             `Total profit: ${this.totalProfit.toFixed(2)}$`,
  105.             `${this.clients.sort((a, b) => a.name.localeCompare(b.name)).map(c => `${c.name} with: \n${c.pets.sort((a, b) => a.name.localeCompare(b.name)).map(p => `---${p.name} - a ${p.kind.toLowerCase()} that needs: ${p.procedures.map(pr => `${pr}`).join(', ')}`).join('\n')}`).join('\n')}`
  106.         ]
  107.  
  108.         return result.join('\n');
  109.     }
  110. }
  111.  
  112. let clinic = new VeterinaryClinic('SoftCare', 10);
  113. console.log(clinic.newCustomer('Jim Jones', 'Tom', 'Cat', ['A154B', '2C32B', '12CDB']));
  114. console.log(clinic.newCustomer('Anna Morgan', 'Max', 'Dog', ['SK456', 'DFG45', 'KS456']));
  115. console.log(clinic.newCustomer('Jim Jones', 'Tiny', 'Cat', ['A154B']));
  116. console.log(clinic.onLeaving('Jim Jones', 'Tiny'));
  117. console.log(clinic.toString());
  118. clinic.newCustomer('Jim Jones', 'Sara', 'Dog', ['A154B']);
  119. console.log(clinic.toString());
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top