/*
 * Created on Jan 29, 2005
 *
 * Advanced Data Management Group
 * Center for Advanced Computing and Informatics Studies
 * University of Puerto Rico, Mayaguez
 * 
 */
package ece.uprm.admg.cs2.p1;

import java.util.StringTokenizer;

import ece.uprm.admg.cs2.util.Searchable;


/**
 * Class Part represents a part that is stored in the catalog of parts.
 * 
 * @author Manuel Rodriguez
 *
 */
public class Part implements Searchable{
    // private methods
    
    // Id of the part
    private String partId;
    
    // Name of the Part
    private String partName;
    
    // Color of the part
    private String partColor;
    
    // Price of the part 
    private float partPrice;
    
    // Material of the part
    private String partMaterial;
     

    /**
     * Defualt constructor 
     */
    public Part() {
        
        this.partId = "None";
        this.partName = "None";
        this.partColor = "None";
        this.partPrice = (float) 0.0;
        this.partMaterial = "None";      
    }
    
    /**
     * @param partId - The part id of the new part.
     * @param partName - The part name of the new part.
     * @param partColor - The part color of the new part.
     * @param partPrice - The part price of the new part.
     * @param partMaterial - The part material of the new part.
     */
    public Part(String partId, String partName, String partColor,
            float partPrice, String partMaterial) {
        this.partId = partId;
        this.partName = partName;
        this.partColor = partColor;
        this.partPrice = partPrice;
        this.partMaterial = partMaterial;
    }
  
    /**
     * Returns the part color.
     * @return Returns the part color.
     */
    public String getPartColor() {
        return partColor;
    }
    
    /**
     * Returns the part identifier
     * @return Returns the part Id.
     */
    public String getPartId() {
        return partId;
    }
    
    /**
     * Returns the part material 
     *  * @return Returns the part material.
     */
    public String getPartMaterial() {
        return partMaterial;
    }
    
    /**
     * Returns the part name
     *  * @return Returns the part name.
     */
    public String getPartName() {
        return partName;
    }
    
    /**
     * Retuerns the part price.
     * @return Returns the part price.
     */
    public float getPartPrice() {
        return partPrice;
    }
    
    /**
     * Sets the part color to a new color 
     * @param partColor - The part color to set.
     */
    public void setPartColor(String partColor) {
        this.partColor = partColor;
    }
    
    /**
     * Sets the part id to a new id value.
     * @param partId - The part Id to set.
     */
    public void setPartId(String partId) {
        this.partId = partId;
    }
    /**
     * Sets the part material to a new value.
     * @param partMaterial - The partMaterial to set.
     */
    public void setPartMaterial(String partMaterial) {
        this.partMaterial = partMaterial;
    }
    /**
     * Sets the part name to a new value.
     * @param partName - The partName to set.
     */
    public void setPartName(String partName) {
        this.partName = partName;
    }
    /**
     * Sets the price of the part to a new value.
     * @param partPrice - The partPrice to set.
     */
    public void setPartPrice(float partPrice) {
        this.partPrice = partPrice;
    }
    
    /**
     * Returns the key for this part. The key is a unique identifier
     * for the part. In this case, the key is the part id.
     * @return the key that uniquely identifies this part.
     */
    public Object getKey(){
        return this.partId;
    }
    
    /**
     * This method is to generate a string representing this Part.
     * @return a string representation of the value of this Part.
     * @see java.lang.Object#toString()
     */
    public String toString(){
        StringBuffer buf = null;
        
        buf = new StringBuffer(this.partId);
        buf.append(", ");
        buf.append(this.partName);
        buf.append(", ");
        buf.append(this.partColor);
        buf.append(", ");
        buf.append(this.partMaterial);
        buf.append(", ");
        buf.append(this.partPrice);
        return buf.toString(); 
  
    }

    /**
     * This method is used to convert a part from its string representation into
     * an instance of type Part.
     * @param info - the string representation of the part.
     * @return an instance of the part created from the input string.
     */
    public static Part parse(String info) {
        StringTokenizer strTok = new StringTokenizer(info, ",");
        Part result = null;
        String partId, partName, partColor, partMaterial;
        float partPrice;
        
        if (strTok.countTokens()!= 5){
            throw new IllegalArgumentException("Illegal string input format for Part.");
        }
        partId = strTok.nextToken();
        partName = strTok.nextToken();
        partColor = strTok.nextToken();
        partMaterial = strTok.nextToken();
        partPrice = Float.parseFloat(strTok.nextToken());
        result = new Part(partId, partName,partColor,partPrice,partMaterial);
        return result;
    }
}
