class Tunjangan {
kids = 0
age = {}
balita = 0
anak = 0
remaja = 0
sallary = 0
sum = 0
constructor(sallary, kids, age) {
this.sallary = sallary
this.kids = kids
this.age = age
}
validation() {
if (this.age.firstKids > 15 || this.age.secondKids > 15) {
return Promise.reject(new Error('Maximum age 15 years old'))
} else if (this.age.firstKids < 1) {
return Promise.reject(new Error('Minimum age 1 years old'))
} else if (this.kids > 2) {
return Promise.reject(new Error('Maximum 2 children'))
} else if (this.kids < 1) {
return Promise.reject(new Error('Minimum 1 children'))
} else if (this.kids == 1) {
this.age.secondKids = 0
}
return true
}
percentageBalita() {
const betweenAge = [1, 2, 3, 4, 5]
if (betweenAge.includes(this.age.firstKids) || betweenAge.includes(this.age.secondKids)) {
betweenAge.includes(this.age.firstKids) && betweenAge.includes(this.age.secondKids)
? (this.balita = 5 + 5)
: (this.balita = 5)
}
return this.balita
}
percentageAnak() {
const betweenAge = [6, 7, 8, 9, 10]
if (betweenAge.includes(this.age.firstKids) || betweenAge.includes(this.age.secondKids)) {
betweenAge.includes(this.age.firstKids) && betweenAge.includes(this.age.secondKids) ? (this.anak = 7 + 7) : (this.anak = 7)
}
return this.anak
}
percentageRemaja() {
const betweenAge = [11, 12, 13, 14, 15]
if (betweenAge.includes(this.age.firstKids) || betweenAge.includes(this.age.secondKids)) {
betweenAge.includes(this.age.firstKids) && betweenAge.includes(this.age.secondKids)
? (this.remaja = 10 + 10)
: (this.remaja = 10)
}
return this.remaja
}
percentageAge() {
if (typeof this.validation() === 'boolean') {
this.percentageBalita()
this.percentageAnak()
this.percentageRemaja()
}
return { balita: this.balita, anak: this.anak, remaja: this.remaja }
}
sumTunjanganKids() {
const balita = Math.floor((this.percentageAge().balita / 100) * this.sallary)
const anak = Math.floor((this.percentageAge().anak / 100) * this.sallary)
const remaja = Math.floor((this.percentageAge().remaja / 100) * this.sallary)
return { balita, anak, remaja }
}
totalSalaryPlusKids() {
const balita = this.sumTunjanganKids().balita
const anak = this.sumTunjanganKids().anak
const remaja = this.sumTunjanganKids().remaja
return this.sallary + balita + anak + remaja
}
reportingTunjangan() {
const subTotal = result.totalSalaryPlusKids()
const before = `${new Intl.NumberFormat('en-ID', { currency: 'IDR' }).format(this.sallary)}`
const after = `${new Intl.NumberFormat('en-ID', { currency: 'IDR' }).format(subTotal)}`
return {
sallary: {
before: `Your current sallary RP.${before}`,
after: `Your current sallary + allowence for kids RP.${after}`
}
}
}
}
const result = new Tunjangan(3500000, 1, { firstKids: 1, secondKids: 0 })
console.log(result.reportingTunjangan())