CS230 Data Structures, Wellesley College, Fall 2008

Lab 3: Objects and Arrays of Objects

Working with credit cards

Task 1: Define the CreditCard Class

Today you will write a class to represent a credit card. The class details are below. To get started, download the files from the download directory
/home/cs230/download/Lab03
to your local directory. Then run the CreditCardDriver executable program to get familiar with the way the program works. What is a driver?

Here are the implementation details:

  1. Static Variables
    1. baseRate - The minimum interest rate that the card can have. This should be a constant that is hard coded.
    2. lastAccountNumber - The last account number assigned to a customer. Account numbers should be assigned sequentially. So, the first customer gets assigned the number 1, the second customer the number 2, etc.
      Do you think it is a good choice for the above two fields to be static (class) variables, as opposed to instance variables?
  2. Instance Variables
    1. accountholder - The name of the person who owns the card.
    2. accountNumber - A unique 7-digit identifier number.
    3. creditScore - The account holder's credit score.
    4. rate - The annual interest rate charged to the card.
    5. balance - The current balance on the card.
    6. creditLimit - The card holder's credit limit.
  3. Methods
    1. Constructor() - Should only take the account holder's name and a credit score. The next account number available should be assigned. The balance will be 0 as nothing has been charged yet. The rate and credit limit will be determined by the credit score. Use the following table to set the rate and credit limit.
    2. Credit Score

      Rate

      Limit

      0-300

      baseRate + 10%

      $1000

      300-500

      baseRate + 7%

      $3000

      500-700

      baseRate + 4%

      $7000

      700+

      baseRate + 1%

      $15000

    3. getBalance() - Returns the current balance. This simple method is what we call a getter method: it gets the value of an instance variable.
    4. makePurchase() - Takes a purchase amount as a parameter and updates the current balance. If the amount + balance creditLimit, deny the transaction.
    5. makePayment() - Takes a payment amount as a parameter and updates the current balance. If the payment is greater than the balance, set the balance to zero and print an appropriate message. If the payment is less than 10% of the balance, apply the payment and raise the account holder's rate by 1%. If the balance is paid off entirely, raise the creditScore by 10. If the increased creditScore changes where the account holder falls in the above table, change the rate and limit as appropriate.
    6. raiseRate() - Raises the account holder's rate by a given percentage.
    7. raiseLimit() - Raises the account holder's limit by a given dollar amount.
    8. calculateBalance() - Calculates the balance on a monthly basis. Remember that the rate is yearly, so the formula for calculating the balance monthly is balance + (balance * (rate / 12)).
    9. toString() - Print the account holder's name, account number obscured with stars (ex: 1234567 would display as ****567), balance, and limit.

Testing your CreditCard class

Now add some testing code in the main() method of your program. Create a CreditCard object, and call the methods on it as you define them. You should be reasonably confident, at this point, that your methods are working as intended.

To thoroughly test your class, run the driver program, in the provided file CreditCardDriver.java. This program tracks an account holder over a three month period:

Please study this program, to make sure you understand its structure, and the programming constructs it uses. Some of them will be familiar to you, like if-statements, others relatively new, like the do-while loop. Note that CreditCardDriver.java uses the Scanner class to read input in from the keyboard. Here is a short example using the Scanner class.

String answer, phrase;
double money;
Scanner scan = new Scanner (System.in);
System.out.print("What is your first name?");
answer = scan.nextLine();
System.out.print("What is your favorite phrase?");
phrase = scan.nextLine();
System.out.print("How old are you?");
age = scan.nextInt();
System.out.print("How much money would you like me to give you?");
money = scan.nextDouble();

A note about Javadoc

You may have noticed the syntax of the comments in your lab solution code handouts. We are using a style of commenting that allows generation of HTML documents, called Javadoc. By following a couple of steps, the comments in your code are incorporated into online documentation of your program. If you are interested in learning more:

Task 2: Define the CreditCardsArray class

In this task, we will work on the definition of a new class named CreditCardsArray. An instance of the CreditCardsArray class will store a collection of CreditCard instances.

Then, as time permits: