Saturday, November 6, 2021

Java Program On Inheritance - Employee And Details

This is a program on inheritance where personal records of Employees will be managed in super class and the sub class will deal with professional records. Authentication while entering values are done.

import java.io.*;
 class EmployeePersonal
 {
     String name[], address[], contact[], qualification[];
     int age[],n;
     BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
     EmployeePersonal(int n1)
     {
        n=n1;
        name=new String[n]; 
        address=new String[n];
        contact=new String[n];
        qualification=new String[n];
        age=new int[n];
    }
    protected void takePersonalDetails(int i) throws IOException
    {
        
            boolean bool=true;
            System.out.print("\nName: ");
            name[i]=br.readLine();
            System.out.print("\nAddress: ");
            address[i]=br.readLine();
            while(bool)
            {
                try
                {
            System.out.print("\nAge: ");
            age[i]=Integer.parseInt(br.readLine());
            bool=false;
        }catch(Exception e)
        {
            System.out.println("\nEnter Age Properly...");
        }
    }
    bool=true;
    while(bool)
    {
            System.out.print("\nContact Number: ");
            contact[i]=br.readLine();
            if(contact[i].length()>=10 && contact[i].length()<=14)
            bool=false;
            else
            System.out.println("\nContact Number Should Be XXXXXXXXXX / +91 XXXXXXXXXX ");
        }       
            System.out.print("\nQualification: ");
            qualification[i]=br.readLine();
       
    }
    protected boolean authentication(String s)
    {
        int day=0, month=0, year=0;
        boolean bool=true;
        int i,len,counter=0,leap;
        len=s.length();
        for(i=0;i<len;i++)
        { 
            if(s.charAt(i)=='/')
            counter++;
        }
        if(counter!=2)
        bool=false;
        else if(counter==2)
        {            
        i=s.indexOf('/');
        day=Integer.parseInt(s.substring(0,i));
        s=s.substring(i+1);
        i=s.indexOf('/');
        month=Integer.parseInt(s.substring(0,i));
        s=s.substring(i+1);
        year=Integer.parseInt(s);         
        if(year%100==0 && year%400==0)
        leap=1;
        else if(year%100!=0 && year%4==0)
        leap=1;
        else
        leap=0;
        if((month==4 || month==6 || month==9 || month==1) && day>30)
        bool=false;
        else if((month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12) && day>31)
        bool=false;
        else if (leap==1 && month==2 && day>29)
        bool=false;
        else if (leap==0 && month==2 && day>28)
        bool=false;
        else if(month>12)
        bool=false;
    }
     
    
        return bool;
    }             
        
    }
  
    class EmployeeProfession extends EmployeePersonal
    {
       String department[], designation[], joining[]; 
       double basic[];
       EmployeeProfession(int n1)
       {
        super(n1);
        department=new String[n1]; 
        designation=new String[n1];
        joining=new String[n1];
        basic=new double[n1];
    }
    protected void takeProfessionalDetails(int i) throws IOException
    {
         boolean bool=true;
         takePersonalDetails(i);
            System.out.print("\nDepartment: ");
            department[i]=br.readLine();
            System.out.print("\nDesignation: ");
            designation[i]=br.readLine();
            while(bool)
            {
            System.out.print("\nDate of joining (dd/mm/yyyy): ");
            joining[i]=br.readLine();
            if(authentication(joining[i]))
            bool=false;
            }
        bool=true;
            while(bool)
            {
                try
                {
            System.out.print("\nBasic Salary: ");
            basic[i]=Double.parseDouble(br.readLine());
            bool=false;
        }catch(Exception e)
        {
              System.out.println("\nEnter Basic Salary Properly...");
        }
        }
    }
    
    protected void showCompleteRecords()
    {
        System.out.println("Name           Address       Age  Contact No  Qualification   Department  Designation Date of Joining   Basic Salary");;
        for(int i=0;i<n;i++)
        {
            System.out.println(name[i]+ " " + address[i]+ " " + age[i]+ " " + contact[i]+ " "+ qualification[i]+ " "+department[i]+ " "+designation[i]+ " "+ joining[i]+ " "+ basic[i]);
        }   
    }
    protected void showProfessionalRecords()
    {
        System.out.println("Name           Department  Designation Date of Joining   Basic Salary");;
        for(int i=0;i<n;i++)
        {
            System.out.println(name[i]+ " " + department[i]+ " "+designation[i]+ " "+ joining[i]+ " "+ basic[i]);
        }   
    }
    
    protected void showDepartmentWiseRecords() throws IOException
    {
       String dept;
       int x=0;
       System.out.print("\nEnter Department: ");
       dept=br.readLine();
       System.out.println("Name           Address       Age  Contact No  Qualification   Department  Designation Date of Joining   Basic Salary");;
        for(int i=0;i<n;i++)
        {
           if(dept.equalsIgnoreCase(department[i]))
           {
               System.out.println(name[i]+ " " + address[i]+ " " + age[i]+ " " + contact[i]+ " "+ qualification[i]+ " "+department[i]+ " "+designation[i]+ " "+ joining[i]+ " "+ basic[i]);
               x=1;
            }
        }
        if(x==0)
        System.out.println("\nNot A Single Employee In This Department ");
    }
    protected void showBasicWiseRecords() throws IOException
    {
       double basic_sal;
       int x=0;
       System.out.print("\nEnter Basic Salary: ");
       basic_sal=Double.parseDouble(br.readLine());
       System.out.println("Name           Address       Age  Contact No  Qualification   Department  Designation Date of Joining   Basic Salary");;
        for(int i=0;i<n;i++)
        {
           if(basic_sal>=basic[i])
           {
               System.out.println(name[i]+ " " + address[i]+ " " + age[i]+ " " + contact[i]+ " "+ qualification[i]+ " "+department[i]+ " "+designation[i]+ " "+ joining[i]+ " "+ basic[i]);
               x=1;
            }
        }
        if(x==0)
        System.out.println("\nNot A Single Employee Found ");
    }
}
class Employee
{
    
