Starting from:

$20

Project 2 Credit Card Solution

Learning Objectives
After completing this project, you should be able to:
 write compile and run a simple Java class
 write expressions using variables
 write methods to meet specific requirements
 explain the differences between local variables, instance variables and method parameters
 write conditional statements with Boolean expressions

Project Summary

Create a class, called CreditCard, which simulates the functionality of a credit card such as Visa or Mastercard. You can do simple things like make purchases, take cash advances and make payments. Important information about your card:
 Annual Percentage Rate (APR) for previous month balance is 15.9%
 Cash advance APR is 25.9% effective on the day of advance and until the end of the month (assume all months have 30 days but the year has 365 days)
 Each balance transfer has a fee of $5 or 3% of the transfer, whichever is greater
 Required minimum payment is 1.5% of balance or $25, whichever is greater
 Due date is the 20th of the month
 Late payment fee is $37 – if at least a minimum payment is not made within grace period (20 days)
 Minimum Interest – if you are charged interest, the minimum amount will be $0.50
 There is no annual fee

User Interaction

You do not use a Scanner in this project for user interaction. Instead, the cardholder interacts with the Credit Card by invoking methods within BlueJ.
Instance Variables (5 pts)
A class should contain several instance variables (section 3.1). Some instance members are not expected to change after given an initial value. It is good practice to define these as final and use
ALL CAPS for the names (section 7.1). Provide appropriate names and data types for each of the private instance variables:
 a String for the card holder name
 doubles for the previous balance, purchases, balance transfers and new balance
 doubles for the fees, interest and cash advances
 an integer for the number of purchases in the current month
 a final double for the balance APR (15.9%)
 a final int for the due date (20)
 a final double for the late payment fee (37.0)
 a final double for the minimum interest amount (0.5)
 a final double for the cash advance APR (25.9%),
private final double ADVANCE_APR = 25.9/100;
 you might identify the need for other final members as well

Constructors (5 pts)
A constructor is a special method with the same name as the class and generally initializes the fields to appropriate starting values. Refer to sections 3.7 and 3.8.
 public CreditCard (String name) – assigns the cardholder name and other
instance variables are set to zero.
 public CreditCard (String name, double amt) - alternative constructor
assigns the cardholder name and previous monthly balance to amt. Other instance variables are set to zero.

Accessor Methods (5 pts)
An accessor method does not modify class fields. The names for these methods, which simply
return the current value of a field, often begin with the prefix ‘get’. Refer to section 3.6.
 public double getPreviousBalance( ) - return the previous balance.
 public double getFees( ) – return the fees
 public double getInterest( ) – return the interest
 public double getPurchases( ) – return the purchases
 public double getBalanceTransfers( ) – return the balance transfers
 public double getCashAdvances( ) – return the cash advances
 public int getNumPurchases( ) – return the number of purchases
 public String getName( ) – return the card holder name

Mutator Methods (20 pts)

A mutator method performs tasks that may modify class fields. Refer to section 3.6.
 public void applyCredit (double amt) – Reduce the purchases by amt.
Display an appropriate message (see sample output below).
 public void balanceTransfer (double amt) – Customer adds to her
transfer balance total for this month. This amount must be paid back and gets moved tothe total amount due at the end of the month. Update the transfer balance and fees. The fee is either $5 or 3% of amt (whichever is greater). Display an appropriate message.
 public void purchase (double amt) – Update the purchase balance and
increment the number of purchases. Display an appropriate message.
 public void cashAdvance (double amt, int day) – Update the cash
advance balance and interest. Parameter day represents the day the advance was made. Interest is calculated by applying the cash advance APR for the number of days until the end of the month (assume 30 days for a month but 365 days for the year). For example, if day=10 then there are twenty-one remaining days in the month. Minimum interest is $0.50. Display an appropriate message.
Private Helper Methods (15 pts)
Designated as private, a helper method is used by other methods within the class. Good practice is to make methods private unless they need to be public. Refer to section 3.6.
 private double calcMinimumPayment ( ) – calculate and return the
minimum payment which is either $25 or 1.5% of the new balance calculated at the end of the month (whichever is greater). Note, this private method is invoked from within printStatement().
 private void resetAmounts ( ) – Update previous balance to the new
balance. Reset all other instance variables to zero. Note, this private method is invoked from within closeBillingPeriod().
 private void printStatement ( ) - display the monthly statement. See
