type.lib
Class GlobalCredit

java.lang.Object
  extended by type.lib.GlobalCredit
All Implemented Interfaces:
java.io.Serializable, java.lang.Iterable<CreditCard>

public class GlobalCredit
extends java.lang.Object
implements java.io.Serializable, java.lang.Iterable<CreditCard>

This class encapsulates the credit card operations of a Global Credit Centre (GCC), a regional card processing centre of the Global Credit bank. It maintains a collection of credit cards and provides services for traversal and content addressing. You can access the contents of the collection using an iterator.

The collection encapsulated by this class is an example of dynamic allocation; i.e. it can grow and shrink as needed.

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

Constructor Summary
GlobalCredit()
          Construct a GCC having the name "NoName".
GlobalCredit(java.lang.String name)
          Construct a GCC having the passed name.
 
Method Summary
 boolean add(CreditCard card)
          Attempt to add the passed credit card to this GCC.
 CreditCard get(java.lang.String number)
          Find the card whose number is passed.
 CreditCard getFirst()
          Deprecated. As of version 4.0, replaced with iterator() The base invocation of the forward iterator.
 CreditCard getNext()
          Deprecated. As of version 4.0, replaced with iterator() Follow up invocation of the forward iterator.
static GlobalCredit getRandom()
          Create a randomly chosen GCC.
static GlobalCredit getRandom(boolean same)
          Create a randomly chosen GCC.
 java.util.Iterator<CreditCard> iterator()
          Determine an Iterator over the credit cards in this centre.
 CreditCard remove(java.lang.String number)
          Remove the card whose number is passed from this GCC.
 int size()
          Determine the number of credit cards registered with this GCC.
 java.lang.String toString()
          Return a string representation of this GCC.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

GlobalCredit

public GlobalCredit(java.lang.String name)
Construct a GCC having the passed name.

Parameters:
name - the name (typically city name) of this GCC.

GlobalCredit

public GlobalCredit()
Construct a GCC having the name "NoName".

Method Detail

add

public boolean add(CreditCard card)
Attempt to add the passed credit card to this GCC. If a card of the same number is already present, no addition is done.

Parameters:
card - a reference to the card to be added.
Returns:
true if the card was added and false if not. The card is not added if a card with the same number has already been added.
Throws:
java.lang.RuntimeException - if the passed parameter is null.

remove

public CreditCard remove(java.lang.String number)
Remove the card whose number is passed from this GCC.

Parameters:
number - the number of the card to remove.
Returns:
the removed credit card or null if there is no card with such a number in the GCC.

get

public CreditCard get(java.lang.String number)
Find the card whose number is passed.

Parameters:
number - the number of the card to find.
Returns:
a reference to the card whose number is passed. If the card is not found in the collection, or if the passed number is null, then null is returned.

size

public int size()
Determine the number of credit cards registered with this GCC.

Returns:
the number of credit cards that were added to this GCC.

toString

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

Overrides:
toString in class java.lang.Object
Returns:
the string "Global Credit Company [title]: CARDS=xx" , where title is the GCC's name and xx is the actual number of cards in the collection.

iterator

public java.util.Iterator<CreditCard> iterator()
Determine an Iterator over the credit cards in this centre.

Specified by:
iterator in interface java.lang.Iterable<CreditCard>
Returns:
an Iterator over the credit cards.

getFirst

@Deprecated
public CreditCard getFirst()
Deprecated. As of version 4.0, replaced with iterator() The base invocation of the forward iterator.

Returns:
the first credit card issued. If none were issued, null is returned.

getNext

@Deprecated
public CreditCard getNext()
Deprecated. As of version 4.0, replaced with iterator() Follow up invocation of the forward iterator.

Returns:
the next issued credit card. If no more cards are present, null is returned. It is assumed that getFirst is first invoked, and if it had a non-null return, then this method is invoked repeatedly until null is returned.

getRandom

public static GlobalCredit getRandom()
Create a randomly chosen GCC. This is a convenience method whose invocation is equivalent to invoking getRandom(false).


getRandom

public static GlobalCredit getRandom(boolean same)
Create a randomly chosen GCC. The GCC has no-name and contains anywhere between 2 and 20 credit cards (the count is uniformly distributed between 2 and 20). One of the cards has the number "123456-6"; otherwise, the cards have randomly chosen-numbers, random balances, random expiry dates, and "Random" as the cardholder name. On average, 25% of the cards are reward cards. You should normally invoke this method with a false parameter. The parameter was added to allow a harness to pass the same test cases to an app being tested and to an oracle.

This method was added for pedagogical reasons.

Parameters:
same - a flag that determines if a new GCC is to be generated (when false) or if a reference to the last-generated GCC should be returned (true). You should normally set it to false to get a different GCC per invocation.
Returns:
a GCC that is either randomly generated (if same is false) or is the same as the one last generated by this method. If this method was never invoked before, a random GCC is returned regardless of the passed parameter.


Java by Abstraction: A Client-View Approach