/*
 * 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.*;

/**
 * Class Car is used to present the information about a particular car model.
 * 
 * @author Manuel Rodriguez
 *
 */
public class Car implements Searchable{
    // private fields
    
    // The identifier for this car model.
    private String carId;
    
    // The Brand of the car
    private String carBrand;
    
    // The model of the car
    private String carModel;
    
    // Year of the car
    private String carYear;
    
    // Price of the car
    private float carPrice;
    
    // Color of the car
    private String carColor; 

    // The ids of the parts used by the car
    private Set carPartsIds;
    
    
    /**
     * Default constructor. Used to create new Car object with no information.
     */
    public Car() {
        this.carId = "None";
        this.carBrand = "None";
        this.carModel = "None";
        this.carYear = "None";
        this.carPrice = 0;
        this.carColor = "None";
        this.carPartsIds = new DynamicSet();
       
    }
    
    /**
     * Constructor based on a set of attributes to create a new car instance.
     * @param carId - the identifier for the car.
     * @param carBrand - the brand for the car.
     * @param carModel - the model for the car.
     * @param carYear - the car year.
     * @param carPrice - the price a brand new car of this type.
     * @param carColor - the color of the car.
     */
    public Car(String carId, String carBrand, String carModel, String carYear,
            float carPrice, String carColor) {
        this.carId = carId;
        this.carBrand = carBrand;
        this.carModel = carModel;
        this.carYear = carYear;
        this.carPrice = carPrice;
        this.carColor = carColor;
        this.carPartsIds = new DynamicSet();
    }
    
    /**
     * @return the carBrand.
     */
    public String getCarBrand() {
        return carBrand;
    }
    
    /**
     * @return the carColor.
     */
    public String getCarColor() {
        return carColor;
    }
    
    /**
     * @return the carModel.
     */
    public String getCarModel() {
        return carModel;
    }
    
    /**
     * @return the carPrice.
     */
    public float getCarPrice() {
        return carPrice;
    }
    
    /**
     * @return the carYear.
     */
    public String getCarYear() {
        return carYear;
    }
    
    /**
     * @param carBrand - the carBrand to set.
     */
    public void setCarBrand(String carBrand) {
        this.carBrand = carBrand;
    }
    
    /**
     * @param carColor - the carColor to set.
     */
    public void setCarColor(String carColor) {
        this.carColor = carColor;
    }
    
    /**
     * @param carModel - the carModel to set.
     */
    public void setCarModel(String carModel) {
        this.carModel = carModel;
    }
    
    /**
     * @param carPrice - the carPrice to set.
     */
    public void setCarPrice(float carPrice) {
        this.carPrice = carPrice;
    }
    
    /**
     * @param carYear - the carYear to set.
     */
    public void setCarYear(String carYear) {
        this.carYear = carYear;
    }
    
    public void addPart(Part newPart){
        this.carPartsIds.add(newPart);
    }
    
    public boolean isPart(Part carPart){
       return this.carPartsIds.contains(carPart);
    }
    
    public boolean removePart(Part carPart){
        return this.carPartsIds.remove(carPart);
    }
    
    public int getNumParts(){
        return this.carPartsIds.size();
    }
    
    public String toString(){
        StringBuffer buf = null;
        
        buf = new StringBuffer(this.carBrand);
        buf.append(", ");
        buf.append(this.carModel);
        buf.append(", ");
        buf.append(this.carYear);
        buf.append(", ");
        buf.append(this.carColor);
        buf.append(", ");
        buf.append(this.carPrice);
        return buf.toString(); 
    }
    
    public Object getKey(){
        return carId;
    }

    /**
     * 
     * @param info
     * @return
     */
    public static Car parse(String info) {
        StringTokenizer strTok = new StringTokenizer(info, ",");
        Car result = null;
        String carId, carBrand, carModel, carYear, carColor;
        float carPrice;
        
        if (strTok.countTokens()!= 6){
            throw new IllegalArgumentException("Illegal string input format for car.");
        }
        carId = strTok.nextToken();
        carBrand = strTok.nextToken();
        carModel = strTok.nextToken();
        carYear = strTok.nextToken();
        carPrice = Float.parseFloat(strTok.nextToken());
        carColor = strTok.nextToken();
        result = new Car(carId, carBrand,carModel,carYear,carPrice, carColor);
        return result;
    }
}
