In Java language: Serialization a) Create an Employee class in which you can sto
ID: 3692133 • Letter: I
Question
In Java language:
Serialization
a)
Create an Employee class in which you can store employees’ identification/employee number, first and last names, phone numbers, employee salary, the month and day that each employee was hired. Write an application that can prompt the user to capture employee data and uses serialization to store each employees’ record to a file called (whatever you like).dat.
In order to accomplish the above task, you need to do the following:
Declare six instance variables for the pieces of data about an employee as listed above in (a) i.e. private int employeeNumber
private String firstName
private String lastName, etc.
Create two constructors; one default and the other with parameters.
You are required to provide appropriate getters and setters.
Create an output stream for file (whatever you like).dat to facilitate the writing of an object to a file.
Create an Employee[] array that will help in capturing employee data and writing it to a file as an array object.
Deserialization:
Create a program that reads the file created in Question#1 (a) above displaying each employee’s data to the screen.
Explanation / Answer
Hello there ,
Please find below code and it's O/P.
import java.io.Serializable;
import java.util.Date;
/**
* Represents an employee class containing basic employee detail .
* @author dipal.prajapati
*
*/
public class Employee implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private int employeeNumber;
private String firstName;
private String lastName;
private String phoneNumber;
private int salary;
private Date dateOfJoining;
public Employee() {
super();
}
public Employee(int employeeNumber, String firstName, String lastName, String phoneNumber, int salary, Date dateOfJoining) {
super();
this.employeeNumber = employeeNumber;
this.firstName = firstName;
this.lastName = lastName;
this.phoneNumber = phoneNumber;
this.salary = salary;
this.dateOfJoining = dateOfJoining;
}
public int getEmployeeNumber() {
return employeeNumber;
}
public void setEmployeeNumber(int employeeNumber) {
this.employeeNumber = employeeNumber;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public Date getDateOfJoining() {
return dateOfJoining;
}
public void setDateOfJoining(Date dateOfJoining) {
this.dateOfJoining = dateOfJoining;
}
@Override
public String toString() {
return "Employee [employeeNumber=" + employeeNumber + ", firstName=" + firstName + ", lastName=" + lastName + ", phoneNumber="
+ phoneNumber + ", salary=" + salary + ", dateOfJoining=" + dateOfJoining + "]";
}
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
public class SerializationTutorial {
static SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd-MM-yyyy");
public static void main(String args[]) {
List<Employee> listOfEmployees = deserialize(); // Read back old records
if (listOfEmployees.size() != 0) {
System.out.println("Restroing old records of employee from file.");
}
Scanner scanner = new Scanner(System.in);
System.out.println("No of employee you want to enter:");
int employeeNumber, salary;
String firstName, lastName, phoneNumber;
Date dateOfJoining = null;
int noOfEmp = scanner.nextInt();
Employee employee = null;
while (noOfEmp > 0) {
System.out.println("Enter Employee Detail below :");
System.out.println("Employee number: ");
employeeNumber = scanner.nextInt();
System.out.println("Employee First Name: ");
firstName = scanner.next();
System.out.println("Employee Last Name: ");
lastName = scanner.next();
System.out.println("Employee Phone number: ");
phoneNumber = scanner.next();
System.out.println("Employee salary: ");
salary = scanner.nextInt();
System.out.println("Employee Date Of Joining (dd-MM-yyyy) : ");
try {
dateOfJoining = DATE_FORMAT.parse(scanner.next());
} catch (ParseException e) {
e.printStackTrace();
}
employee = new Employee(employeeNumber, firstName, lastName, phoneNumber, salary, dateOfJoining);
listOfEmployees.add(employee);
noOfEmp--;
}
System.out.println("--Serializing Employees --");
searilizeEmployees(listOfEmployees);
System.out.println("--Deserializing Employees --");
List<Employee> list = deserialize();
System.out.println("List of Employees in file:");
for (Employee emp : list) {
System.out.println(emp.toString());
}
}
public static void searilizeEmployees(List<Employee> employees) {
try {
FileOutputStream fileOut = new FileOutputStream("employee.dat");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(employees);
out.close();
fileOut.close();
} catch (IOException ex) {
}
}
public static List<Employee> deserialize() {
List<Employee> employees = null;
try {
ObjectInputStream in = new ObjectInputStream(new FileInputStream("employee.dat"));
employees = (List<Employee>) in.readObject();
in.close();
} catch (Exception e) {
}
return employees;
}
}
====O/P===
Restroing old records of employee from file.
No of employee you want to enter:
1
Enter Employee Detail below :
Employee number:
76868
Employee First Name:
Jinal
Employee Last Name:
Dave
Employee Phone number:
32443343
Employee salary:
78000
Employee Date Of Joining (dd-MM-yyyy) :
5-05-2015
--Serializing Employees --
--Deserializing Employees --
List of Employees in file:
Employee [employeeNumber=3456, firstName=Helan, lastName=Kelar, phoneNumber=45404334, salary=45999, dateOfJoining=Thu May 07 00:00:00 IST 2015]
Employee [employeeNumber=1234, firstName=Hemant, lastName=Prajapati, phoneNumber=4662993320, salary=45000, dateOfJoining=Fri Jan 09 00:00:00 IST 2015]
Employee [employeeNumber=3454, firstName=Dipal, lastName=Prajapati, phoneNumber=4734383049, salary=50000, dateOfJoining=Fri Jan 23 00:00:00 IST 2015]
Employee [employeeNumber=2345, firstName=Gina, lastName=Gupta, phoneNumber=3889343, salary=46000, dateOfJoining=Mon Sep 03 00:00:00 IST 2012]
Employee [employeeNumber=76868, firstName=Jinal, lastName=Dave, phoneNumber=32443343, salary=78000, dateOfJoining=Tue May 05 00:00:00 IST 2015]
Here it will restore all the record in file before every exection of the program . Hence , It will retain data of all execution .
Let me know if you have doubts.
Thanks.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.