Friday, 24 October 2014

ICSE PROJECT for accepting data of employees, searching for a particular employee, deleting a particular employee, displaying the data of a particular employee and finally exiting.

/* ICSE project for accepting data of employees, searching for a particular employee,  deleting a particular employee, displaying the data  of a  particular employee and finally exiting. */
           

import java.util.Scanner ;
public class Employee1
{
    // Declaration of instance variables
   
    private int empId;
    private String empName;
    private double empSalary;
    private String empAddress;
   
    //An araray of objects is being created
   
    private Employee1 arr[] ;
    private boolean emp_isDeleted ;
   
    private static Scanner sc = new Scanner(System.in);

    //  The default Constructor for objects of class Emplyoee

     public Employee1(){
        // initialise instance variables
        empId = 0;
        empName = new String();
        empSalary = 0.0;
        empAddress = new String();
        emp_isDeleted = false;
    }

    // Function  to accepts the data required for the object.
   
    public void input(){
        System.out.print("Enter Id : ");
        this.empId = sc.nextInt();
        System.out.print("Enter Salary : ");
        this.empSalary = sc.nextDouble();
        sc.nextLine();
        System.out.print("Enter the Name : ");
        this.empName = sc.nextLine();
        System.out.print("Enter the Address : ");
        this.empAddress = sc.nextLine();
        emp_isDeleted = false;
    }
       
    // A search funtion based on Linear search strategy
   
    public int search(int check_empId){
        int position=-1;
        for(int i = 0;i<arr.length;i++){
            if (arr[i].empId == check_empId)
            {
                position = i ;
                break;
            }
        }
        return position;
    }
   
    // The function creates, fills and returns an array of the Employee1
   
    public Employee1[] makeAndFillArray(){
        System.out.print("enter no. of Employee1s :");
        int num = sc.nextInt();
        arr = new Employee1[num];
        for(int i = 0;i<arr.length;i++){
            System.out.println("this is for the emplyoee number "+(i+1)+". :");
            arr[i] = new Employee1();
            arr[i].input();
        }
        return arr ;
    }
   
    //funtion to delete record based on empId
   
    public void delete(){
        if(emp_isDeleted){
            System.out.println("already deleted.");
        }
        else{
            emp_isDeleted = true;
            System.out.println("the Employee1 object has been deleted.");
        }
    }
   
    //Funtion to display the content
   
    public void display(){
        if(emp_isDeleted){
            System.out.println("The object of "+empName+", and of id : "+empId+" is deleted.");
        }
        else{
            System.out.println("Employee1 name : "+empName);
            System.out.println("Employee1 ID : "+empId);
            System.out.println("Employee1 Salary : "+empSalary);
            System.out.println("Employee1 Address : "+empAddress);
        }
    }
   
    // The main function to generate a menu based program
   
    public static void main(String[] args){
        Employee1 obj = new Employee1();
        int choice = 0;
        while(choice<5){
            System.out.println("          \t\t MAIN MENU :");
            System.out.println("---------------------------------");
            System.out.println("\t 1.INPUT DATA");
            System.out.println("\t 2.SEARCHING DATA");
            System.out.println("\t 3.DELETING DATA");
            System.out.println("\t 4.DISPLAYING DATA");
            System.out.println("\t 5.EXIT");
           
         
            do{
                System.out.print("enter choice(1 - 4) : ");
                choice = sc.nextInt();
            }while(choice<1 && choice>5);
           
           
            switch(choice){
                case 1:
                System.out.println("Accepting Data ");
                obj.makeAndFillArray();
                break;
               
                case 2:
                System.out.print("Enter the Employee1's id no. to be searched : ");
                int id = sc.nextInt();
                if(obj.search(id)>-1){
                    System.out.println("Search successful, Employee1 found at "+(obj.search(id)+1));
                    System.out.println("The details are : ");
                    obj.arr[obj.search(id)].display();
                }
                else{
                    System.out.println("Search unsuccessful");
                }
                break;
               
                case 3:
                System.out.print("Enter the id number to be deleted : ");
                int id1 = sc.nextInt();
                int position = obj.search(id1);
                obj.arr[position].delete();
                break ;
               
                case 4:
                System.out.println("Displaying data :");
                for(int i = 0;i<obj.arr.length;i++){
                    obj.arr[i].display();
                }
                break;
            }
        }
        System.out.println("The End of the Project");
    }
}

No comments:

Post a Comment