CMSC 206 (Data Structures)
Assignment#2
Due: September 24, 2012
11:59:59pm
The goal of this assignment is
to develop your ability to do object-oriented programming,
focusing specifically on developing and using a basic
class.
Introduction
Over the next two assignments,
you will write a program that allows a user to repeatedly
create and manage credit card accounts. In this
assignment, we will focus on developing a basic class to
represent the credit card.
When a new credit card account
is opened, the credit company sets a credit limit for the
user that specifies the maximum balance allowed on the
account. Unlike a debit card, the balance starts at
$0. The user can make purchases and charge them to the
credit card, increasing the balance. For example, if I
charge two purchases of $100 and $50, my credit card balance
would be $150. Say that my credit limit is $500.
If I then tried to charge another $400 purchase to the card,
the card would be declined since that would put the balance
over my credit limit. At the end of each month, the
user must pay the entire balance on the card, or else the
credit company charges the user interest on the balance.
Program
Requirements
- Implement
a
class CreditAccount that includes
the following attributes and methods at a minimum (you can
add others of your own design):
- Private
Variables:
accountNumber, accountBalance, creditLimit,
interestRate.
- The interest rate should be set to 1.25%, and
the credit limit to $4,000. While we won't
change them in this assignment, they must be
variables.
- A constructor
that takes in one integer for the accountNumber, and
initializes the accountNumber to the given value.
- Accessors (i.e., getters) for all private
variables needed outside the class with appropriate
public (or protected) access levels.
- A
toString() function that outputs a textual description
of the account that includes the account number,
balance, credit limit and interest rate.
- The
following
two methods:
- void charge(double amount) which takes in an
amount to charge against the account and throws an
exception if this amount would push the balance over
the credit limit. This exception should be
properly handled in the calling code. The method
should also output a simple status message to
System.out, such as "Charging $40 to Account #101".
- void makePayment(double amount) which reduces
the balance by the specified amount. This method
is also responsible for assessing interest on the
balance if it is not paid in full. Note that
this interest should be assessed against the balance
prior to the amount being paid off. For example,
if the balance is $200 and the user pays $50, 1.25%
interest would be assessed against $200. The method should also output a simple
status message to System.out, such as "Received
payment of $40 to Account #101".
- Be
sure
to do error checking on the inputs. Negative
payments and charges are not allowed, although the user
can carry a minimum balance.
- Since
we're
working with money, all decimals need to be rounded off to
two decimal places. You can achieve this by
multiplying a decimal by 100, rounding using Math.round(),
and then dividing the result by 100.0. I suggest
making a function to do this, since you will need to do it
a lot.
- Write
a main() method within the CreditAccount class to
demonstrate its functionality. The main() method
should perform the following tasks (in-order):
- Print
out your name to System.out along with the string " CS
206 Assignment 2".
- Instantiate
a new instance of the CreditAccount class
- Print
out a description of the account, including account
number, balance, credit limit and interest rate, by
calling the toString() method on it.
- Charge
$100 to the account (automatically printing out the
corresponding status message of what was done to the
account)
- Print
out the current balance (which should be $100)
- Make
a payment of $100 to the account (automatically printing out the corresponding
status message of what was done to the account)
- Print
out the current balance (which should be $0)
- Charge $100 to the account
- Print out the current balance (which should be $100)
- Make a payment of $50 to the account
- Print out the current balance (which should be $51.25,
including the unpaid portion and the interest)
- Make a payment of $51.25 to the account
- Print out the current balance (which should be
$0)
- Charge
$-100 to the account, which should display an error
message via an Exception
- Make
a payment of $-100, which should display an error
message via an Exception
- Print
out the current balance (which should still be $0)
Implementation
Hints
- Start by writing the CreditAccount class with only the
accountBalance field, a toString() method that outputs the
balance, and a main() method that instantiates a new
instance of the CreditAccount class and outputs the result
of calling the toString() method. Compile and test to
make sure that this simple version works.
- Gradually build up the functionality of the CreditAccount
class, adding each field and method in turn, testing
thoroughly that your class works at each step. Do this
very gradually, breaking the development of the
CreditAccount class into about 10 steps, just as I did for
you in assignment 1.
- It
is often a handy trick to write a main() method for
every class that tests the class thoroughly, as we've
done in this assignment. This is very useful for
diagnostic purposes, and will be essential when we start
developing larger programs.
Submitting
the Assignment
Be certain that your
CreditAccount.java file contains proper header comments that
include your name.
Place your CreditAccount.java file and compiled
CreditAccount.class file into a directory entitled
LastnameFirstname-Assignment2
.
Compress this folder as a zip or tar file, and submit it via
Dropbox, as detailed in the
assignment submission
instructions.