Exercise - 9 Classes and Objects
Balaji V 24mx305
JavaScript Code:
class Person {
constructor(name, age) {
[Link] = name;
[Link] = age;
}
displayPersonalDetail() {
[Link](`Name: ${[Link]}, Age: ${[Link]}`);
}
}
class Employee extends Person {
static empCount = 0;
constructor(name, age, basicPay) {
super(name, age);
this.#empNumber = ++[Link];
this.#basicPay = basicPay;
this.#da = 0;
this.#hra = 0;
this.#netPay = 0;
this.#loan = 0;
this.#calculateAllowances();
this.#calculateSalary();
}
#empNumber;
#basicPay;
#da;
#hra;
#netPay;
#loan;
#calculateAllowances() {
this.#da = this.#basicPay * 0.1;
this.#hra = this.#basicPay * 0.2;
}
Exercise - 9 Classes and Objects
Balaji V 24mx305
#calculateSalary() {
const grossPay = this.#basicPay + this.#da + this.#hra;
this.#loan = grossPay > 3000 ? grossPay * 0.05 : 0;
this.#netPay = grossPay - this.#loan;
}
displaySalaryDetails() {
[Link](`Employee Number: ${this.#empNumber}`);
[Link]();
[Link](`Gross Pay: ${this.#basicPay + this.#da + this.#hra}`);
[Link](`Loan Deduction: ${this.#loan}`);
[Link](`Net Pay: ${this.#netPay}`);
}
}
const employees = [];
[Link](new Employee("Alice", 30, 3200));
[Link](new Employee("Bob", 28, 2800));
[Link](new Employee("Charlie", 35, 4500));
[Link](`Salary Report of << ${[Link]} >> Employees`);
[Link]("Employee Number | Employee Name | Gross Pay | Loan Deduction
| Net Pay");
[Link](employee => {
[Link]();
[Link]("-----------------------------");
});
Exercise - 9 Classes and Objects
Balaji V 24mx305
Output:
Exercise - 9 Classes and Objects
Balaji V 24mx305
Diagram: