type.lib
Class RewardCard

java.lang.Object
  extended by type.lib.CreditCard
      extended by type.lib.RewardCard
All Implemented Interfaces:
java.io.Serializable

public class RewardCard
extends CreditCard

This class encapsulates a special kind of credit card that is similar to CreditCard except it offers reward points. In addition to the dollar balance of an ordinary credit card, this card has a point balance such that whenever a purchase is charged to the card, a certain number of reward points is added to the point balance. Similarly if the holder returned a purchase for refund, the point balance would be reduced by the same rate. The accumulated points can be redeemed for free merchandise.

Version:
7.0 - Summer 2010
Author:
H. Roumani, roumani@cse.yorku.ca
See Also:
Serialized Form

Field Summary
static double DEFAULT_LIMIT
          The default credit limit used by the two-argument constructor.
static int REWARD_RATE
          The rate used to compute the number of reward points.
 
Fields inherited from class type.lib.CreditCard
MIN_NAME_LENGTH, MOD, SEQUENCE_NUMBER_LENGTH
 
Constructor Summary
RewardCard(int no, java.lang.String aName)
          Construct a reward card having the passed number and holder name, and set its initial dollar and point balances to zero.
RewardCard(int no, java.lang.String aName, double aLimit)
          Construct a reward credit card having the passed number, holder name and credit limit and set its initial dollar and point balances to zero.
 
Method Summary
 boolean charge(double amount)
          Attempt to charge this card.
 void credit(double amount)
          Credit the credit card (decrease its dollar balance) by the passed amount.
 int getPointBalance()
          Return the number of reward points accumulated on this reward card.
 boolean isSimilar(RewardCard other)
          Test the similarity of two reward cards.
 void redeem(int point)
          Redeem the passed number of points and reduce the point balance accordingly.
 java.lang.String toString()
          Return a string representation of this card.
 
Methods inherited from class type.lib.CreditCard
equals, getBalance, getExpiryDate, getIssueDate, getLimit, getName, getNumber, hashCode, isSimilar, pay, setExpiryDate, setLimit
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_LIMIT

public static final double DEFAULT_LIMIT
The default credit limit used by the two-argument constructor.

See Also:
Constant Field Values

REWARD_RATE

public static final int REWARD_RATE
The rate used to compute the number of reward points. When a purchase is charged, one reward point is given for every REWARD_RATE dollars spent. And when a return is made, one reward point is taken back for every REWARD_RATE dollars returned.

See Also:
Constant Field Values
Constructor Detail

RewardCard

public RewardCard(int no,
                  java.lang.String aName,
                  double aLimit)
Construct a reward credit card having the passed number, holder name and credit limit and set its initial dollar and point balances to zero. See the corresponding superclass constructor for precondition details.


RewardCard

public RewardCard(int no,
                  java.lang.String aName)
Construct a reward card having the passed number and holder name, and set its initial dollar and point balances to zero. Invoking this constructor has the same effect as invoking the three-parameter constructor but using CreditCard.DEFAULT_LIMIT for the credit limit.

Method Detail

getPointBalance

public int getPointBalance()
Return the number of reward points accumulated on this reward card.

Returns:
the point balance of this reward card.

charge

public boolean charge(double amount)
Attempt to charge this card. Same behavior as the overridden method except the point balance is increased (if the charge succeeded) at the rate of 1 point per REWARD_RATE.

Overrides:
charge in class CreditCard
Parameters:
amount - the charge amount
Returns:
true if successful and false if such a charge exceeds the available credit.

credit

public void credit(double amount)
Credit the credit card (decrease its dollar balance) by the passed amount. Same behavior as the overridden method except the point balance is decreased (possibly making it negative) at the rate of 1 point per REWARD_RATE.

Overrides:
credit in class CreditCard
Parameters:
amount - the amount to be credited.

redeem

public void redeem(int point)
Redeem the passed number of points and reduce the point balance accordingly. This method is invoked when the card holder exchanges reward points for free merchandise.

Parameters:
point - the number of reward points to redeem.
Throws:
java.lang.RuntimeException - if point is negative or more than the point balance.

toString

public java.lang.String toString()
Return a string representation of this card.

Overrides:
toString in class CreditCard
Returns:
the same string as the overridden method except the prefix "CARD" is replaced by "RWRD", and the reward point balance is shown; e.g.
RWRD [NO=001005-3, BALANCE=7634.12, POINTS=500]

isSimilar

public boolean isSimilar(RewardCard other)
Test the similarity of two reward cards. A reward card object is considered similar to this one if it has the same dollar balance (to the nearest cent) and the same point balance as this one, regardless of card number. Even if the two cards have different numbers, this method considers them similar as long as the difference between their dollar balances is less than 0.01 and the difference between their point balances is zero.

Parameters:
other - a reference to the credit card object to test equality with.
Returns:
true if other is not null and it points to an object that is similar (as defined above) to is similar (as defined above) to this object, and false otherwise.


Java by Abstraction: A Client-View Approach