728x90 AdSpace

Latest Article

Employee Class Example Code Java Inheritance Polymorphism Example





Employee management System Project in Java Online Source code Employee Class Example Code Java Inheritance Polymorphism Example OOP Inheritance Polymorphism Java Programming Tutorial with Example code What is polymorphism in Java? Method overloading or overriding? all explain in given example

public abstract class Employee {
    int employeeId;
    String firstName;
    String lastName;
 
    public Employee(int argEmployeeId,String argFirstName,String argLastName){
     

        this.employeeId=argEmployeeId;
        this.firstName=argFirstName;
        this.lastName=argLastName;
    }
    public int getEmployeeId(){ return this.employeeId;}
    public void setEmployeeId(int argEmployeeId){ this.employeeId=argEmployeeId;}
 
    public  String getFirstName(){ return this.firstName;}
    public void setFirstName(String argFirstName){ this.firstName=argFirstName;}
 
    public  String getLastName(){ return this.lastName;}
    public void setLastName(String argLastName){ this.lastName=argLastName;}
 
    public  void display(){
       System.out.println("ID : "+ getEmployeeId());
       System.out.println("First Name : "+ getFirstName());
       System.out.println("Last Name : "+ getLastName());
   }
     public abstract  float getEarning();
}




public class HourlyEmployee extends Employee {
    private float wage;
    private int hour;
    
    public HourlyEmployee(int argEmployeeId,String argFirstName,String argLastName,float argWage,int argHour){
        
        super(argEmployeeId,argFirstName,argLastName);
        this.wage=argWage;
        this.hour=argHour;
        
        
    }

    
    public  float getWage(){ return this.wage;}
    public  void setWage(float argWage){ this.wage=argWage;}
    
    public  int getHour(){ return this.hour;}
    public  void setHour(int argHour){ this.hour=argHour;}
    
    @Override
    public  void display(){
       System.out.println("HourlyEmployee------ : ");
       super.display();
       System.out.println("Wage : "+ getWage());
       System.out.println("Hour : "+ getHour());
       System.out.println("Earning: "+ getEarning());
   }
   
    public  float getEarning(){
        
        return getWage()*getHour();
    }
    
}



public class SalariedEmployee extends Employee {
  private  float weeklySalary;
    
   public  SalariedEmployee(int argEmployeeId,String argFirstName,String argLastName,float argWeeklySalary){
        
        super(argEmployeeId,argFirstName,argLastName);
        this.weeklySalary=argWeeklySalary;
        
    }
   public  float getWeeklySalary(){ return this.weeklySalary;}
   public  void setWeeklySalary(float argWeeklySalary){ this.weeklySalary=argWeeklySalary;}
    
    @Override
    public  void display(){
       System.out.println("SalariedEmployee------ : ");
       super.display();
       System.out.println("Weekly Salary : "+ getWeeklySalary());
       System.out.println("Earning: "+ getEarning());
   }
    
   
    public float getEarning(){
        
        return weeklySalary*4;
    }
    
}





import java.util.ArrayList;
import javax.swing.JOptionPane;


public class HumanResource {
    ArrayList<Employee> employees;
    
    public HumanResource( ) {
         employees = new ArrayList<Employee>();
      }
    void addEmployee()
    {
       String type= JOptionPane.showInputDialog(null, "Employee Type \n 1.Salaried Employee \n 2.Hourly Employee ");
        switch (type) {
            case "1":
                {
                    int id= Integer.parseInt (JOptionPane.showInputDialog(null, "Enter Employee Id :"));
                    String fn= JOptionPane.showInputDialog(null, "Enter First Name :");
                    String ln= JOptionPane.showInputDialog(null, "Enter Last Name: ");
                    float weeklysalray= Float.parseFloat(JOptionPane.showInputDialog(null, "Enter Weekly Salary :"));
                    Employee obj=new SalariedEmployee(id, fn, ln, weeklysalray);
                    employees.add(obj);
                    break;
                }
            case "2":
                {
                    int id= Integer.parseInt (JOptionPane.showInputDialog(null, "Enter Employee Id :"));
                    String fn= JOptionPane.showInputDialog(null, "Enter First Name :");
                    String ln= JOptionPane.showInputDialog(null, "Enter Last Name: ");
                    float wage= Float.parseFloat(JOptionPane.showInputDialog(null, "Enter Wage :"));
                    int hour= Integer.parseInt (JOptionPane.showInputDialog(null, "Enter Hour :"));
                    Employee obj=new HourlyEmployee(id, fn, ln, wage,hour);
                    employees.add(obj);
                    break;
                }
        }
    }
    
    void searchEmployee()
    {
        String sid= JOptionPane.showInputDialog(null, "Employee Id for Search. ");
        String type= JOptionPane.showInputDialog(null, "Employee Type for Search \n 1.Salaried Employee \n 2.Hourly Employee ");
        int id=Integer.parseInt(sid);
        switch (type) {
            case "1":
                for (int i=0; i< employees.size(); i++) 
           
                {
                  Employee p = employees.get(i);
                   if ( id==p.getEmployeeId()) {
                       //Printable obj=p;
                       p.display();
                    }
                 } // end for
                break;
            case "2":
                for (int i=0; i< employees.size(); i++) {
                Employee p = employees.get(i);
                if ( id==p.getEmployeeId()) {
//                      Printable obj=p;
//                      obj.print();
                      p.display();
                 }
              } // end for
                break;
        }
    
    }
    
       void deleteEmployee()
    {
        String sid= JOptionPane.showInputDialog(null, "Employee Id for delete ");
        String type= JOptionPane.showInputDialog(null, "Employee Type for delete \n 1.Salaried Employee \n 2.Hourly Employee ");
        int id=Integer.parseInt(sid);
        switch (type) {
            case "1":
                for (int i=0; i< employees.size(); i++) {
                   Employee p = employees.get(i);
                   if ( id==p.getEmployeeId()) {
                      employees.remove(i);
                    }
                 } // end for
                break;
            case "2":
                for (int i=0; i< employees.size(); i++) {
                Employee p = employees.get(i);
                if ( id==p.getEmployeeId()) {
                   employees.remove(i);
                 }
              } // end for
                break;
        }
    
    }
}



import javax.swing.JOptionPane;

public class Test {
    public static void main(String[] args) {
        // TODO code application logic here
        HumanResource hr = new HumanResource();
        String input, s;
        int ch;
        while (true) {
            String menu="Enter 1 to add \n Enter 2 to Search \n Enter 3 to Delete \n Enter 4 to Exit";
            input = JOptionPane.showInputDialog(menu);
            ch = Integer.parseInt(input);
            switch (ch) {
            case 1:
                hr.addEmployee();
            break;
            case 2:
                hr.searchEmployee();
            break;
            case 3:
                hr.deleteEmployee();
            break;
            case 4:
            System.exit(0);
            default:
            JOptionPane.showMessageDialog(null, "Enter option 1 to 4");
            }//end switch
        }//end while
}//end main
}//end class


  

no image
  • Title : Employee Class Example Code Java Inheritance Polymorphism Example
  • Posted by :
  • Date : 00:50
  • Labels :





  • Blogger Comments
  • Facebook Comments

0 comments:

Post a Comment

Top