Social Icons

banner image

write a java program which contains two classesnamed as “Employee” and “Test” and one text file as input.txt.

Problem Statement:                                                        
You are required to write a java program which contains two classesnamed as “Employee” and “Test” and one text file as input.txt.
Note: There should be single java file which contains both classes.
Employee class must have the following data members:
  • empId
  • empName
  • costPrHour
  • salary
Employee class must have the following member methods:
  • Parameterized constructor
  • Getter functions for each data member
Detailed Description:
Parameterized constructor: It should take four parameters (empId, empName, costPrHour, salary) andset the values of data members with the passed parameters.
Getter Functions: You will have to define the getter function for each data member (empId, empName,costPrHour, salary). There will be a separate getter function for each data member. Such as getEmpID(), getEmpName(),getCostPrHour() and getSalary( ).
Input text file contains the Employee data which has three columns and as many rows as you want. First column contains EmployeeempId, second column contains EmployeeempNameand third column contains EmployeeCostprHour. All three columns are separated by space.
1 Ali 100
2 Noman 200
3 waqar 300
4 Toqeer 400
5 Waqas 500

input.txt

Testis a public driver class that contains the main() method. The name of your file should be Test as it is a public class in your program.
Within main() method, you are required to enter the  value of working hours in a week 
and read the Employee record one by one from input.txt file.
Calculate the monthly Salary of each employee based on the working hours in a week and cost per hour.
salary = costPrHour*workinghoursprweek*4;
then display the Employee information (empId, empName, costPrHour, salary) on the command prompt and GUI (Graphical User Interface). You have to display the information in the same format as was in input.txt file.
solution

import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.util.ArrayList;
class employee
{
int empid;
String empname;
int costprhour;
int salary;
public employee()
{
empname = null;
empid =0;
costprhour= 0;
salary = 0;
}

public employee(String n,int i,int c ,int s)
{
empname = n;
empid = i;
costprhour = c;
salary = s;
}
public int getempid()
{
return empid;
}
public String getempname()
{
return empname;
}
public int getcostprhour()
{
return costprhour;
}
public int getsalary()
{
return salary;
}
}

class test
{

  public static void main(String[] args){
FileReader fr = null;
BufferedReader br = null;
ArrayList<employee> emp = new ArrayList<employee>();
int count =0;
String d = JOptionPane.showInputDialog(null ,"enter number of hours per week:");
int duration = Integer.parseInt(d);
try
{
fr = new FileReader("input.txt");
br = new BufferedReader(br);
String[]tokens;
String line = br.readLine();
while( line != null)
{
tokens = line.split( " " );
String i = tokens[0];
String nm = tokens[1];
String c = tokens[2];
int id = Integer.parseInt(i);
int cost =Integer.parseInt(c);
int salary = cost*duration*4;
employee e = new employee (nm,id,cost,salary);
System.out.println("emp id  : " +id + "name :" +nm + "per hour cst:" + cost + "montly salary :"+ salary);
emp.add(e);
line = br.readLine();
count++;
}
}
catch(IOException e)
{
System.out.println(e);
}
int size = emp.size()+ 2;
JFrame frame = new JFrame();
JLabel l1,l2,l3,l4;
Container c = frame.getContentPane();
frame.setLayout(new GridLayout(size,4));
c.add(new JLabel("employee id"));
c.add(new JLabel("employee name"));
c.add(new JLabel("employee cost per hour"));
c.add(new JLabel("monthly salary"));
for(int i =0;i<emp.size();i++)
{
employee s = (employee)emp.get(i);
l1 = new JLabel (" " + s.getempid());
l2 = new JLabel (" " + s.getempname());
l3 = new JLabel (" " + s.getcostprhour());
l4 = new JLabel (" " + s.getsalary());
c.add(l1);
c.add(l2);
c.add(l3);
}
frame.setSize(400 , 300 );
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}


}


sample output


sample 

write a java program which contains two classesnamed as “Employee” and “Test” and one text file as input.txt. write a java program which contains two classesnamed as “Employee” and “Test” and one text file as input.txt. Reviewed by Shobhit Goel on May 19, 2016 Rating: 5

No comments:

Airtel Hackaton 2017

Powered by Blogger.