Skip to content

Commit e08aa0f

Browse files
authored
Update employee salary system complete code.js
1 parent 001d860 commit e08aa0f

1 file changed

Lines changed: 307 additions & 0 deletions

File tree

Lines changed: 307 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,307 @@
1+
// Comprehensive Employee Salary Management System
2+
3+
// Utility Class for Validation and Helpers
4+
class ValidationUtils {
5+
static validateEmail(email) {
6+
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
7+
return emailRegex.test(email);
8+
}
9+
10+
static validateSalary(salary) {
11+
return salary > 0 && salary < 1000000;
12+
}
13+
14+
static generateUniqueId() {
15+
return 'EMP-' + Math.random().toString(36).substr(2, 9).toUpperCase();
16+
}
17+
}
18+
19+
// Tax Calculation Utility
20+
class TaxCalculator {
21+
static calculateTax(grossSalary) {
22+
// Progressive tax brackets (simplified)
23+
const taxBrackets = [
24+
{ min: 0, max: 50000, rate: 0.10 },
25+
{ min: 50001, max: 100000, rate: 0.20 },
26+
{ min: 100001, max: 250000, rate: 0.30 },
27+
{ min: 250001, max: Infinity, rate: 0.40 }
28+
];
29+
30+
let totalTax = 0;
31+
let remainingSalary = grossSalary;
32+
33+
for (let bracket of taxBrackets) {
34+
if (remainingSalary > 0) {
35+
const taxableAmount = Math.min(remainingSalary, bracket.max - bracket.min);
36+
totalTax += taxableAmount * bracket.rate;
37+
remainingSalary -= taxableAmount;
38+
}
39+
}
40+
41+
return {
42+
grossSalary,
43+
totalTax,
44+
netSalary: grossSalary - totalTax
45+
};
46+
}
47+
}
48+
49+
// Benefit Management Class
50+
class BenefitManager {
51+
constructor() {
52+
this.benefits = [];
53+
}
54+
55+
addBenefit(benefit) {
56+
this.benefits.push(benefit);
57+
}
58+
59+
removeBenefit(benefitName) {
60+
this.benefits = this.benefits.filter(b => b.name !== benefitName);
61+
}
62+
63+
calculateTotalBenefitValue() {
64+
return this.benefits.reduce((total, benefit) => total + benefit.value, 0);
65+
}
66+
67+
getBenefitsSummary() {
68+
return this.benefits.map(benefit => ({
69+
name: benefit.name,
70+
value: benefit.value,
71+
type: benefit.type
72+
}));
73+
}
74+
}
75+
76+
// Performance Evaluation Class
77+
class PerformanceEvaluator {
78+
constructor() {
79+
this.performanceRecords = [];
80+
}
81+
82+
addPerformanceRecord(record) {
83+
this.performanceRecords.push({
84+
date: new Date(),
85+
...record
86+
});
87+
}
88+
89+
calculatePerformanceScore() {
90+
if (this.performanceRecords.length === 0) return 0;
91+
92+
const totalScore = this.performanceRecords.reduce((sum, record) => sum + record.score, 0);
93+
return totalScore / this.performanceRecords.length;
94+
}
95+
96+
getPerformanceTrend() {
97+
return this.performanceRecords.map(record => ({
98+
date: record.date,
99+
score: record.score,
100+
project: record.project
101+
}));
102+
}
103+
}
104+
105+
// Employee Class
106+
class Employee {
107+
constructor(details) {
108+
// Validate and set basic information
109+
if (!details.name) throw new Error('Name is required');
110+
if (!ValidationUtils.validateEmail(details.email)) throw new Error('Invalid email');
111+
112+
this.id = details.id || ValidationUtils.generateUniqueId();
113+
this.name = details.name;
114+
this.email = details.email;
115+
this.department = details.department || 'Unassigned';
116+
this.position = details.position || 'Entry Level';
117+
this.hireDate = details.hireDate || new Date();
118+
this.baseSalary = details.baseSalary || 0;
119+
this.employmentType = details.employmentType || 'Full-time';
120+
121+
// Composition for additional features
122+
this.benefitManager = new BenefitManager();
123+
this.performanceEvaluator = new PerformanceEvaluator();
124+
}
125+
126+
// Salary and Compensation Methods
127+
calculateGrossSalary(includeBonus = true) {
128+
let grossSalary = this.baseSalary;
129+
130+
// Performance-based bonus
131+
if (includeBonus) {
132+
const performanceScore = this.performanceEvaluator.calculatePerformanceScore();
133+
const bonusMultiplier = this.calculateBonusMultiplier(performanceScore);
134+
grossSalary += this.baseSalary * bonusMultiplier;
135+
}
136+
137+
// Add benefits value
138+
grossSalary += this.benefitManager.calculateTotalBenefitValue();
139+
140+
return grossSalary;
141+
}
142+
143+
calculateBonusMultiplier(performanceScore) {
144+
if (performanceScore >= 9) return 0.20; // Exceptional
145+
if (performanceScore >= 7) return 0.15; // Excellent
146+
if (performanceScore >= 5) return 0.10; // Good
147+
return 0.05; // Needs improvement
148+
}
149+
150+
calculateNetSalary() {
151+
const grossSalary = this.calculateGrossSalary();
152+
const taxCalculation = TaxCalculator.calculateTax(grossSalary);
153+
return taxCalculation;
154+
}
155+
156+
// Benefit Management Delegation
157+
addBenefit(benefit) {
158+
this.benefitManager.addBenefit(benefit);
159+
}
160+
161+
removeBenefit(benefitName) {
162+
this.benefitManager.removeBenefit(benefitName);
163+
}
164+
165+
// Performance Management Delegation
166+
addPerformanceRecord(record) {
167+
this.performanceEvaluator.addPerformanceRecord(record);
168+
}
169+
170+
// Comprehensive Employee Profile
171+
getEmployeeProfile() {
172+
const taxDetails = this.calculateNetSalary();
173+
174+
return {
175+
personalInfo: {
176+
id: this.id,
177+
name: this.name,
178+
email: this.email,
179+
department: this.department,
180+
position: this.position,
181+
hireDate: this.hireDate,
182+
employmentType: this.employmentType
183+
},
184+
compensation: {
185+
baseSalary: this.baseSalary,
186+
grossSalary: taxDetails.grossSalary,
187+
netSalary: taxDetails.netSalary,
188+
taxDetails: {
189+
totalTax: taxDetails.totalTax,
190+
taxRate: (taxDetails.totalTax / taxDetails.grossSalary * 100).toFixed(2) + '%'
191+
}
192+
},
193+
benefits: this.benefitManager.getBenefitsSummary(),
194+
performance: {
195+
averageScore: this.performanceEvaluator.calculatePerformanceScore(),
196+
performanceTrend: this.performanceEvaluator.getPerformanceTrend()
197+
}
198+
};
199+
}
200+
}
201+
202+
// Payroll Processing Class
203+
class PayrollProcessor {
204+
constructor() {
205+
this.employees = [];
206+
}
207+
208+
addEmployee(employee) {
209+
this.employees.push(employee);
210+
}
211+
212+
processMonthlyPayroll() {
213+
return this.employees.map(employee => {
214+
const profile = employee.getEmployeeProfile();
215+
return {
216+
employeeId: employee.id,
217+
employeeName: employee.name,
218+
netSalary: profile.compensation.netSalary,
219+
paymentMethod: 'Direct Deposit'
220+
};
221+
});
222+
}
223+
224+
generateDepartmentSalarySummary() {
225+
const departmentSummary = {};
226+
227+
this.employees.forEach(employee => {
228+
const { department } = employee;
229+
if (!departmentSummary[department]) {
230+
departmentSummary[department] = {
231+
totalEmployees: 0,
232+
totalSalary: 0,
233+
averageSalary: 0
234+
};
235+
}
236+
237+
const profile = employee.getEmployeeProfile();
238+
departmentSummary[department].totalEmployees++;
239+
departmentSummary[department].totalSalary += profile.compensation.netSalary;
240+
});
241+
242+
// Calculate average salaries
243+
Object.keys(departmentSummary).forEach(dept => {
244+
const summary = departmentSummary[dept];
245+
summary.averageSalary = summary.totalSalary / summary.totalEmployees;
246+
});
247+
248+
return departmentSummary;
249+
}
250+
}
251+
252+
// Example Usage and Demonstration
253+
function demonstrateSalarySystem() {
254+
// Create Payroll Processor
255+
const payrollSystem = new PayrollProcessor();
256+
257+
// Create Employees
258+
const john = new Employee({
259+
name: 'John Doe',
260+
email: 'john.doe@company.com',
261+
department: 'Engineering',
262+
position: 'Senior Developer',
263+
baseSalary: 85000,
264+
employmentType: 'Full-time'
265+
});
266+
267+
// Add Benefits
268+
john.addBenefit({
269+
name: 'Health Insurance',
270+
value: 5000,
271+
type: 'Healthcare'
272+
});
273+
274+
john.addBenefit({
275+
name: 'Retirement Plan',
276+
value: 4000,
277+
type: 'Pension'
278+
});
279+
280+
// Add Performance Records
281+
john.addPerformanceRecord({
282+
project: 'Backend Redesign',
283+
score: 8.5,
284+
description: 'Exceptional project delivery'
285+
});
286+
287+
// Add Employee to Payroll
288+
payrollSystem.addEmployee(john);
289+
290+
// Generate Reports
291+
console.log('Employee Profile:', john.getEmployeeProfile());
292+
console.log('Monthly Payroll:', payrollSystem.processMonthlyPayroll());
293+
console.log('Department Salary Summary:', payrollSystem.generateDepartmentSalarySummary());
294+
}
295+
296+
// Run the demonstration
297+
demonstrateSalarySystem();
298+
299+
// Export classes for potential module usage
300+
export {
301+
Employee,
302+
PayrollProcessor,
303+
TaxCalculator,
304+
ValidationUtils,
305+
BenefitManager,
306+
PerformanceEvaluator
307+
};

0 commit comments

Comments
 (0)