Advertisement
divanov94

Untitled

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