View difference between Paste ID: DrxvKpSu and Pg5R4ZZg
SHOW: | | - or go back to the newest paste.
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
            
13
        if(this.pets==this.capacity){
14
            
15
            throw new Error('Sorry, we are not able to accept more patients!')
16
        }
17
        else{
18
            let owner=this.clients.find(c=>c.name==ownerName)
19
20
            if(owner===undefined){
21
                
22
                owner={
23
                    name: ownerName,
24
                    pets: []
25
                }
26
                this.clients.push(owner)
27
            }
28
            let pet=owner.pets.find(p=>p.name===petName)
29
30
            if(pet===undefined || pet.procedures.length==0){
31
                
32
                if(pet===undefined){
33
                    pet={
34
                        name: petName,
35
                        kind: kind.toLowerCase(),
36
                        owner: ownerName,
37
                        procedures: [],
38
                    }
39
                    owner.pets.push(pet)
40
                }
41
                pet.procedures=procedures
42
                this.pets++
43
                this.currentWorkload=(this.pets/this.capacity)*100
44
                return `Welcome ${petName}!`
45
            }
46
            else{
47
48
               throw new Error(`This pet is already registered under ${ownerName} name! ${petName} is on our lists, waiting for ${pet.procedures.join(', ')}`)
49
            }
50
        }
51
    }
52
    
53
    onLeaving(ownerName, petName){
54
        let client=this.clients.find(c=>c.name===ownerName)
55
        
56
        if(client===undefined){
57
            
58
            throw new Error('Sorry, there is no such client!')
59
        }
60
        else{
61
            
62
            let pet=client.pets.find(p=>p.name===petName)
63
            
64
            if(pet===undefined || pet.procedures.length===0){
65
                
66
                throw new Error(`Sorry, there are no procedures for ${petName}!`)
67
            }
68
            else{
69
                
70
                this.totalProfit+=(pet.procedures.length)*500
71
                pet.procedures=[]
72
                this.pets--
73
                this.currentWorkload=(this.pets/this.capacity)*100
74
                return `Goodbye ${petName}. Stay safe!`
75
            }
76
        }
77
    }
78
    toString(){
79
        let output=[
80
            `${this.clinicName} is ${Math.floor(this.currentWorkload)}% busy today!`,
81
            `Total profit: ${this.totalProfit.toFixed(2)}$`,
82
        ]
83
        this.clients.sort((a, b)=>(a.name > b.name) ? 1 : ((b.name > a.name) ? -1 : 0))
84
        .forEach(c => {
85
            output.push(`${c.name} with:`)
86
            c.pets.sort((a, b)=> (a.name>b.name)? 1 : ((b.name>a.name)?-1:0)).forEach(p=>{
87
                output.push(`---${p.name} - a ${p.kind} that needs: ${p.procedures.join(', ')}`)
88
            })
89
        });
90
        return output.join('\n')
91
    }
92
}