/**
 * Class Item.
 * 
 * <h4>ITI 1120 Fall 2011, Assignment 5, Question 2</h4>
 * <p>
 * This class represents an item that could be purchased 
 * <p>
 * There are three types of items that could be purchased.  Constant type codes
 * for each of these are stored as public static constants.
 * <p>
 * Specific items store the the title, type, and number of copies available for
 * each item. The cost of the item is also stored.
 * <p>
 * Operations that are provided are as follows:
 * <ul>
 * <li> ask for the item's title</li>
 * <li> ask for the item's type</li>
 * <li> ask for the number of copies available</li>
 * <li> increment or decrement the number of copies available</li>
 * <li> ask for the cost of a specific item</li>
 * <li> change the cost of a specific item</li>
 * <li> translate the type code to a meaningful string</li>
 * </ul>
 */
class Item
{
    /**
     * Useful type constants.
     */
    public static final int BOOK = 0;
    public static final int DVD = 1;
    public static final int GAME = 2;
    
    /**
     * Type of the item.
     * <p>
     * This value should be one of the static constants defined above.
     */
    private int type;
    
    /**
     * The item's title.
     */
    private String title;
    
    /**
     * The item's price.
     */
    private int price;
    
    /**
     * The number of copies of this item that are available.
     */
    private int copiesAvailable;
    
    /**
     * Constructor of a new item which sets the type, title, 
     * price, and the number of copies.
     * 
     * @param itemType
     *            The item type: should be one of the static constants.
     * @param itemTitle
     *            The item's title
     * @param price
     *            The cost of the item
     * @param numCopies
     *            The number of copies available
     */
    public Item( int itemType, String itemTitle, int itemPrice, int numCopies )
    {
        // Copy parameters to instance variables.
        
        this.type = itemType;
        this.title = itemTitle;
        this.price = itemPrice;
        this.copiesAvailable = numCopies;
    }
    
    /**
     * Returns the price of the item.
     * 
     * @return Price of the item.
     */
    public int getPrice( )
    {
        return this.price;
    }
    
    /**
     * Modifies the price of the item.
     * 
     * @param itemPrice
     *            The updated price.
     */
    public void setPrice( int itemPrice )
    {
        this.price = itemPrice;
    }
    
    /**
     * Returns the number of copies of this item that are currently available.
     * 
     * @return The number of available copies.
     */
    public int getCopiesAvailable( )
    {
        return this.copiesAvailable;
    }
    
    /**
     * Returns the item's title
     * 
     * @return A string containing the item title.
     */
    public String getTitle( )
    {
        return this.title;
    }
    
    /**
     * Returns the type of the item.
     * <p>
     * The value returned is one of the static constants.
     * @return The item's type.
     */
    public int getType( )
    {
        return this.type;
    }
    
    /**
     * Reduces the number of copies available by one.
     * <p>
     * This method would be called as an Item is purchased.
     * <p>
     * If there are no copies available, a message is printed.
     * @return True if there is a copy available, and false otherwise.
     */
    public boolean decrementCopies( )
    {
        boolean success;
        
        // Check whether there are any copies remaining.
        if ( this.copiesAvailable > 0 )
        {
            // One copy has just been purchased.
            this.copiesAvailable = this.copiesAvailable - 1;
            success = true;
        }
        else
        {
            System.out.println( "Error: No copy of '" + title + "' is available." );
            success = false;
        }
        return success;
    }
    
    /**
     * Increases the number of copies available by one.
     */
    public void incrementCopies( )
    {
        copiesAvailable = copiesAvailable + 1;
    }
    
    /**
     * Converts a type number to a string.
     * <p>
     * This method converts a type number -- one of the static constants -- to a
     * more user-friendly value that explains the meaning of the type.
     * 
     * @param type
     *            The static constant for which the type is needed
     * @return A string explaining the type constant.
     */
    public static String typeToString( int type )
    {
        String result;
        
        if ( type == Item.BOOK )
        {
            result = "Book";
        }
        else
        {
            if ( type == Item.DVD )
            {
                result = "DVD";
            }
            else
            {
                // For three types, this works. However, a better
                // plan would be to check the third type and if the
                // type somehow got set to 3 or higher, null would
                // be returned.
                
                // An even better plan would be to put these strings
                // into an array instead of hard-coding them.
                
                result = "Video game";
            }
        }
        
        return result;
    }
    
}