-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
88 lines (68 loc) · 2.79 KB
/
cli.js
File metadata and controls
88 lines (68 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// CPF Payroll Calculator
const readline = require('readline');
const CPF_RATES = {
'<55': { employee: 0.2, employer: 0.17 },
'55-60': { employee: 0.13, employer: 0.13 },
'60-65': { employee: 0.075, employer: 0.09 },
'>65': { employee: 0.05, employer: 0.075 },
};
const getAgeGroup = (age) => {
if (age < 55) return '<55';
if (age >= 55 && age < 60) return '55-60';
if (age >= 60 && age < 65) return '60-65';
return '>65';
};
const calculateCPF = (salary, age, isCitizen = true) => {
if (!isCitizen) return { employeeCPF: 0, employerCPF: 0, totalCPF: 0 };
const ageGroup = getAgeGroup(age);
const rates = CPF_RATES[ageGroup];
const employeeCPF = salary * rates.employee;
const employerCPF = salary * rates.employer;
const totalCPF = employeeCPF + employerCPF;
return { employeeCPF, employerCPF, totalCPF };
};
const calculatePayroll = (grossSalary, age, isCitizen, additionalPayments = 0) => {
const totalSalary = grossSalary + additionalPayments;
const { employeeCPF, employerCPF, totalCPF } = calculateCPF(totalSalary, age, isCitizen);
const netSalary = totalSalary - employeeCPF;
return {
grossSalary,
additionalPayments,
totalSalary,
employeeCPF,
employerCPF,
totalCPF,
netSalary,
};
};
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const getEmployeeData = () => {
rl.question('Enter employee name: ', (name) => {
rl.question('Enter gross salary: ', (grossSalaryInput) => {
const grossSalary = parseFloat(grossSalaryInput);
rl.question('Enter age: ', (ageInput) => {
const age = parseInt(ageInput, 10);
rl.question('Is the employee a Singapore Citizen? (yes/no): ', (isCitizenInput) => {
const isCitizen = isCitizenInput.toLowerCase() === 'yes';
rl.question('Enter additional payments (if any): ', (additionalPaymentsInput) => {
const additionalPayments = parseFloat(additionalPaymentsInput) || 0;
const payroll = calculatePayroll(grossSalary, age, isCitizen, additionalPayments);
console.log(`Payroll for ${name}:`, payroll);
rl.question('Do you want to calculate payroll for another employee? (yes/no): ', (anotherInput) => {
if (anotherInput.toLowerCase() === 'yes') {
getEmployeeData();
} else {
rl.close();
}
});
});
});
});
});
});
};
getEmployeeData();
module.exports = { calculateCPF, calculatePayroll };