import java.util.Stack;
public class StackPerson
{
public static void main(String args[])
{
// Create a new, empty stack
Stack lifo = new Stack();
// Let's add some items to it
Person person = new Person();
person.setFirstName("Elvis");
person.setLastName("Goodyear");
person.setAge(56);
System.out.print ( person
+ ": ");
String lastName = person.getLastName();
String firstName = person.getFirstName();
int age = person.getAge();
System.out.println(lastName
+ ", " + firstName + ". Age:" +
age);
lifo.push ( person
);
person = new Person();
person.setFirstName("Stanley");
person.setLastName("Clark");
person.setAge(8);
System.out.print ( person
+ ": ");
lastName = person.getLastName();
firstName = person.getFirstName();
age = person.getAge();
System.out.println(lastName
+ ", " + firstName + ". Age:" +
age);
lifo.push ( person
);
person = new Person();
person.setFirstName("Jane");
person.setLastName("Graff");
person.setAge(16);
System.out.print ( person
+ ": ");
lastName = person.getLastName();
firstName = person.getFirstName();
age = person.getAge();
System.out.println(lastName
+ ", " + firstName + ". Age:" +
age);
lifo.push ( person
);
person = new Person();
person.setFirstName("Nancy");
person.setLastName("Goodyear");
person.setAge(69);
System.out.print ( person
+ ": ");
lastName = person.getLastName();
firstName = person.getFirstName();
age = person.getAge();
System.out.println(lastName
+ ", " + firstName + ". Age:" +
age);
lifo.push ( person
);
System.out.println ("After pop()
--------------");
// Last in first out means reverse order
while ( !lifo.empty() )
{
person = (Person) lifo.pop();
System.out.print ( person
+ ": ");
lastName = person.getLastName();
firstName = person.getFirstName();
age = person.getAge();
System.out.println(lastName
+ ", " + firstName + ". Age:" +
age);
}
}
} // class: StackPerson
//-------------------------
class Person {
private String firstName;
private String lastName;
private int
age;
public String getFirstName() {
return
firstName;
}
public void setFirstName(String firstName) {
this.firstName
= firstName;
}
public String getLastName() {
return
lastName;
}
public void setLastName(String lastName) {
this.lastName
= lastName;
}
public int getAge() {
return
age;
}
public void setAge(int age) {
this.age
= age;
}
}
//class: Person