Wednesday, May 9, 2012

Employee Database Using JAVA



import java.io.*;
public class employee
{
    public static void write()throws IOException
    {
        long empcode;
        String empname;
        int hrsworked;
        double rate;
        boolean con=true;
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        FileOutputStream obj=new FileOutputStream("emp.dat");
        DataOutputStream fl=new DataOutputStream(obj);
        while(con)
        {
            System.out.print("Enter Employee name:");
            empname=br.readLine();
            System.out.print("Enter employee code:");
            empcode=Long.parseLong(br.readLine());
            System.out.print("Enter Hours Worked:");
            hrsworked=Integer.parseInt(br.readLine());
            System.out.print("Enter rate:");
            rate=Double.parseDouble(br.readLine());
            fl.writeUTF(empname);
            fl.writeLong(empcode);
            fl.writeInt(hrsworked);
            fl.writeDouble(rate);
            System.out.print("Continue(y/n)?:");
            String c=br.readLine();
            if(c.equalsIgnoreCase("n")==true)
            con=false;
            System.out.println();
        }
        obj.close();
        fl.close();
        System.out.println("Data added to file successfully!!");
        System.out.println();
    }
    public static void append()throws IOException
    {
        long empcode;
        String empname;
        int hrsworked;
        double rate;
        boolean con=true;
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        FileOutputStream std=new FileOutputStream("emp.dat",true);
        DataOutputStream mat=new DataOutputStream(std);
        while(con)
        {
            System.out.print("Enter Employee name:");
            empname=br.readLine();
            System.out.print("Enter employee code:");
            empcode=Long.parseLong(br.readLine());
            System.out.print("Enter Hours Worked:");
            hrsworked=Integer.parseInt(br.readLine());
            System.out.print("Enter rate:");
            rate=Double.parseDouble(br.readLine());
            mat.writeUTF(empname);
            mat.writeLong(empcode);
            mat.writeInt(hrsworked);
            mat.writeDouble(rate);
            System.out.print("Continue(y/n)?:");
            String c=br.readLine();
            if(c.equalsIgnoreCase("n")==true)
            con=false;
        }
        std.close();
        mat.close();
        System.out.println("Data added to file successfully!!");
        System.out.println();
    }
       
    public static void read()throws IOException
    {
        long empcode[]=new long[100];
        String empname[]=new String[100];
        int hrsworked[]=new int[100];;
        double rate[]=new double[100];
        boolean con=true;
        int x=0;
        FileInputStream obj=new FileInputStream("emp.dat");
        DataInputStream fl=new DataInputStream(obj);
        boolean eof=false;
        while(!eof)
        {
            try
            {
                empname[x]=fl.readUTF();
                empcode[x]=fl.readLong();
                hrsworked[x]=fl.readInt();
                rate[x]=fl.readDouble();
                x++;
            }
            catch(EOFException e)
            {
                eof=true;
            }
        }
        for(int i=0;i<x;i++)
        {
            for(int j=0;j<x-1;j++)
            {
                if(empname[j].compareTo(empname[j+1])>1)
                {
                    long lt;
                    int it;
                    String st;
                    double dt;
                    st=empname[j];
                    lt=empcode[j];
                    it=hrsworked[j];
                    dt=rate[j];
                    empname[j]=empname[j+1];
                    empcode[j]=empcode[j+1];
                    hrsworked[j]=hrsworked[j+1];
                    rate[j]=rate[j+1];
                    empname[j+1]=st;
                    empcode[j+1]=lt;
                    hrsworked[j+1]=it;
                    rate[j+1]=dt;
                }
            }
        }
        System.out.println("Employee Code\t Name\t\tHours Worked\tRate\t\tWeekly Wages");
        for(int i=0;i<x;i++)
        {
            System.out.println(empcode[i]+"\t\t "+empname[i]+"\t\t"+hrsworked[i]+"\t\tRs."+rate[i]+"\t\tRs."+(hrsworked[i]*rate[i]));
        }
            x=0;
            System.out.println();
    }
    public static void main(String args[])throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        boolean con=true;
        while(con)
        {
            System.out.println("       Menu");
            System.out.println("1:Write to file");
            System.out.println("2:Read from file");
            System.out.println("3:Modify to file");
            System.out.print("Enter your option:");
            int n=Integer.parseInt(br.readLine());
            System.out.println();
            if(n==1)
            write();
            else if(n==2)
            read();
            else if(n==3)
            append();
            else
            System.out.println("Wrong Entry!!!!");
            System.out.print("Continue with menu options(y/n)?:");
            String c=br.readLine();
            if(c.equalsIgnoreCase("n")==true)
            con=false;
        }
    }
}
//---------------------------------------------------------------------------
/**
 Output:-
       Menu
1:Write to file
2:Read from file
3:Modify to file
Enter your option:1

Enter Employee name:abc
Enter employee code:47
Enter Hours Worked:56
Enter rate:25
Continue(y/n)?:y

Enter Employee name:pqr
Enter employee code:96
Enter Hours Worked:45
Enter rate:65
Continue(y/n)?:y

Enter Employee name:xyz
Enter employee code:87
Enter Hours Worked:85
Enter rate:51
Continue(y/n)?:n

Data added to file successfully!!

Continue with menu options(y/n)?:y
       Menu
1:Write to file
2:Read from file
3:Modify to file
Enter your option:2

Employee Code    Name       Hours Worked    Rate        Weekly Wages
47               abc        56              Rs.25.0     Rs.1400.0
96               pqr        45              Rs.65.0     Rs.2925.0
87               xyz        85              Rs.51.0     Rs.4335.0

Continue with menu options(y/n)?:y
       Menu
1:Write to file
2:Read from file
3:Modify to file
Enter your option:3

Enter Employee name:lmn
Enter employee code:69
Enter Hours Worked:20
Enter rate:90
Continue(y/n)?:n
Data added to file successfully!!

Continue with menu options(y/n)?:y
       Menu
1:Write to file
2:Read from file
3:Modify to file
Enter your option:2

Employee Code    Name       Hours Worked    Rate        Weekly Wages
47               abc        56              Rs.25.0     Rs.1400.0
69               lmn        20              Rs.90.0     Rs.1800.0
96               pqr        45              Rs.65.0     Rs.2925.0
87               xyz        85              Rs.51.0     Rs.4335.0
   
Continue with menu options(y/n)?:n

*/
       

                   
                   

               
       


 

1 comment: