Inf1 OP : Lab Sheet Week 6 Q3 - Make your Own CreditCard
Overview

In this question, you are going to build a class representing a credit card. The exercise will give you more practice in manipulating strings. It also requires you to use the Java Calendar API in order to get the current year and month, which in turn will tell you whether a card has expired or not.

In a more realistic implementation, we would do a lot more checking of the validity of things like dates and credit card numbers! However, we won’t bother with such details right now.

Credit Cards

The CreditCard data type will contain the following data:

  • an expiryMonth and an expiryYear, both of type int; these will determine the card’s expiry date.
  • a firstName and a lastName, both of type String; these will determine the name of the card’s account holder.
  • and finally, a ccNumber, the String that holds the card’s number.

You should implement all these as private instance variables.

Here is the API for the class:

public CreditCard(int expiryMonth, int expiryYear, String firstName, String lastName, String ccNumber)

The class constructor; this will set the values for the class’s instance variables. Here’s an example of initializing a new instance of this class:

CreditCard cc1 = new CreditCard(10, 2014, "Bob", "Jones", "1234567890123456");

Notice that we are passing the expiryYear argument as a four-digit number, and that ccNumber is a 16-character string.

public String formatExpiryDate()
Return the expiry date (month and year) in the format 'MM/YY'. For example, calling formatExpiryDate() on cc1 should return the string "10/14". The month does not need to be zero-padded.
public String formatFullName()
Return the full name of the account holder in the format 'firstName lastName'. For example, calling formatFullName() on cc1 should return the string "Bob Jones".
public String formatCCNumber()
Return the credit card number as a four-block string, where blocks are separated by a single whitespace. For example, calling formatCCNumber() on cc1 should return the string "1234 5678 9012 3456".
public boolean isValid()
Returns true if the expiry date of the credit card is later than the current value provided by the Calendar utility. For example, calling isValid() on cc1 would return true if evaluated at date September 2014 or any prior date.
public String toString()

Returns a multi-line String which contains information such as the credit card number, the account holder, the expiration date and whether the card is valid or not. Here’s an example of the expected output:

Number: 1234 5678 9012 3456
Expiration date: 10/14
Account holder: Bob Jones
Is valid: true

In order to determine the current date, you will need to include the following code in your class definition:

import java.util.Calendar;

Calendar now = Calendar.getInstance();

In order to get the current year, call now.get(Calendar.YEAR); and similarly for the current month.

Warning

Calendar uses zero-based counting for months! So, for example, January is encoded as month 0.

When you write the code for isValid(), do not use the Calendar.after() method; instead, write your own boolean test.

Write a CreditCard class which meets the API above.

In addition, we recommend that you write a client program CreditCardTester. This should contain a main() method which creates some CreditCard objects and calls the methods of these objects.

An automated test has been created for this exercise: CreditCardTest.java.