sample output for how the statement should appear. Invoke calcMinimumPayment() to avoid duplicate code. Note, this private method is invoked from within closeBillingPeriod().

Additional Mutator Methods (10 pts)

These methods invoke one or more of the private helper methods to keep it short and concise.
 public void makePayment (double amt, int day) – Reduce the
previous balance by amt. Parameter day represents the day of the month payment was submitted. Apply a penalty fee of $37 if amt is not at least the required minimum payment or is paid later than the 20th of the month. Invoke calcMinimumPayment() rather than duplicating the code. Display an appropriate message.
 public void closeBillingPeriod () – Calculate interest on the previous
balance, if any, by applying the balance APR. Calculate the new balance due by adding interest, transfer, purchases, cash advances, fees and the previous balance. Display the current statement by invoking the helper method printStatement(). Reset all instance variables as necessary by invoking the helper method resetAmounts().

Preventing Customer Errors (10 pts)

The following should only be attempted after all other requirements described above have been completed. The credit card class is now functional but it is possible the cardholder will make various errors. Make improvements to the following methods that you already wrote. Your goal is to prevent common customer mistakes by ignoring parameters with negative values. No action should be taken if the amt is negative or if day is not between 1 and 30.
 balanceTransfer (double amt)
 applyCredit (double amt)
 purchaase (double amt)
 cashAdvance (double amt, int day)
 makePayment (double amt, int day)

Currency Format (10 pts)

The following should only be attempted after all other requirements have been completed.
 You will note that the monetary amounts display with extra decimal places and no dollar sign. Use the NumberFormat class (described below) to display all monetary figures
in currency format.
 Define an instance variable and use it throughout the code to format currency values private NumberFormat fmt = NumberFormat.getCurrencyInstance();

Number Format

The NumberFormat class can be used to display currency numbers as text/strings. It is not explained in the book but you can find more information in the Java API. The following example may be enough for you.
Code fragment output
NumberFormat fmt = NumberFormat.getCurrencyInstance();
double amount = 3.0111111;
System.out.println("Cost: " + fmt.format(amount)); Cost: $3.01
Coding Style (10 pts)
Good programming practice includes writing elegant source code for the human reader. Follow the GVSU Java Style Guide.

Sample Messages

The following sample messages, printed while interacting with BlueJ, are provided as examples.
Your messages should be similar.
Make purchase of 184.95
Purchase Amount: $184.95
Cash advance of (1000, 13)
Cash Advance on Day 13: $1,000.00
Cash advance with illegal day (1000, -1)
Request cancelled: provide a day 1 - 30
Balance transfer of 300
Transfer: $300.00
Make purchase of -5
Request cancelled: provide a positive amount
Apply credit of 45.5
Credit: $45.50
Close Billing Period
Previous Balance: $0.00
1 Purchase(s): $139.45
Advances: $1,000.00
Transfers: $300.00
Fees: $9.00
Interest: $12.77
New Balance: $1,461.22
Minimum Payment: $25.00
Make payment of (500, 10)
Payment on Day 10: $500.00

Software Testing (10 pts)

Software developers must carefully test their code. BlueJ allows you to instantiate objects and then invoke individual methods to manipulate the fields (instance variables) within the object. You can carefully check each method and compare actual results with expected results. However, this gets tedious and cannot be automated.
Testing Your Class using a main() method

Another approach is to write a main method, called a test harness, that instantiates an object and then calls methods in a carefully designed sequence. Refer to section 4.12.
For this project, write a main method in a new class called CardTest that instantiates a credit card and invokes each of the methods with a variety of parameter values. Provide multiple if statements to test each method along with error messages as needed. It takes careful consideration to test every possibility.
This is a brief and incomplete example. Your test harness should be much longer with 10-15
if statements.
public static void main(String arg[]){
CreditCard brenda = new CreditCard("Brenda Stern");
int errors = 0;
// multiple purchases
brenda.purchase(100);
brenda.purchase(2.35);
if(brenda.getPurchases() != 102.35){
System.out.println(" ERROR! with purchase");
errors++;
}
// negative purchase
brenda.purchase(-10);
if(brenda.getPurchases() != 102.35){
System.out.println(" ERROR! with negative purchase");
errors++;
}

System.out.println(errors + " errors detected");
}

More products