hi All,
Suppose you want an array of customers...
Each customer can be printed. So, you define
an interface:
public interface Printable {
public void print();
}
and you implement it:
public class NamedCustomer implements Printable {
private String name;
private double netWorth;
public NamedCustomer(
String _name,
double _netWorth) {
name = _name;
netWorth = _netWorth;
}
public void print() {
System.out.println("Name:"+name+"\tNetworth:"+netWorth);
}
}
Then you make up your data-base, and print it:
public class CustomerArray implements Printable {
NamedCustomer list[] = {
new NamedCustomer("Frank",100000),
new NamedCustomer("Rob",50000),
new NamedCustomer("Velma",999999)
};
public static void main(String args[]){
CustomerArray ca = new CustomerArray();
ca.print();
}
public void print() {
for (int x=0; x < list.length; x++)
list[x].print();
}
}
Give it a try...see what you think!!
- DL
|