package data;

import java.security.InvalidParameterException;

import data.Money;
import output.XMLizable;

/**
 * class to represent price in the game
 * 
 * @author Rukmal Fernando
 * @author Hardik Kotecha
 * @author Radhika Srinivasan
 */
public class Price implements XMLizable, Comparable<Price>
{
	private Float value;  // float representation of price
	
	/**
	 * default constructor
	 * @exception RuntimeException
	 */
	public Price()
	{
		throw new RuntimeException( "No contructor for default Price" );
	}
	
	/**
	 * constructor 
	 * 
	 * @param pValue : value of Price
	 */
	public Price( double pValue )
	{
		setValue( new Float( pValue ) );
	}
	
	/**
	 * constructor
	 * 
	 * @param pValue : Float for value of Price
	 */
	public Price( Float value )
	{
		setValue( value );
	}
	
	/*========================================================================
	|                           getters and setters
	+-----------------------------------------------------------------------*/
	
	/**
	 * set the Price to the given Float
	 * 
	 * @param Float v : value of Price
	 */
	void setValue( Float v )
	{
		// determine if given price is within proper bounds
		if ( v < 0 || v > 1 )
		{
			throw new InvalidParameterException( "Invalid Price Value " + v );
		}
		
		value = v;
	}
	
	/**
	 * get the value of price
	 * 
	 * @return Float : representation of Price
	 */
	public Float getValue()
	{
		return value;
	}
	
	/*-----------------------------------------------------------------------
	|                       end getters and setters
	+======================================================================*/
	
	/**
	 * generate the xml representation of price
	 * 
	 * @return String
	 */
	public String generateXML()
	{
		Money tmp = new Money( value );
		
		return new String( "<price>" + tmp.generateXML() + "</price>\n");
	}
	
	/**
	 * print the XML representation of this class to the console
	 */
	public void print()
	{
		System.out.println( generateXML() );
	}
	
	/**
	 * compare this price to the given price
	 * 
	 * @param pPrice : Price
	 * @return int
	 */
	public int compareTo( Price pPrice )
	{
		Float current = getValue();
		Float param = pPrice.getValue();
		
		return current.compareTo( param );
	}
}