Employee.java
Employee.java
//Employee Class (Base class)
public class Employee {
int Empno;
String name;
String telephone;
double basicsalar;
double othrs;
double otrate;
double netSal;
public double calcNetSalary(){
return basicsalar + othrs * otrate;
}
public void displayNetSalary() {
netSal = basicsalar + othrs * otrate;
System.out.println("Net Salary: "+netSal);
}
//Set name
public void setName(String name) {
this.name = name;
}
//Set telephone
public void setTelephone(String telephone) {
this.telephone = telephone;
}
//Set Empno
public void setEmpno(int Empno) {
this.Empno = Empno;
}
//Set OT hours
public void setOthrs(double othrs) {
this.othrs = othrs;
}
//Set OT Rate
public void setOtrate(double otrate) {
this.otrate = otrate;
}
//Set OT Rate
public void setBasicsalar(double basicsalar) {
this.basicsalar = basicsalar;
}
}
Manager.java
//Manager Class public class Manager extends Employee { double allowances; double deductions; public double calcNetSalary() { return basicsalar + allowances - deductions; } public void displayNetSalary() { netSal = basicsalar + allowances - deductions; System.out.println("Net Salary : " + netSal); } //Set allowances public void setAllowances(double allowances) { this.allowances = allowances; } //Set name public void setName(double deductions) { this.deductions = deductions; } }
Director.java
//Director class
public class Director extends Manager{
double bonus;
double entertainmentAllowance;
public double calcNetSalary() {
return basicsalar + allowances - deductions + bonus + entertainmentAllowance;
}
public void displayNetSalary() {
netSal = basicsalar + allowances - deductions + bonus + entertainmentAllowance;
System.out.println("Net Salary : " + netSal);
}
//Set bonus
public void setBonus(double bonus ) {
this.bonus = bonus;
}
//Set entertainmentAllowance
public void setEntertainmentAllowance(double entertainmentAllowance) {
this.entertainmentAllowance = entertainmentAllowance;
}
}
Now we can create the main functionalities to access those classes. Then we can create and initialize objects. Following is an example main method. It uses the classes created above.
Main.java
//Main public class Main { public static void main(String[] args) { /*-------Creating objects---*/ Employee emp1 = new Employee();//Employee 1 Manager m1 = new Manager(); // Manager 1 Director d1 = new Director();// Director 1 Employee emp2 = new Employee();//Employee 2 Manager m2 = new Manager();// Manager 2 Director d2 = new Director();// Director 2 /*------ setting values for Employee 1----*/ emp1.setName("Amal the Employee"); emp1.setTelephone("0786141343"); emp1.setEmpno(0112); emp1.setOthrs(5); emp1.setOtrate(500.00); emp1.setBasicsalar(10000.00); /*------ setting values for Employee 2----*/ emp2.setName("Kamal the Employee"); emp1.setTelephone("0786141343"); emp1.setEmpno(0112); emp1.setOthrs(3); emp1.setOtrate(500.00); emp1.setBasicsalar(10000.00); /*------ setting values for Manager 1----*/ m1.setName("Nimal the Manager"); emp1.setTelephone("0786141343"); emp1.setEmpno(0112); emp1.setOthrs(7); emp1.setOtrate(530.00); emp1.setBasicsalar(10400.00); /*------ setting values for Manager 2----*/ m2.setName("Sunil the Manager"); emp1.setTelephone("0783424343"); emp1.setEmpno(0115); emp1.setOthrs(3); emp1.setOtrate(300.00); emp1.setBasicsalar(10500.00); /*------ setting values for Director 1----*/ d1.setName("Namal the Directoe"); emp1.setTelephone("0774641343"); emp1.setEmpno(0122); emp1.setOthrs(9); emp1.setOtrate(460.00); emp1.setBasicsalar(130000.00); /*------ setting values for Director 2----*/ d2.setName("Imal the Director"); emp1.setTelephone("0707141343"); emp1.setEmpno(0132); emp1.setOthrs(0); emp1.setOtrate(870.00); emp1.setBasicsalar(140000.00); System.out.println(""); } }
Comments
Post a Comment
Hello there, Add your comment here!