home / study / engineering / computer science / questions and answers / in java
ID: 3692820 • Letter: H
Question
home / study / engineering / computer science / questions and answers / in java language: serialization a) create an employee ...
Your question has been answered! Rate it below.
Let us know if you got a helpful answer.
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
import java.io.Serializable;
public class Employee implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
String idNumber;
String firstName, lastNames, phoneNumbers;
double salary;
int month, day;
public Employee() {
// TODO Auto-generated constructor stub
}
/**
* @param idNumber
* @param firstName
* @param lastNames
* @param phoneNumbers
* @param salary
* @param month
* @param day
*/
public Employee(String idNumber, String firstName, String lastNames,
String phoneNumbers, double salary, int month, int day) {
this.idNumber = idNumber;
this.firstName = firstName;
this.lastNames = lastNames;
this.phoneNumbers = phoneNumbers;
this.salary = salary;
this.month = month;
this.day = day;
}
/**
* @return the idNumber
*/
public String getIdNumber() {
return idNumber;
}
/**
* @param idNumber
* the idNumber to set
*/
public void setIdNumber(String idNumber) {
this.idNumber = idNumber;
}
/**
* @return the firstName
*/
public String getFirstName() {
return firstName;
}
/**
* @param firstName
* the firstName to set
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* @return the lastNames
*/
public String getLastNames() {
return lastNames;
}
/**
* @param lastNames
* the lastNames to set
*/
public void setLastNames(String lastNames) {
this.lastNames = lastNames;
}
/**
* @return the phoneNumbers
*/
public String getPhoneNumbers() {
return phoneNumbers;
}
/**
* @param phoneNumbers
* the phoneNumbers to set
*/
public void setPhoneNumbers(String phoneNumbers) {
this.phoneNumbers = phoneNumbers;
}
/**
* @return the salary
*/
public double getSalary() {
return salary;
}
/**
* @param salary
* the salary to set
*/
public void setSalary(double salary) {
this.salary = salary;
}
/**
* @return the month
*/
public int getMonth() {
return month;
}
/**
* @param month
* the month to set
*/
public void setMonth(int month) {
this.month = month;
}
/**
* @return the day
*/
public int getDay() {
return day;
}
/**
* @param day
* the day to set
*/
public void setDay(int day) {
this.day = day;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Employee [idNumber=" + idNumber + ", firstName=" + firstName
+ ", lastNames=" + lastNames + ", phoneNumbers=" + phoneNumbers
+ ", salary=" + salary + ", month=" + month + ", day=" + day
+ "]";
}
}
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.Scanner;
/**
* @author srinu
*
*/
public class TestSerialization {
/**
* @param args
*/
public static void main(String[] args) {
Scanner scanner = null;
try {
FileOutputStream fos = new FileOutputStream("employee.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
scanner = new Scanner(System.in);
Employee employees[] = new Employee[5];
System.out.println("Enter the 5 employees data from console: ");
for (int i = 0; i < 5; i++) {
String firstName, lastNames, phoneNumbers;
double salary;
int month, day;
System.out.print("Enter the employee id:");
String idNumber = scanner.next();
System.out.print("Enter the employee First name:");
firstName = scanner.next();
System.out.print("Enter the employee Last name:");
lastNames = scanner.next();
System.out.print("Enter the employee Phone Number:");
phoneNumbers = scanner.next();
System.out.print("Enter the employee Salary:");
salary = scanner.nextDouble();
System.out.print("Enter the employee hired month:");
month = scanner.nextInt();
System.out.print("Enter the employee hired day:");
day = scanner.nextInt();
employees[i] = new Employee(idNumber, firstName, lastNames,
phoneNumbers, salary, month, day);
}
for (int i = 0; i < employees.length; i++) {
oos.writeObject(employees[i]);
}
System.out.println("Saved successfully...");
oos.close();
fos.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
import java.io.FileInputStream;
import java.io.ObjectInputStream;
/**
* @author srinu
*
*/
class TestDeserialization {
/**
* @param args
*/
public static void main(String[] args) {
Employee employee;
try {
FileInputStream fis = new FileInputStream("employee.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
for (int i = 0; i < 5; i++) {
employee = (Employee) ois.readObject();
System.out.println(employee);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
OUTPUT:
Employee [idNumber=123, firstName=Srinivas, lastNames=Palli, phoneNumbers=12345, salary=34576.98, month=2, day=1]
Employee [idNumber=124, firstName=Pavan, lastNames=kumar, phoneNumbers=3467, salary=32456.32, month=3, day=2]
Employee [idNumber=23111, firstName=Srr, lastNames=dfd, phoneNumbers=233, salary=1343.0, month=2, day=3]
Employee [idNumber=2344, firstName=sdsdf, lastNames=dfdf, phoneNumbers=2344, salary=23333.0, month=2, day=2]
Employee [idNumber=34343, firstName=sadsads, lastNames=sfdsfds, phoneNumbers=221122, salary=33322.0, month=2, day=3]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.