import java.util.*;
/**
 * Illustrate stack and use ArrayList
 * Suzanne Balik, 19 Apr 2003
 */

public class AnsweringMachine {

  
  private ArrayList messages;
  
  public AnsweringMachine() {
    messages = new ArrayList();
  }
  
  public boolean addMessage(String message) {
  
      messages.add(new PhoneMessage(message));
      
      return true;
    
  }
  
  public String lastMessage() {
    if (messages.size() > 0) {
      PhoneMessage pm = (PhoneMessage)messages.remove(messages.size() - 1);
      return pm.toString();
    }
    else
      return "No messages";
  }
  
  public int numberOfMessages() {
    return messages.size();
  }
  
  public boolean hasMessages() {
    if (messages.size() > 0)
      return true;
    else
      return false;
  }
  
  public String toString() {
  
    String s = "";
    for (int i = 0; i < messages.size(); i++)
      s += ((PhoneMessage)(messages.get(i))).toString() + "\n";
    return s;
  }
  
  public static void main(String[] args) {
  
    AnsweringMachine machine = new AnsweringMachine();
    
    System.out.println("Last message: " + machine.lastMessage());
    
    System.out.println("\nMessages: " + machine);
    
    machine.addMessage("Call home");
    
    if (!machine.addMessage("Doctor appt. tomorrow at 9:00"))
      System.out.println("Message NOT added");
    
    if (!machine.addMessage("Party tonight on Brent Road"))
      System.out.println("Message NOT added");
    
    System.out.println("\nYou have " + machine.numberOfMessages() +
                       " messages");
    
    System.out.println("\nMessages: \n" + machine);
    
    System.out.println("\nLast message: " + machine.lastMessage());
    
    System.out.println("\nMessages: \n" + machine);
    
    System.out.println("\nHave messages?: " + machine.hasMessages());
    
    System.out.println("\nLast message: " + machine.lastMessage());
	
    System.out.println("\nLast message: " + machine.lastMessage());
    
    System.out.println("\nLast message: " + machine.lastMessage());
  }
}
/*OUTPUT:
csc% java AnsweringMachine
eos% java AnsweringMachine
Last message: No messages

Messages: 

You have 3 messages

Messages: 
Sat Apr 19 13:46:08 EDT 2003 Call home
Sat Apr 19 13:46:08 EDT 2003 Doctor appt. tomorrow at 9:00
Sat Apr 19 13:46:08 EDT 2003 Party tonight on Brent Road


Last message: Sat Apr 19 13:46:08 EDT 2003 Party tonight on Brent Road

Messages: 
Sat Apr 19 13:46:08 EDT 2003 Call home
Sat Apr 19 13:46:08 EDT 2003 Doctor appt. tomorrow at 9:00


Have messages?: true

Last message: Sat Apr 19 13:46:08 EDT 2003 Doctor appt. tomorrow at 9:00

Last message: Sat Apr 19 13:46:08 EDT 2003 Call home

Last message: No messages

*/