April 2004
Intermediate to advanced
606 pages
20h 4m
English
The com.mycompany.expense.ReportEntry class,
shown in Example 5-1, is
a simple bean, with properties for all expense report entry
items—the date, the expense type, and the amount—plus the
entry’s ID, unique within a
Report.
package com.mycompany.expense;
import java.io.Serializable;
import java.util.Date;
public class ReportEntry implements Serializable {
private int id = -1;
private Date date;
private int type;
private double amount;
public ReportEntry( ) {
}
public ReportEntry(ReportEntry src) {
this.setId(src.getId( ));
this.setDate(src.getDate( ));
this.setType(src.getType( ));
this.setAmount(src.getAmount( ));
}
public int getId( ) {
return id;
}
public void setId(int id) {
this.id = id;
}
public Date getDate( ) {
if (date == null) {
date = new Date( );
}
return date;
}
public void setDate(Date date) {
this.date = date;
}
public int getType( ) {
return type;
}
public void setType(int type) {
this.type = type;
}
public double getAmount( ) {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
public String toString( ) {
return "id: " + id + " date: " + date + " type: " + type +
" amount: " + amount;
}
}Each property is represented by standard JavaBeans accessor methods:
getId() and setId(),
getDate() and setDate(),
getType() and setType(), and
getAmount() and setAmount().
The ReportEntry class also has a copy constructor, i.e., a constructor that initializes the new instance’s properties to the ...
Read now
Unlock full access