Lecture Notes – Serializing Employee Class – Incomplete…

Driver

packagecsu.matos;

importjava.io.FileNotFoundException;

importjava.io.FileOutputStream;

importjava.io.IOException;

importjava.io.ObjectOutputStream;

importjava.util.ArrayList;

importcsu.matos.model.Employee;

publicclassCompanyDriver {

//Author:V. Matos

//Date:May-19-2015

//Goal:Define and Test a simple POJO defining an Employee

//class.

//------

publicstaticvoid main(String[] args)

throwsIOException {

Employee e1 = newEmployee("Daenerys", "Targaryan",

"123 Dragon Rd", 1000000 );

Employee e2 = newEmployee("Tyrion", "Lannister",

"123 Vino Rd", 2 );

System.out.println(e1 );

System.out.println(e2 );

ArrayList<Employee> list = newArrayList>();

list.add(e1);

list.add(e2);

System.out.println(list );

// Serialize Circle object

FileOutputStreamfileOut = newFileOutputStream("c:/temp/mypeople.ser");

ObjectOutputStreamobjOut = newObjectOutputStream(fileOut);

objOut.writeObject(list);

objOut.close();

fileOut.close();

System.out.println("Done..." );

}

}

Employee

packagecsu.matos.model;

importjava.io.Serializable;

publicclass Employee implements Comparable<Employee>,

Serializable {

//class variables

private String firstName;

private String lastName;

private String address;

privatedoublesalary;

//constructor(s)

public Employee(String firstName, String lastName, String address,

doublesalary) {

this.firstName = firstName;

this.lastName = lastName;

this.address = address;

this.salary = salary;

}

public Employee() {

}

//Accessors - Mutators

public String getFirstName() {

returnfirstName;

}

publicvoidsetFirstName(String firstName) {

this.firstName = firstName;

}

public String getLastName() {

returnlastName;

}

publicvoidsetLastName(String lastName) {

this.lastName = lastName;

}

public String getAddress() {

returnaddress;

}

publicvoidsetAddress(String address) {

this.address = address;

}

publicdoublegetSalary() {

returnsalary;

}

publicvoidsetSalary(doublesalary) {

this.salary = salary;

}

// ------

//try later - remove comment markers on custom toString() method

@Override

public String toString() {

return"Employee [firstName=" + firstName + ", lastName=" + lastName

+ ", address=" + address + ", salary=" + salary + "]";

}

@Override

publicboolean equals(Object obj) {

Employee other = (Employee) obj;

if(this.getFirstName().equals(other.getFirstName()) ){

returntrue;

}

returnfalse;

}

@Override

publicintcompareTo(Employee other) {

if (this.getSalary() == other.getSalary() ){

return 0;

}

if ( this.getSalary() > other.getSalary() ){

return 1;

}else {

return -1;

}

}

}

Console

Employee [firstName=Daenerys, lastName=Targaryan, address=123 Dragon Rd, salary=1000000.0]

Employee [firstName=Tyrion, lastName=Lannister, address=123 Vino Rd, salary=2.0]

[Employee [firstName=Daenerys, lastName=Targaryan, address=123 Dragon Rd, salary=1000000.0], Employee [firstName=Tyrion, lastName=Lannister, address=123 Vino Rd, salary=2.0]]

Done...

Data in the c:/temp/mypeople.ser disk file

¬í srjava.util.ArrayListx Ò™Ça I sizexp w srcsu.matos.model.Employeek]uDŸ†øD salaryLaddresstLjava/lang/String;L firstNameq ~ L lastNameq ~ xpA.„€ t
123 Dragon RdtDaenerystTargaryansq ~ @ t
123 VinoRdtTyriontLannisterx