/**
 * This strategy discards the hung up phone and all calls that
 * joined the conversation after the hung up call.
 */
public class Strategy1 implements Strategy {
  
  private SearchableStack<Phone> ss; 
  
  /**
   * Constructor for array implementation
   * @param size the maximum size of the conversation
   */   
  public Strategy1( int size ) {
    ss = new ArraySearchableStack<Phone>( size ); 
  }
  
  /**
   * Constructor for ArrayList implementation
   */ 
  public Strategy1() {
    ss = new ArrayListSearchableStack<Phone>();
  }
    
  /**
   * Add a phone to the conversation
   * @param fromPhone a phone already part of the 
   * conversation
   * @param toPhone a phone to be added to the 
   * conversation
   */
  public void makeCall( Phone fromPhone, Phone toPhone ) {
    
    if( ss.search( fromPhone ) || fromPhone == null && ! ss.search( toPhone ) ) {
      ss.push(toPhone);
      toPhone.setFromPhone( fromPhone );
    }
  }

  /**
   * Hang up the phone
   * @param phone the phone hung up
   */
  public void hangUp(Phone phone) {
    
    if( ss.search( phone ) ) {
      while ( ! phone.equals( (Phone) ss.pop() ) ) {
        ;
      }
    }
  }
  
  /**
   * Return a string representation of the conversation
   */
  public String toString() {
    
    return ss.toString();
  }
  
}