/**
 * This strategy discards just the phone call that 
 * is hung up and all phones directly added to the
 * conversation by this phone.  
 * Any calls added indirectly are retained.
 */
public class Strategy2 implements Strategy {
  
  private SearchableStack<Phone> ss;
    
  /**
   * Constructor for array implementation
   * @param size the maximum size of the conversation
   */
  public Strategy2( int size ) {
    ss = new ArraySearchableStack<Phone>( size );
  }
    
  /**
   * Constructor for ArrayList implementation
   */ 
  public Strategy2() {
    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 ) ) {
      SearchableStack<Phone> temp = new ArraySearchableStack<Phone>( ss.size() );
      Phone next = ss.pop();
      
      while ( ! next.equals( phone ) ) {
        
        if( ! phone.equals( next.fromPhone() ) ) {
          temp.push( next ) ;
        }
        next = ss.pop();
      }
      
      while( ! temp.isEmpty() ) {
        ss.push( temp.pop() );
      }
    }
  }
  
  /**
   * Return a string representation of the conversation
   */
  public String toString() {
    
    return ss.toString();
  }
  
}