Sunday, 16 July 2017

Java Practical - 1 - 2

===========================
  2]   Employee’s details are maintained by organization and that contains parameter namely name, designation, salary, department and experience. Create individual                           thread for each employee and display information of them at a regular interval. [Assume that there are four employees in the company].

=============================================

import java.util.*;
class data
{
synchronized public void print(String name,String dept,String des,float Salary)
{
System.out.println("Name\t:"+name);
System.out.println("Dept\t:"+dept);
System.out.println("Des\t:"+des);
System.out.println("Salary\t:"+Salary);
try
{
Thread.sleep(1000);
}
catch(InterruptedException ex){}
}
}

class Emp1 extends Thread
{
data d;
String Name,dept,des;
float Salary;

void getData(data d,String nm,String dept,String des,float sal)
{
this.d=d;
this.Name=nm;
this.dept=dept;
this.des=des;
this.Salary=sal;
}
public void run()
{
d.print(Name,dept,des,Salary);
}
}

class Emp2 extends Thread
{
data d;
String Name,dept,des;
float Salary;

void getData(data d,String nm,String dept,String des,float sal)
{
this.d=d;
this.Name=nm;
this.dept=dept;
this.des=des;
this.Salary=sal;
}
public void run()
{
d.print(Name,dept,des,Salary);
}
}

class Emp3 extends Thread
{
data d;
String Name,dept,des;
float Salary;

void getData(data d,String nm,String dept,String des,float sal)
{
this.d=d;
this.Name=nm;
this.dept=dept;
this.des=des;
this.Salary=sal;
}
public void run()
{
d.print(Name,dept,des,Salary);
}
}

class Emp4 extends Thread
{
data d;
String Name,dept,des;
float Salary;

void getData(data d,String nm,String dept,String des,float sal)
{
this.d=d;
this.Name=nm;
this.dept=dept;
this.des=des;
this.Salary=sal;
}
public void run()
{
d.print(Name,dept,des,Salary);
}
}



class Employee
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String Name,dept,des;
float salary;
data d=new data();

Emp1 e1=new Emp1();
Emp2 e2=new Emp2();
Emp3 e3=new Emp3();
Emp4 e4=new Emp4();

for(int i=0;i<4;i++)
{
System.out.println("Enter Name");
Name=sc.nextLine();

System.out.println("Enter Designation");
des=sc.nextLine();

System.out.println("Enter Department");
dept=sc.nextLine();

System.out.println("Enter Salary");
salary=sc.nextFloat();
sc.nextLine();

if (i==0)
{
e1.getData(d,Name,des,dept,salary);
}
else if (i==1)
{
e2.getData(d,Name,des,dept,salary);
}
else if (i==2)
{
e3.getData(d,Name,des,dept,salary);
}
else if (i==3)
{
e4.getData(d,Name,des,dept,salary);
}
}

synchronized(d)
{
System.out.println("Data of Emp1..");
e1.start();
System.out.println("\n\nData of Emp2..");
e2.start();
System.out.println("\n\nData of Emp3..");
e3.start();
System.out.println("\n\nData of Emp4..");
e4.start();
}
}

}

No comments :

Post a Comment

Know us

Our Team

Translate

Contact us

Name

Email *

Message *