package com.hammurapi.common.extract.tests;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import com.hammurapi.store.StoreException;

/**
 * A class to test object store.
 * @author Pavel Vlasov
 *
 */
public class Account {
	
	public Account() {
		
	}
	
	public Account(String customer, int number, BigDecimal balance) {
		super();
		this.number = number;
		this.balance = balance;
		this.customer = customer;
	}

	private int number;
	private String customer;
	private List<Transaction> transactions = new ArrayList<Transaction>();
	private BigDecimal balance;
	private AtomicInteger balanceInquiries = new AtomicInteger();
	
	public String getCustomer() {
		return customer;
	}
	
	public void setCustomer(String customer) {
		this.customer = customer;
	}
	
	public int getNumber() {
		return number;
	}
	public void setNumber(int number) {
		this.number = number;
	}
	public BigDecimal getBalance() {
		// Artificial delay to test parallel processing.
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			throw new StoreException(e);
		}
		balanceInquiries.incrementAndGet();
		return balance;
	}
	public void setBalance(BigDecimal balance) {
		this.balance = balance;
	}
	public List<Transaction> getTransactions() {
		// Artificial delay to test parallel processing.
		try {
			Thread.sleep(150);
		} catch (InterruptedException e) {
			throw new StoreException(e);
		}
		return transactions;
	}
	
	public List<Transaction> getTransactionsImmediately() {
		return transactions;
	}
	
	/**	
	 * @return Number of times getBalance() was invoked.
	 */
	public int getNumberOfBalanceInquiries() {
		return balanceInquiries.get();
	}

	@Override
	public String toString() {
		return "Account [number=" + number + ", customer=" + customer
				+ ", balance=" + balance + ", balanceInquiries="
				+ balanceInquiries + "]";
	}
	

}
