Here is a complete example of a Vector
being used to store and print customers...
This is a stand-alone program!
Regards,
- DL
import java.util.Vector;
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);
}
}
public class CustomerVector
implements Printable {
private Vector v = new Vector();
public void init() {
v.addElement(
new NamedCustomer(
"Frank",100000));
v.addElement(
new NamedCustomer(
"Rob",50000));
v.addElement(
new NamedCustomer(
"Velma",999999));
}
public static void main(String args[]){
CustomerVector cv =
new CustomerVector();
cv.init();
cv.print();
}
public void print() {
for (int x=0; x < v.size(); x++)
((Printable)v.elementAt(x)).print();
}
}
public interface Printable {
public void print();
}
|