type.lib
Class CreditCard

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

public class CreditCard
extends java.lang.Object
implements java.io.Serializable

This class encapsulates a credit card and maintains information about it. Each card is identified by the attributes: card number, holder's name, issue and expiry dates, credit limit, and balance owing. The key methods in the class are charge (used when the holder makes a purchase), pay (used when the holder makes a payment), and credit (when the holder returns a purchase for refund). It also supports standard methods (equals and toString) and a few accessors and mutators.

Note that this class aggregates the issue and expiry dates via a composition.

The card number is 8-character in length and consists of a 6-digit string (set by the user upon construction), a dash, and a MOD-9 check digit. The check digit ensures that the sum of all digits, including it, is a multiple of 9; it is added to detect possible transmission errors. The dash and the check digit are added by the class.

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 MIN_NAME_LENGTH
          The minimum allowed length for the name of the cardholder.
static int MOD
          The sum of the digits in the credit card number must be divisible by this constant.
static int SEQUENCE_NUMBER_LENGTH
          The number of digits in the serial part of the card number.
 
Constructor Summary
CreditCard(int no, java.lang.String aName)
          Construct a credit card having the passed number and holder name.
CreditCard(int no, java.lang.String aName, double aLimit)
          Construct a credit card having the passed number, holder name, and limit.
CreditCard(int no, java.lang.String aName, double aLimit, java.util.Date issue)
          Construct a credit card having the passed number, name, and credit limit; and set its initial balance to zero.
 
Method Summary
 boolean charge(double amount)
          Attempt to charge the passed amount on the credit card (increase its balance by that amount).
 void credit(double amount)
          Credit the credit card (decrease its balance) by the passed amount.
 boolean equals(java.lang.Object other)
          Test the equality of credit cards.
 double getBalance()
          Credit card balance accessor.
 java.util.Date getExpiryDate()
          Credit card expiry date accessor.
 java.util.Date getIssueDate()
          Credit card issue date accessor.
 double getLimit()
          Credit card limit accessor.
 java.lang.String getName()
          Credit card holder accessor.
 java.lang.String getNumber()
          Credit card number accessor.
 int hashCode()
          Compute a hash code for this credit card.
 boolean isSimilar(CreditCard other)
          Test the similarity of two credit cards.
 void pay(double amount)
          Make a payment to reduce the balance of this card.
 boolean setExpiryDate(java.util.Date expiry)
          Credit card expiry date mutator.
 boolean setLimit(double newLimit)
          Credit card limit mutator.
 java.lang.String toString()
          Return a string representation of this card.
 
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

MIN_NAME_LENGTH

public static final int MIN_NAME_LENGTH
The minimum allowed length for the name of the cardholder.

See Also:
Constant Field Values

SEQUENCE_NUMBER_LENGTH

public static final int SEQUENCE_NUMBER_LENGTH
The number of digits in the serial part of the card number.

See Also:
Constant Field Values

MOD

public static final int MOD
The sum of the digits in the credit card number must be divisible by this constant. A check digit (possibly zero) is added to the serial card number to meet this requirement.

See Also:
Constant Field Values
Constructor Detail

CreditCard

public CreditCard(int no,
                  java.lang.String aName,
                  double aLimit,
                  java.util.Date issue)
Construct a credit card having the passed number, name, and credit limit; and set its initial balance to zero. The expiry date of the card is set to two years from the given issue date.

Parameters:
no - the credit card number, must be > 0 and <= 999999
aName - the name of the holder of this card, must have at least MIN_NAME_LENGTH characters
aLimit - the credit limit, must be positive
issue - the issue date after which the card becomes valid
Throws:
java.lang.RuntimeException - if the number is not in the above range, the name has fewer characters than indicated, or credit limit is not positive.

CreditCard

public CreditCard(int no,
                  java.lang.String aName,
                  double aLimit)
Construct a credit card having the passed number, holder name, and limit. Invoking this constructor has the same effect as invoking the four-parameter constructor and using today's date for the issue date.


CreditCard

public CreditCard(int no,
                  java.lang.String aName)
Construct a credit card having the passed number and holder name. Invoking this constructor has the same effect as invoking the three-parameter constructor and using DEFAULT_LIMIT for the credit limit.

Method Detail

getBalance

public double getBalance()
Credit card balance accessor.

Returns:
the balance owing on this credit card.

getNumber

public java.lang.String getNumber()
Credit card number accessor.

Returns:
the number of this credit card, which is a string of 8 characters (see the class description for details).

getName

public java.lang.String getName()
Credit card holder accessor.

Returns:
the name of the holder of this credit card.

getLimit

public double getLimit()
Credit card limit accessor.

Returns:
the credit limit of this credit card.

getIssueDate

public java.util.Date getIssueDate()
Credit card issue date accessor.

Returns:
a copy of the issue date of this credit card.

getExpiryDate

public java.util.Date getExpiryDate()
Credit card expiry date accessor.

Returns:
a copy of the expiry date of this credit card.

setLimit

public boolean setLimit(double newLimit)
Credit card limit mutator. Attempt to change the credit limit to the passed value. The change is successful only if the passed value is non-negative and not less than the current balance.

Parameters:
newLimit - the new credit limit.
Returns:
true if the limit was successfully changed and return false otherwise.

setExpiryDate

public boolean setExpiryDate(java.util.Date expiry)
Credit card expiry date mutator. Attempt to change the expiry date to the passed value. The change is successful only if the passed reference is not null and it occurs at any point of time after the issue date.

Parameters:
expiry - the new expiry date of the card.
Returns:
true if the expiry date was successfully changed and return false otherwise.

credit

public void credit(double amount)
Credit the credit card (decrease its balance) by the passed amount. This method is used when goods purchased using this card are subsequently returned for refund. An assertion failure occurs if the passed amount is < 0.

Parameters:
amount - the amount to be credited.

charge

public boolean charge(double amount)
Attempt to charge the passed amount on the credit card (increase its balance by that amount). If this will lead to a balance greater than the credit limit, the request is denied and false is returned. The request is also denied if the card has expired. Otherwise the balance is increased and true is returned. An assertion failure occurs if the passed amount is < 0.

Parameters:
amount - the amount (of the purchase) to be charged.
Returns:
true if successful and false if such a charge exceeds the available credit.

pay

public void pay(double amount)
Make a payment to reduce the balance of this card. If the passed amount is negative, an assertion failure occurs. Otherwise, the balance is reduced by the passed amount (even if it becomes negative). Over-paying (making the balance negative) is a legitimate transaction used in certain circumstances to effectively increase the credit limit

Parameters:
amount - the amount (of payment).

equals

public boolean equals(java.lang.Object other)
Test the equality of credit cards. An object is considered equal to this one if it is indeed a credit card object with the same card number as this one.

Overrides:
equals in class java.lang.Object
Parameters:
other - a reference to the object to test equality with.
Returns:
true if other is not null and it points to an object that is equal (as defined above) to this object, and false otherwise.

hashCode

public int hashCode()
Compute a hash code for this credit card.

Overrides:
hashCode in class java.lang.Object
Returns:
a hash code value for this object.

isSimilar

public boolean isSimilar(CreditCard other)
Test the similarity of two credit cards. A credit card object is considered similar to this one if it has the same balance, to the nearest cent, 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 balances is less than 0.01.

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 this object, and false otherwise.

toString

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

Overrides:
toString in class java.lang.Object
Returns:
a string like this:
CARD [NO=001005-3, BALANCE=7634.12]
The string starts with CARD and between two brackets, it contains the credit card number (001005-3 in the above example), and balance (rounded to two decimals), formatted as shown.


Java by Abstraction: A Client-View Approach