Define a Java class called Employee. The class has data members and accompanying
ID: 3828540 • Letter: D
Question
Define a Java class called Employee. The class has data members
and accompanying accessor and mutator methods for each of the following six data items. (This involves creating the file Employee.java.)
* id (string)
* name (string)
* salary (double)
* department (string)
* position (string)
* years of service (integer)
Create a text (data) file containing data for at least five different
employees (objects). Let each data item sit on its own line in
the text file. For example, the first six lines of the file might look like:
‘Heap’ is a tree-based data-structure that satisfies the heap property. A max-heap is a complete binary tree in which the value in each internal node is greater than or equal to the values in the children of that node.
By having a heap (or an array that satisfies the heap property), it would be more efficient (generally faster) to perform important tasks on the array such as finding the maximum element in the array (and removing it) and sorting the array.
In this assignment, you will have to write a program that reads a list of employees from a file. The name of the file will be ‘Employee.txt’. The program should output the *sorted* array to a file called “SortedEmployee.txt”
Explanation / Answer
Hi,
Please see beloow classes and input/output sample.
Pleasecomment for any queries/feedbacks.
Thanks.
Employee.java
public class Employee {
private String id;
private String name;
private double salary;
private String department;
private String position;
private int yearsOfService;
public Employee() {
}
public Employee(String id, String name, double salary, String department,
String position, int yearsOfService) {
super();
this.id = id;
this.name = name;
this.salary = salary;
this.department = department;
this.position = position;
this.yearsOfService = yearsOfService;
}
//accessor and mutator methods
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public int getYearsOfService() {
return yearsOfService;
}
public void setYearsOfService(int yearsOfService) {
this.yearsOfService = yearsOfService;
}
}
EmployeeTester.java
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
public class EmployeeTester {
public static void main(String[] args) {
//Readingthe file contents
BufferedReader br = null;
FileReader fr = null;
Employee [] empArr = new Employee[6]; //Creating Emplyee Array
int counter =0;
try {
fr = new FileReader("Employee.txt");
br = new BufferedReader(fr);
String sCurrentLine;
while ((sCurrentLine = br.readLine()) != null) {//Looping through each line
String input[] = sCurrentLine.split(" ");//Splitting each line to get the values
String id= input[0];
String name = input[1];
double salary = Double.valueOf(input[2]);
String dept = input[3];
String position =input[4];
int service = Integer.valueOf(input[5]);
empArr[counter] = new Employee(id,name,salary,dept,position,service);
counter++;
System.out.println(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
}
//Sorting Array based on Employee names
Arrays.sort(empArr,new Comparator<Employee>() {
@Override
public int compare(Employee o1, Employee o2) {
return o1.getName().compareTo(o2.getName());
}
});
String content = printAray(empArr);
writeToFile(content); //Writinmg to file
}
public static String printAray(Employee[] emparray){
String retString ="";
Employee currEmp =null;
for(int i=0;i<emparray.length;i++){
currEmp = emparray[i];
retString= retString+currEmp.getName()+" "
+currEmp.getId()+" "
+currEmp.getPosition()+" "
+currEmp.getSalary()+" "
+currEmp.getDepartment()+" ";
}
return retString;
}
/**
* To write the output to a file
* @param args
*/
public static void writeToFile(String content) {
BufferedWriter bw = null;
FileWriter fw = null;
try {
fw = new FileWriter("SortedEmployee.txt");
bw = new BufferedWriter(fw);
bw.write(content);
bw.flush();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Employee.txt
106 Helen 10000 UG Teacher 5
101 Calvin 8000 PG Professor 10
108 Dwayne 5000 CS Lab 7
103 Clare 4000 EC Teacher 5
102 Walter 6500 CS Assistant 8
105 Cooper 6700 CS Assistant 8
SortedEmployee.txt
Calvin 101 Professor 8000.0 PG
Clare 103 Teacher 4000.0 EC
Cooper 105 Assistant 6700.0 CS
Dwayne 108 Lab 5000.0 CS
Helen 106 Teacher 10000.0 UG
Walter 102 Assistant 6500.0 CS
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.