package lab01;
import java.util.HashMap;

/**
 * Keeps track of latest quotes for all stocks
 * @author CS-140 Teaching Staff
 *
 */
public class Quotes {
	
	/* Fields */
	// Map from company to latest quote for that company
	private HashMap<String, Quote> quoteMap; 
	
	/**
	 * Create a new list of quotes
	 */
	public Quotes() {
		quoteMap = new HashMap<String,Quote>();
	}
	
	/**
	 * Adds a quote to the list of quotes
	 * 
	 * Will only add the quote if either no previous quote exists for the 
	 * specified stock (company) or if this quote is newer than the quote 
	 * on file for this stock
	 * @param q - A quote object to add to the list of quotes
	 * @return true if quote is added, false otherwise
	 */
	public boolean addQuote(Quote q) {
		String company = q.getCompany();
		if (quoteMap.containsKey(company)) {
			if (q.isNewer(quoteMap.get(company))) {
				quoteMap.replace(company, q);
				return true;
			}
		} else {
			quoteMap.put(company, q);
			return true;
		}
		return false;
	}
	
	/**
	 * Look up the latest quote for a stock
	 * @param company - Ticker tape symbol of stock to find
	 * @return The current price of that stock (0.00 if no quote is available)
	 */
	public double getPrice(String company) {
		if (quoteMap.containsKey(company)) {
			return quoteMap.get(company).getPrice();
		}
		return 0.0; // No quote available
	}
	
}
