/**
 * Class Client.
 * 
 * <h4>ITI 1120 Fall 2011, Assignment 5, Question 1</h4>
 * <p>
 * This class represents a client of amazon.tv
 * <p>
 * The client information that is stored is the name, email, password, and
 * credit.
 * <p>
 * Operations that are provided are as follows:
 * <ul>
 * <li> ask for the client's name </li>
 * <li> ask for, or change, the client's email address </li>
 * <li> ask for the current credit </li>
 * <li> verify the client's password </li>
 * <li> change the client's password, if and only if the current password can be provided </li>
 * </ul>
 * 
 */

class Client
{
    /**
     * The name of the client.
     */
    private String name;
    
    /**
     * The client's email address.
     */
    private String email;
    
    /**
     * The client's password.
     */
    private String password;
    
    /**
     * The amount of credit.
     */
    private int credit;
    
    /**
     * Constructs a new Client and installs the name, email, and initial password.
     * 
     * @param clientName
     *            The name of the new Client.
     * @param clientEmail
     *            The new Client's email address
     * @param initialPassword
     *            The new Client's initial password
     */
    public Client( String clientName, String clientPhone, String initialPassword )
    {
        // Copy parameters to instance variables
        this.name = clientName;
        this.email = clientPhone;
        this.password = initialPassword;
        
        // A client begins with 10 dollars credit.
        this.credit = 10;
    }
    
    /**
     * Compares a stored password with an entered password for validity.
     * <p>
     * If the stored password matches the entered password, the method returns
     * true. Otherwise, the method returns false.
     * 
     * @param enteredPassword
     *            The password to be verified.
     * @return True if the entered password is the same as the stored password,
     *         and false otherwise.
     */
    public boolean checkPassword( String enteredPassword )
    {
        boolean result; 
        
        // Check the password. The compareTo() method must be
        // used for string comparison.
        
        result = enteredPassword.compareTo( this.password ) == 0;
        return result;
    }
    
    /**
     * Changes the password for an authorized user.
     * <p>
     * To change the password, the user must be able to supply the current
     * password. If the password check is satisfied, then change the current
     * password to the proposed new password.
     * 
     * @param oldPassword
     *            Current password, which must be checked.
     * @param newPassword
     *            If authorized, this is the new password.
     * @return True if the password was successfully changed and false
     *         otherwise.
     */
    public boolean changePassword( String oldPassword, String newPassword )
    {
        boolean success;
        
        success = this.checkPassword( oldPassword );
        if ( success )
        {
            this.password = newPassword;
        }
        else
        {
            ; // do nothing
        }
        return success;
    }
    
    /**
     * Return's the current amount of credit of the Client's.
     * 
     * @return The current amount of credit.
     */
    public int getCredit( )
    {
        return this.credit;
    }
    
    /**
     * Modify the Client's credit.
     * <p>
     * Used when a client buys items.
     * 
     * @param newAmount
     *            The amount to de deducted.
     */
    public boolean reduceAmountCredited( int amount )
    { boolean success = true;
      if (credit >= amount)
         credit = credit - amount;
      else 
         success =  false;
      
      return success;
    }
    
    /**
     * Return's the Client's email
     * 
     * @return The email address.
     */
    public String getEmail( )
    {
        return email;
    }
    
    /**
     * Modify the Client's email address.
     * 
     * @param newEmail
     *            The new email address.
     */
    public void setEmail( String newEmail )
    {
        email = newEmail;
    }
    
    /**
     * Returns the Client's name.
     * 
     * @return The name.
     */
    public String getName( )
    {
        return this.name;
    }
}