package data; import java.lang.Comparable; import java.security.InvalidParameterException; import data.Money; import output.XMLizable; /** * class to represent quality in the game * * @author Rukmal Fernando * @author Hardik Kotecha * @author Radhika Srinivasan */ public class Quality implements Comparable, XMLizable { Float value; // float representation of quality /** * default constructor * * @exception RuntimeException */ public Quality() { throw new RuntimeException( "No constructor for default Quality" ); } /** * constructor * * @param value : Quality value */ public Quality( Float value ) { setQuality( value ); } /** * constructor * * @param pValue : value of Quality */ public Quality( double pValue) { setQuality( new Float( pValue ) ); } /*======================================================================== | getters and setters +-----------------------------------------------------------------------*/ /** * set the quality to v * * @param v : quality value to be set */ void setQuality( Float v ) { // determine if given quality is within proper bounds if ( v < 0 || v > 1 ) { throw new InvalidParameterException( "Invalid Quality Value" ); } value = v; } /** * get the quality * * @return Float */ public Float getValue() { return value; } /*----------------------------------------------------------------------- | end getters and setters +======================================================================*/ /** * generate the xml representation of a quality * * @return String */ public String generateXML() { Money tmp = new Money( value ); return tmp.generateXML(); } /** * print the XML representation of this class to the console */ public void print() { System.out.println( generateXML() ); } /** * compare this quality to the given quality * * @param Quality * @return int */ public int compareTo( Quality pQuality ) { Float current = getValue(); Float param = pQuality.getValue(); return current.compareTo( param ); } /** * determine the best price based on this quality * * @return Price */ public Price determineBestPrice() { Price newPrice = null; if ( this.compareTo( new Quality( 1.0 ) ) >= 0 ) newPrice = new Price( 1.0 ); else { Float newPriceVal = getValue() + new Float( 0.05 ); if(newPriceVal.floatValue() > 1.0f) newPriceVal = new Float(1.0f); newPrice = new Price( newPriceVal ); } return newPrice; } }