Advertisement
kstoyanov

03. Veterinary Clinic JS Advanced Exam - 27 June 2020

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