package data; import output.XMLizable; import utilities.NameGenerator; /** * class to represent a derivative object * * @author Rukmal Fernando * @author Hardik Kotecha * @author Radhika Srinivasan */ public class Derivative implements XMLizable { private String name; // name of the derivative private String creator; // creator of the derivative private Price price; // price of the derivative private DerivativeType type; // type of the derivative private String boughtBy; // player that purchased the derivative private RawMaterial rawMaterial; // raw material composing derivative private FinishedProduct finishedProduct; // finished product made from derivative /** * default constructor * * @exception RuntimeException */ public Derivative() { throw new RuntimeException( "No constructor for default DerivativeType" ); } /** * constructor for derivative * * @param pName : name of the derivative * @param pType : type of the derivative * @param pCreator : type of the derivative * @param pPrice : price of the derivative */ public Derivative( String pName, String pCreator, Price pPrice, DerivativeType pType ) { setName( pName ); setType( pType ); setCreator( pCreator ); setPrice( pPrice ); setBoughtBy( null ); setRawMaterial( null ); setFinishedProduct( null ); } /** * constructor for derivative * * @param pCreator : creator of the derivative * @param pType : type of the derivative * @param pPrice : price of the derivative */ public Derivative( String pCreator, Price pPrice, DerivativeType pType ) { setName( NameGenerator.getNewName() ); setType( pType ); setCreator( pCreator ); setPrice( pPrice ); setBoughtBy( null ); setRawMaterial( null ); setFinishedProduct( null ); } /** * constructor for derivative * * @param pName : name of the derivative * @param pType : type of the derivative * @param pCreator :type of the derivative * @param pPrice : price of the derivative * @param pBoughtBy : purchaser of derivative * @param pRawMaterial : initial raw material * @param pFinishedProduct : resulting finished product */ Derivative( String pName, String pCreator, Price pPrice, DerivativeType pType, String pBoughtBy, RawMaterial pRawMaterial, FinishedProduct pFinishedProduct ) { setName( pName ); setType( pType ); setCreator( pCreator ); setPrice( pPrice ); setBoughtBy( pBoughtBy ); setRawMaterial( pRawMaterial ); setFinishedProduct( pFinishedProduct ); } /*======================================================================== | getters and setters +-----------------------------------------------------------------------*/ /** * get the name of the derivative * * @return string : name of derivative */ public String getName() { return name; } /** * set the name of the derivative * * @param pName : String the derivative name is set to */ private void setName( String pName ) { name = pName; } /** * get the type of the derivative * * @return RawMaterialType : type of derivative */ public DerivativeType getType() { return type; } /** * set the type of the derivative * * @param pType : RawMaterialType the type is set to */ private void setType( DerivativeType pType ) { type = pType; } /** * get the creator of the derivative * * @return string : creator of derivative */ public String getCreator() { return creator; } /** * set the creator of the derivative * * @param pCreator : string the creator is set to */ private void setCreator( String pCreator ) { creator = pCreator; } /** * get the price of the derivative * * @return Price : price of derivative */ public Price getPrice() { return price; } /** * set the price of the derivative * * @param pPrice : Price the derivative price is set to */ public void setPrice( Price pPrice ) { price = pPrice; } /** * get the player that bought the derivative * * @return Player : purchaser of derivative */ public String getBoughtBy() { return boughtBy; } /** * set the boughtBy of the derivative * * @param pPrice : string the derivative boughtBy field is set to */ private void setBoughtBy( String pBoughtBy ) { boughtBy = pBoughtBy; } /** * get the raw material of the derivative * * @return RawMaterial : raw material of the derivative */ public RawMaterial getRawMaterial() { return rawMaterial; } /** * set the RawMaterial of the derivative * * @param pRawMaterial : RawMaterial the derivative rawMaterial field is set to */ public void setRawMaterial( RawMaterial pRawMaterial ) { rawMaterial = pRawMaterial; } /** * get the resulting FinishedProduct * * @return FinishedProduct : FinishedProduct of the derivative */ public FinishedProduct getFinishedProduct() { return finishedProduct; } /** * set the FinishedProduct of the derivative * * @param pFinishedProduct : FinishedProduct the derivative FinishedProduct is set to */ public void setFinishedProduct( FinishedProduct pFinishedProduct ) { finishedProduct = pFinishedProduct; } /*----------------------------------------------------------------------- | end getters and setters +======================================================================*/ /** * generate the xml representation of a derivative * * @return String */ public String generateXML() { String ret = new String( "\n" + "" + getName() + "\n" + "" + getCreator() + "\n" + getPrice().generateXML() + getType().generateXML() ); if( getBoughtBy() != null ) ret = ret.concat( "" + getBoughtBy() + "\n" ); if( getRawMaterial() != null ) ret = ret.concat( getRawMaterial().generateXML() ); if( getFinishedProduct() != null ) { ret = ret.concat( getFinishedProduct().generateXML() ); } ret = ret.concat( "\n" ); return ret; } /** * print the XML representation of this class to the console */ public void print() { System.out.println( generateXML() ); } /** * does this derivative need a raw material? * * @return boolean */ public boolean needRawMaterial() { return ( boughtBy != null && rawMaterial == null ); } /** * does this derivative need a finished product? * * @return boolean */ public boolean needFinishedProduct() { return ( boughtBy != null && rawMaterial != null && finishedProduct == null ); } /** * reprice this derivative and return a new one with a different name * * @param pPlayerName : String * @return Derivative */ public Derivative reprice( String pPlayerName ) { // Find price Derivative newDerivativeTest = new Derivative(pPlayerName, new Price(1), this.getType()); newDerivativeTest.adjustForDelivery(true); newDerivativeTest.adjustForFinishing(); float repricedQuality = newDerivativeTest.getFinishedProduct().getQuality().getValue().floatValue(); float price = this.getPrice().getValue(); float newPriceFloat = (float)Math.min(price - 0.01, repricedQuality + 0.1); Price newPrice = new Price(newPriceFloat); //Derivative newDerivative = new Derivative(this.getName(), pPlayerName, newPrice, this.getType()); Derivative newDerivative = new Derivative(pPlayerName, newPrice, this.getType()); // Mark the name as special to handle re-pricing newDerivative.setName("X" + newDerivative.getName()); return newDerivative; } /** * add a raw material to this derivative to prepare it for delivery */ public void adjustForDelivery() { adjustForDelivery(getName().startsWith("X")); } /** * add a raw material to this derivative to prepare it for delivery */ public void adjustForDelivery(boolean repriced) { DerivativeType derivativeType = getType(); if (repriced) { System.out.println("Delivering repriced derivative " + getName()); setRawMaterial( derivativeType.generateRepricedRawMaterial() ); } else { System.out.println("Delivering own derivative " + getName()); setRawMaterial( derivativeType.generateRawMaterial() ); } } /** * add a finished product to this derivative to finish it */ public void adjustForFinishing() { RawMaterial derivativeRawMaterial = getRawMaterial(); setFinishedProduct( derivativeRawMaterial.generateFinishedProduct() ); float finalQ = getFinishedProduct().getQuality().getValue().floatValue(); float price = getPrice().getValue().floatValue(); if (finalQ < price) { //System.out.println("Lost money on " + getName() + ". Price = " + price + ". Quality = " + finalQ); } } /** * set the boughtBy field to the given name * * @param pName : String */ public void adjustForBuying( String pName ) { setBoughtBy( pName ); } /** * has this derivative been purchased? * * @return boolean */ public boolean purchased() { return ( getBoughtBy() != null ); } /** * is the given creator name the same as this derivative creator name * * @param pCreatorName : String * @return boolean */ public boolean sameCreator( String pCreatorName ) { return ( getCreator().compareTo( pCreatorName ) == 0 ); } }