Ivankooo1

vetClinic - Papazov

Oct 20th, 2020
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class VeterinaryClinic {
  2.     clients = [];
  3.    
  4.     constructor (clinicName, capacity) {
  5.         this.clinicName = clinicName;
  6.         this.capacity = capacity;
  7.         this.currentWorkload = 0;
  8.         this.totalProfit = 0;
  9.     }
  10.  
  11.     getPet(owner, petName) {
  12.         if (!owner) {
  13.             return;
  14.         }
  15.  
  16.         return owner.pets.find(x => x.petName == petName);
  17.     }
  18.  
  19.     getClient(ownerName) {
  20.         let client = this.clients.find(x => x.ownerName == ownerName);
  21.  
  22.         return client;
  23.     }
  24.  
  25.     newCustomer(ownerName, petName, kind, procedures) {
  26.         if (this.currentWorkload >= this.capacity) {
  27.             throw new Error('Sorry, we are not able to accept more patients!');
  28.         }
  29.  
  30.         // TODO: Check if currently in clinic with procedures
  31.         let currentOwner = this.getClient(ownerName);
  32.         let currentPet = this.getPet(currentOwner, petName);
  33.         if (currentOwner && currentPet) {
  34.             if (currentPet.procedures.length > 0) {
  35.                 throw new Error(`This pet is already registered under ${currentOwner.ownerName} name! ${currentPet.petName} is on our lists, waiting for ${currentPet.procedures.join(', ')}.`);
  36.             } else {
  37.                 currentPet.procedures = procedures
  38.             }
  39.         } else if (!currentOwner) {
  40.             currentOwner = {
  41.                 ownerName,
  42.                 pets: [],
  43.             };
  44.  
  45.             this.clients.push(currentOwner);
  46.         }
  47.  
  48.         // Add pet to owner
  49.         currentOwner.pets.push({
  50.             petName,
  51.             kind,
  52.             procedures,
  53.         });
  54.  
  55.         // Modify workload
  56.         this.currentWorkload++;
  57.  
  58.         // return welcome message
  59.         return `Welcome ${petName}!`;
  60.     }
  61.  
  62.     onLeaving (ownerName, petName) {
  63.         let currentOwner = this.getClient(ownerName);
  64.  
  65.         if (!currentOwner) {
  66.             throw new Error('Sorry, there is no such client!');
  67.         }
  68.  
  69.         let currentPet = this.getPet(currentOwner, petName);
  70.  
  71.         if (!currentPet || currentPet.procedures.length == 0) {
  72.             throw new Error(`Sorry, there are no procedures for ${petName}!`);
  73.         }
  74.  
  75.         // Add new price 500.00
  76.         this.totalProfit += currentPet.procedures.length * 500;
  77.  
  78.         // update workload
  79.         this.currentWorkload--;
  80.  
  81.         // clear procedures of the current pet
  82.         currentPet.procedures = [];
  83.  
  84.         // return
  85.         return `Goodbye ${currentPet.petName}. Stay safe!`;
  86.     }
  87.  
  88.     toString() {
  89.         let busyPercentage = Math.floor(this.currentWorkload / this.capacity * 100);
  90.        
  91.         let result = `${this.clinicName} is ${busyPercentage}% busy today!`;
  92.         result += '\n';
  93.         result += `Total profit: ${this.totalProfit.toFixed(2)}$`;
  94.  
  95.         this.clients.sort((a, b) => a.ownerName.localeCompare(b.ownerName));
  96.  
  97.         for (const client of this.clients) {
  98.             client.pets.sort((a, b) => a.petName.localeCompare(b.petName));
  99.            
  100.             result += '\n';
  101.             result += `${client.ownerName} with:`;
  102.  
  103.             for (const pet of client.pets) {
  104.                 result += '\n';
  105.                 result += `---${pet.petName} - a ${pet.kind.toLowerCase()} that needs: ${pet.procedures.join(', ')}`
  106.             }
  107.         }
  108.  
  109.         return result.trim();
  110.     }
  111. }
  112.  
  113. // let VeterinaryClinic = result;
  114. // let clinic = new VeterinaryClinic('SoftCare', 10);
  115. // console.log(clinic.newCustomer('Jim Jones', 'Tom', 'Cat', ['A154B', '2C32B', '12CDB']));
  116. // console.log(clinic.onLeaving('Jim Jones', 'Tom'));
  117. // console.log(clinic.newCustomer('Jim Jones', 'Tom', 'Cat', ['A154B', '2C32B', '12CDB']));
  118. // console.log(clinic.newCustomer('Anna Morgan', 'Max', 'Dog', ['SK456', 'DFG45', 'KS456']));
  119. // console.log(clinic.newCustomer('Jim Jones', 'Tiny', 'Cat', ['A154B']));
  120. // console.log(clinic.onLeaving('Jim Jones', 'Tiny'));
  121. // console.log(clinic.toString());
  122. // clinic.newCustomer('Jim Jones', 'Sara', 'Dog', ['A154B']);
  123. // console.log(clinic.toString());
  124.  
  125. let clinic = new VeterinaryClinic('SoftCare', 10);
  126. console.log(clinic.newCustomer('Jim Jones', 'Tom', 'Cat', ['A154B', '2C32B', '12CDB']));
  127. // console.log(clinic.newCustomer('Anna Morgan', 'Max', 'Dog', ['SK456', 'DFG45', 'KS456']));
  128. // console.log(clinic.newCustomer('Jim Jones', 'Tiny', 'Cat', ['A154B']));
  129. console.log(clinic.onLeaving('Jim Jones', 'Tiny'));
  130. // console.log(clinic.toString());
  131. // clinic.newCustomer('Jim Jones', 'Sara', 'Dog', ['A154B']);
  132. // console.log(clinic.toString());
  133.  
  134.  
  135. // Welcome Tom!
  136. // Welcome Max!
  137. // Welcome Tiny!
  138. // Goodbye Tiny. Stay safe!
  139. // SoftCare is 20% busy today!
  140. // Total profit: 500.00$
  141. // Anna Morgan with:
  142. // ---Max - a dog that needs: SK456, DFG45, KS456
  143. // Jim Jones with:
  144. // ---Tiny - a cat that needs:
  145. // ---Tom - a cat that needs: A154B, 2C32B, 12CDB
  146. // SoftCare is 30% busy today!
  147. // Total profit: 500.00$
  148. // Anna Morgan with:
  149. // ---Max - a dog that needs: SK456, DFG45, KS456
  150. // Jim Jones with:
  151. // ---Sara - a dog that needs: A154B
  152. // ---Tiny - a cat that needs:
  153. // ---Tom - a cat that needs: A154B, 2C32B, 12CDB
Add Comment
Please, Sign In to add comment