The employee list for a company contains employee code, name, designation and basic pay. The employee is given HRA of 10% of the basic and DA of 45% of the basic pay. The total pay of the employee is calculated as Basic pay+HRA+ DA. Write a class to define the details of the employee. Write a constructor to assign the required initial values. Add a method to calculate HRA, DA and Total pay and print them out. Write another class with a main method. Create objects for three different employees and calculate the HRA, DA and total pay.
class Employee {
int employeeCode;
String name;
String designation;
double basicPay;
double hra;
double da;
double totalPay;
Employee(int employeeCode, String name, String designation, double basicPay) {
this.employeeCode = employeeCode;
this.name = name;
this.designation = designation;
this.basicPay = basicPay;
}
void calculatePay() {
hra = 0.1 * basicPay;
da = 0.45 * basicPay;
totalPay = basicPay + hra + da;
}
void printDetails() {
System.out.println("Employee Code: " + employeeCode);
System.out.println("Name: " + name);
System.out.println("Designation: " + designation);
System.out.println("Basic Pay: $" + basicPay);
System.out.println("HRA: $" + hra);
System.out.println("DA: $" + da);
System.out.println("Total Pay: $" + totalPay);
System.out.println();
}
}
public class Main {
public static void main(String[] args) {
// Creating objects for three different employees
Employee employee1 = new Employee(101, "John Doe", "Manager", 50000.0);
Employee employee2 = new Employee(102, "Jane Smith", "Engineer", 40000.0);
Employee employee3 = new Employee(103, "Alice Johnson", "Analyst", 30000.0);
employee1.calculatePay();
employee2.calculatePay();
employee3.calculatePay();
System.out.println("Employee 1 Details:");
employee1.printDetails();
System.out.println("Employee 2 Details:");
employee2.printDetails();
System.out.println("Employee 3 Details:");
employee3.printDetails();
}
}