        public static void main(String args[])throws IOException
        {
            int choice=1,n,i;
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            System.out.print("\nHow Many Employees: ");
            n=Integer.parseInt(br.readLine());
            EmployeeProfession ob=new EmployeeProfession (n);
            for(i=0;i<n;i++)
            ob.takeProfessionalDetails(i);
            while(choice!=0)
            {
                System.out.println("Enter 1 to view Full Records, 2 to view only Professional Records, 3 to view Department wise record, 4 to view Basic Salary wise Records and 0 to exit: ");
                choice=Integer.parseInt(br.readLine());
                switch(choice)
                {
                     case 1:
                     ob.showCompleteRecords();
                     break;
                     case 2:
                     ob.showProfessionalRecords();
                     break;
                     case 3:
                     ob.showDepartmentWiseRecords();
                     break;
                     case 4:
                     ob.showBasicWiseRecords();
                     break;
                     case 0:
                     System.out.print("\nEnd of Program.");
                     break;
                     default:
                     System.out.print("\nWrong Choice.");
                    }
                }
            }
        }
                

       Sample Input Output


How Many Employees: 2
Name: Abinash
Address: Burdwan
Age: 23
Contact Number: 98765445
Contact Number Should Be XXXXXXXXXX / +91 XXXXXXXXXX 
Contact Number: 9874009876
Qualification: B.Tech
Department: Spinning
Designation: Spg. Master
Date of joining (dd/mm/yyyy): 11/22/2000
11:22:2000
Date of joining (dd/mm/yyyy): 11/11/2020
11:11:2020
Basic Salary: 2345

Name: Amal
Address: Kolkata
Age: 26
Contact Number: 9876009834
Qualification: B.E
Department: Weaving
Designation: Wvg. Manager
Date of joining (dd/mm/yyyy): 11/11/2000
11:11:2000
Basic Salary: 5555

Enter 1 to view Full Records, 2 to view only Professional Records, 3 to view Department wise record, 4 to view Basic Salary wise Records and 0 to exit: 
1
Name           Address       Age  Contact No  Qualification   Department  Designation Date of Joining   Basic Salary
Abinash Burdwan 23 9874009876 B.Tech Spinning Spg. Master 11/11/2020 2345.0
Amal Kolkata 26 9876009834 B.E Weaving Wvg. Manager 11/11/2000 5555.0

Enter 1 to view Full Records, 2 to view only Professional Records, 3 to view Department wise record, 4 to view Basic Salary wise Records and 0 to exit: 
2

Name           Department  Designation Date of Joining   Basic Salary
Abinash Spinning Spg. Master 11/11/2020 2345.0
Amal Weaving Wvg. Manager 11/11/2000 5555.0

Enter 1 to view Full Records, 2 to view only Professional Records, 3 to view Department wise record, 4 to view Basic Salary wise Records and 0 to exit: 
3

Enter Department: Spinning
Name           Address       Age  Contact No  Qualification   Department  Designation Date of Joining   Basic Salary
Abinash Burdwan 23 9874009876 B.Tech Spinning Spg. Master 11/11/2020 2345.0
Enter 1 to view Full Records, 2 to view only Professional Records, 3 to view Department wise record, 4 to view Basic Salary wise Records and 0 to exit: 
2

Name           Department  Designation Date of Joining   Basic Salary
Abinash Spinning Spg. Master 11/11/2020 2345.0
Amal Weaving Wvg. Manager 11/11/2000 5555.0
Enter 1 to view Full Records, 2 to view only Professional Records, 3 to view Department wise record, 4 to view Basic Salary wise Records and 0 to exit: 
4

Enter Basic Salary: 4444
Name           Address       Age  Contact No  Qualification   Department  Designation Date of Joining   Basic Salary
Abinash Burdwan 23 9874009876 B.Tech Spinning Spg. Master 11/11/2020 2345.0
Enter 1 to view Full Records, 2 to view only Professional Records, 3 to view Department wise record, 4 to view Basic Salary wise Records and 0 to exit: 
0

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner