Advertisement
svetlio_top

Untitled

Aug 11th, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. //VeterinaryClinic
  2. class VeterinaryClinic {
  3. constructor(clinicName, capacity){
  4. this.clinicName = clinicName;
  5. this.capacity = capacity;
  6. this.totalProfit = 0;
  7. this.clients = [];
  8. }
  9. newCustomer(ownerName, petName, kind, procedures){
  10. let check = true;
  11.  
  12. if(this.capacity>0){
  13. for (let i = 0; i < this.clients.length; i++) {
  14. const element = this.clients[i];
  15. console.log(element);
  16. }
  17. if(check === true)
  18. let obj = {};
  19.  
  20. obj[ownerName] = [
  21. petName,
  22. kind,
  23. procedures
  24. ];
  25.  
  26. this.clients.push(obj);
  27.  
  28. this.capacity--;
  29. // console.log(this.clients);
  30. return `Welcome ${petName}!`;
  31.  
  32. }else{
  33. throw new Error(`Sorry, we are not able to accept more patients!`);
  34. }
  35.  
  36. }
  37. onLeaving(ownerName, petName){
  38.  
  39.  
  40.  
  41. if (this.clients.find(c => c.ownerName === ownerName && c.petName === petName)){
  42.  
  43. for (let i = 0; i < this.clients.length; i++) {
  44. const element = this.clients[i];
  45. // console.log(element);
  46. if(ownerName === element.ownerName && petName === element.petName){
  47. if(element.procedures.length === 0){
  48. return `Sorry, there are no procedures for ${petName}!`
  49. }else{
  50. this.totalProfit = element.procedures.length*500;
  51. element.procedures = [];
  52. this.capacity++;
  53. // console.log(this.clients);
  54. }
  55. }
  56. }
  57. // console.log('da');
  58. return `Goodbye ${petName}. Stay safe!`;
  59. }else{
  60. throw new Error(`Sorry, there is no such client!`);
  61. }
  62. }
  63. toString(){
  64. let percentage = (10-this.capacity)*10;
  65.  
  66. let arr = [];
  67.  
  68. // let test = Object.keys(this.clients);
  69. // console.log(test);
  70. for (const key in this.clients) {
  71. let obj = {};
  72. const element = this.clients[key];
  73. obj[element.ownerName] = {petName: element.petName, kind: element.kind, procedures:element.procedures};
  74. console.log(obj);
  75.  
  76. arr.push(obj);
  77. for (let i = 0; i < arr.length; i++) {
  78. console.log(arr[i]);
  79.  
  80. // if(arr[i][element.ownerName] === element.ownerName){
  81. // console.log('da');
  82. // }
  83. }
  84. //arr.push([`${element.ownerName} with:`,
  85. // `---${element.petName} - a ${element.kind} that needs: ${element.procedures.join(', ')}`
  86. // ].join('\n'));
  87. console.log(arr);
  88. }
  89. let result = [
  90. `${this.clinicName} is ${percentage}% busy today!`,
  91. `Total profit: ${this.totalProfit.toFixed(2)}$`
  92. ];
  93.  
  94. return result.concat(arr).join('\n');
  95. // console.log(this.clients);
  96. }
  97. }
  98.  
  99.  
  100.  
  101.  
  102. let clinic = new VeterinaryClinic('SoftCare', 10);
  103. //console.log(clinic);
  104.  
  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