// Arup Guha 1/18/07

// Class created to illustrate how to use a class.
// This class allows the user to create a GiftCard object.
// A gift card object can be used to buy something, money
// can be added to it, and it can be transferred to a new
// owner.

import java.text.*;

public class GiftCard implements Comparable<GiftCard> {
	
	private String firstName;
	private String lastName;
	private double amount;

	// Constructor for an "empty" gift card.	
	public GiftCard(String first, String last) {
		firstName = first;
		lastName = last;
		amount = 0;
	}
	
	// This constructor initializes the card to amt dollars.
	public GiftCard(String first, String last, double amt) {
		firstName = first;
		lastName = last;
		amount = amt;
	}
	
	// This method allows the user to spend amt dollars from the current
	// object, IF the user has the requisite funds. In this case, true
	// is returned. If the user doesn't have enough money, the transaction
	// is cancelled and false is returned.
	public boolean spend(double amt) {
		if (amt > amount)
			return false;
		amount -= amt;
		return true;
	}
	
	// Transfers the gift card to the person with the first and last names
	// specified.
	public void transfer(String first, String last) {
		firstName = first;
		lastName = last;
	}
	
	// Returns a String representation of the current object.
	public String toString() {
		DecimalFormat fmt = new DecimalFormat("0.##");
		return firstName+" "+lastName+" $"+fmt.format(amount);
	}
	
	// Returns a negative integer if the current object is worth less than g,
	// 0 if they are of equal value, and a positive integer if it is worth more
	// than g.
	public int compareTo(GiftCard g) {
		if (amount < g.amount)
			return -1;
		else if (amount > g.amount)
			return 1;
		return 0;
	}
	
	
	// Returns true iff g is a GiftCard and is owned by the same person as the
	// current object and has the same amount as the current object.
	public boolean equals(Object g) {
		if (g instanceof GiftCard) {
			GiftCard temp = (GiftCard)g;
			return (firstName.equals(temp.firstName) &&
			        lastName.equals(temp.lastName) &&
			        amount == temp.amount);	
		}
		return false;
	}
	
	// Adds amt to the gift card balance IF amt is positive. Otherwise, no
	// action is taken.
	public void addAmount(double amt) {
		if (amt > 0)
			amount += amt;
	}
	
}