import java.util.*;
//namespace DoFactory.GangOfFour.Observer.RealWorld
//{
  
  // "Subject" 

  class Stock
  {
    String symbol;
    double price;
    private ArrayList investors;

    // Constructor 
    public Stock()
    {}
    public Stock(String symbol, double price)
    {
      this.symbol = symbol;
      this.price = price;
    }

    public void Attach(Investor investor)
    {
      investors.add(investor);
    }

    public void Detach(Investor investor)
    {
      investors.remove(investor);
    }

    public void Notify()
    {
      //foreach (Investor investor in investors)
    	int len=investors.size();
    	int i=0;
      while(i<len)
      {
    	Investor ii=(Investor)investors.get(i);
    	ii.Update(this);    	
      }
      System.out.println();
    }
    
    public double GetPrice()
    {
      return price; 
    }
    public void SetPrice (double value)
    {
        price = value;
        Notify(); 
    }
    

    public String getSymbol()
    {
       return symbol; 
    }
    public void setSymbol(String value)
    { 
    	symbol = value;
    }
  }

  // "ConcreteSubject" 

  class IBM extends Stock
  {
    // Constructor 
    public IBM(String symbol, double price)     
    {
    	this.symbol=symbol;
    	this.price=price;
    }
  }

  // "Observer" 

  interface IInvestor
  {
    void Update(Stock stock);
    Stock getStock();
    void setStock(Stock value);
  }

  // "ConcreteObserver" 

  class Investor implements IInvestor
  {
    private String name;
    private Stock stock;

    // Constructor 
    public Investor(String name)
    {
      this.name = name;
    }

    public void Update(Stock stock)
    {
      System.out.println("Notified {0} of {1}'s " +
        "change to {2:C}"+stock.symbol);
      stock.GetPrice();
    }

    // Property 
    public Stock getStock()
    {
      return stock; 
    }
    public void setStock(Stock value)
    { 
    	stock = value; 
    }
  }


// MainApp test application 

public  class Mainapp
  {
    static void Main()
    {
      // Create investors 
      Investor s = new Investor("Sorros");
      Investor b = new Investor("Berkshire");

      // Create IBM stock and attach investors 
      IBM ibm = new IBM("IBM", 120.00);
      ibm.Attach(s);
      ibm.Attach(b);

     
      // Change price, which notifies investors 
      ibm.SetPrice(120.10);
      ibm.SetPrice(121.00);
      ibm.SetPrice(120.50);
      ibm.SetPrice(120.75);

      // Wait for user 
      //Console.Read();
    }
  }
