Advertisement
svetlyoek

Solution 03.

Jun 26th, 2020
1,230
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.         this.totalProfit = 0;
  7.     }
  8.  
  9.     newCustomer(ownerName, petName, kind, procedures) {
  10.  
  11.         let currentOwner = this.clients.find(c => c.name == ownerName);
  12.  
  13.         if (currentOwner !== undefined) {
  14.  
  15.             var currentPet = currentOwner.pets.find(p => p.name == petName);
  16.         }
  17.  
  18.         if (this.clients.length >= this.capacity) {
  19.  
  20.             throw new Error('Sorry, we are not able to accept more patients!');
  21.  
  22.         } else if (currentPet !== undefined && currentOwner !== undefined) {
  23.  
  24.             if (currentPet.procedures.length > 0) {
  25.  
  26.                 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(', ')}.`);
  27.             }
  28.  
  29.         } else if (currentOwner === undefined) {
  30.  
  31.             let owner = {
  32.  
  33.                 name: ownerName,
  34.                 pets: []
  35.             };
  36.  
  37.             let pet = {
  38.  
  39.                 name: petName,
  40.                 kind: kind,
  41.                 procedures: procedures,
  42.             }
  43.  
  44.             owner.pets.push(pet);
  45.  
  46.             this.clients.push(owner);
  47.  
  48.             return `Welcome ${petName}!`;
  49.  
  50.         } else {
  51.  
  52.             let newPet = {
  53.  
  54.                 name: petName,
  55.                 kind: kind,
  56.                 procedures: procedures,
  57.             }
  58.             currentOwner.pets.push(newPet);
  59.  
  60.             return `Welcome ${petName}!`;
  61.         }
  62.  
  63.     }
  64.  
  65.     onLeaving(ownerName, petName) {
  66.  
  67.         let currentOwner = this.clients.find(c => c.name == ownerName);
  68.         let currentPet = currentOwner.pets.find(p => p.name == petName);
  69.  
  70.         if (currentOwner === undefined) {
  71.  
  72.             throw new Error(`Sorry, there is no such client!`);
  73.  
  74.         } else if (currentPet === undefined || currentPet.procedures.length == 0) {
  75.  
  76.             throw new Error(`Sorry, there are no procedures for ${petName}`);
  77.  
  78.         } else {
  79.  
  80.             for (let i = 0; i < currentPet.procedures.length; i++) {
  81.  
  82.                 this.totalProfit += 500;
  83.             }
  84.  
  85.             currentPet.procedures = [];
  86.  
  87.             return `Goodbye ${petName}. Stay safe!`;
  88.         }
  89.     }
  90.  
  91.     toString() {
  92.  
  93.         let finalPets = this.clients.map(c => c.pets.filter(p => p.length > 0));
  94.         let result = [
  95.             `${this.clinicName} is ${Math.floor((finalPets.length / this.capacity) * 100)}% busy today!`,
  96.             `Total profit: ${this.totalProfit.toFixed(2)}$`,
  97.             `${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('\n')}`).join('\n')}`
  98.         ]
  99.  
  100.         return result.join('\n');
  101.     }
  102. }
  103.  
  104. let clinic = new VeterinaryClinic('SoftCare', 10);
  105. console.log(clinic.newCustomer('Jim Jones', 'Tom', 'Cat', ['A154B', '2C32B', '12CDB']));
  106. console.log(clinic.newCustomer('Anna Morgan', 'Max', 'Dog', ['SK456', 'DFG45', 'KS456']));
  107. console.log(clinic.newCustomer('Jim Jones', 'Tiny', 'Cat', ['A154B']));
  108. console.log(clinic.onLeaving('Jim Jones', 'Tiny'));
  109. console.log(clinic.toString());
  110. clinic.newCustomer('Jim Jones', 'Sara', 'Dog', ['A154B']);
  111. console.log(clinic.toString());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement