public class CreditCard
extends java.lang.Object
implements java.io.Serializable
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.
Modifier and Type | Field and Description |
---|---|
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 and Description |
---|
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.
|
Modifier and Type | Method and Description |
---|---|
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. |
public static final double DEFAULT_LIMIT
public static final int MIN_NAME_LENGTH
public static final int SEQUENCE_NUMBER_LENGTH
public static final int MOD
public CreditCard(int no, java.lang.String aName, double aLimit, java.util.Date issue)
no
- the credit card number, must be > 0 and ≤ 999999aName
- the name of the holder of this card, must have at least
MIN_NAME_LENGTH
charactersaLimit
- the credit limit, must be positiveissue
- the issue date after which the card becomes validjava.lang.RuntimeException
- if the number is not in the above range, the name has fewer
characters than indicated, or credit limit is not positive.public CreditCard(int no, java.lang.String aName, double aLimit)
no
- the credit card number, must be > 0 and ≤ 999999aName
- the name of the holder of this card, must have at least
MIN_NAME_LENGTH
charactersaLimit
- the credit limit, must be positivepublic CreditCard(int no, java.lang.String aName)
DEFAULT_LIMIT
for the
credit limit.no
- the credit card number, must be > 0 and ≤ 999999aName
- the name of the holder of this card, must have at least
MIN_NAME_LENGTH
characterspublic double getBalance()
this
credit card.public java.lang.String getNumber()
this
credit card, which is a string of
8 characters (see the class description for details).public java.lang.String getName()
this
credit card.public double getLimit()
this
credit card.public java.util.Date getIssueDate()
this
credit card.public java.util.Date getExpiryDate()
this
credit card.public boolean setLimit(double newLimit)
newLimit
- the new credit limit.true
if the limit was successfully changed and
return false
otherwise.public boolean setExpiryDate(java.util.Date expiry)
expiry
- the new expiry date of the card.true
if the expiry date was successfully changed and
return false
otherwise.public void credit(double amount)
amount
- the amount to be credited.public boolean charge(double amount)
amount
- the amount (of the purchase) to be charged.true
if successful and false
if such a
charge exceeds the available credit.public void pay(double amount)
amount
- the amount (of payment).public boolean equals(java.lang.Object other)
this
one if it is indeed a credit card object with the same
card number as this
one.equals
in class java.lang.Object
other
- a reference to the object to test equality with.true
if other
is not null
and it points to an object that is equal (as defined above) to
this
object, and false
otherwise.public int hashCode()
this
credit card.hashCode
in class java.lang.Object
public boolean isSimilar(CreditCard other)
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.other
- a reference to the credit card object to test equality with.true
if other
is not null
and it points to an object that is similar (as defined above) to
this
object, and false
otherwise.public java.lang.String toString()
this
card.toString
in class java.lang.Object
CARD [NO=001005-3, BALANCE=7634.12]