package com.hammurapi.common.extract.tests;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;

import com.hammurapi.common.Observable;
import com.hammurapi.common.Observer;

/**
 * Account which notifies observers of changes.
 * @author Pavel Vlasov
 *
 */
public class ObservableAccount extends Account implements Observable<Account> {

	public ObservableAccount() {
		super();
	}

	public ObservableAccount(String customer, int number, BigDecimal balance) {
		super(customer, number, balance);
	}

	@Override
	public void addObserver(Observer<? super Account> o) {
		observers.add(o);		
	}

	@Override
	public void deleteObserver(Observer<? super Account> o) {
		observers.remove(o);		
	}
	
	private Collection<Observer<? super Account>> observers = new ArrayList<Observer<? super Account>>();
	
	public int countObservers() {
		return observers.size();
	}
	
	@Override
	public void setBalance(BigDecimal balance) {
		super.setBalance(balance);
		for (Observer<? super Account> o: observers) {
			o.update(null, this);
		}
	}

}
