This assignment will give you practice writing if
statements, loops, and manipulating String
s. You will
also develop JUnit TestCase classes.
Plan to spend a full hour reading this handout so that you thoroughly understand what we are asking for. Do this before you start programming! Take notes as you read. What classes do we want? What are the method names for each? After reading the handout, look at some examples of using these methods.
If you notice parts that are unclear, then please post a message to the discussion board. Any major changes/clarifications will be posted to the A2 FAQ page.
This assignment involves writing a small system to purchase items from
online stores and make payments by credit cards.
You will write two classes (ShoppingCart
and
CreditCard
), and two test classes (ShoppingCartTester
and CreditCardTester
). The
ShoppingCart
class keeps track of items you want to buy from an
online store. The CreditCard
class keeps track
of transactions (i.e. purchases, refunds, and bill payments) posted
to a credit card. Both these classes will involve processing date
information. We provide you with a simple implementation of date
class call MyDate
which you will download from this website
and use in your programs.
How do you know whether the ShoppingCart
and CreditCard
classes that you write are correct or
not? The only way you can be sure is if you test them. Every time you
write a method for your ShoppingCart
and CreditCard
classes you should also
write a couple of tests for it, and run your collection of tests
frequently to make sure that everything works correctly.
Write two JUnit TestCase classes: ShoppingCartTester
and CreditCardTester
. Make sure that your test classes adhere to
the following principles:
ShoppingCart
and CreditCard
classes, your test
suites should have at least one test case that
tests that method.
You are strongly encouraged to add "helper" methods. Whenever you find yourself repeating code (cutting and pasting, for example), you should think strongly about whether that code should be in a helper method. We will be looking for this when we mark. As an example and a hint, extracting the date, storeName, and amount from a single transaction are things that you will need several times.
We'll be looking for places where you should call existing methods (methods that are specified in the assignment description), and we'll be looking to see if you added new helper methods.
Important Note: Any helper methods that you add should be
private
. This is because they are intended for use
only within the class (not for use by other classes). It's not required
to write testers for these private helper methods.
For this assignment you must use Javadoc comments, including @param and @return tags.
As mentioned, shopping carts and credit cards will involve processing date information. To make your life easier, we have provided you with a simple implementation called MyDate. You don't need to implement or test this class, just download it from here and use it in your program. The public methods available in this class are as follows:
Constructor | Description |
---|---|
|
Constructor for the |
Method | Description |
|
Return the date as a |
|
Return true if the date is the same as or before date d2 (i.e. <= d2). Otherwise return false. |
|
Return true if the date is the same as or after date d2 (i.e. >= d2). Otherwise return false. |
Make yourself familiar with MyDate
calss before using it in
your program (e.g. create a couple of MyDate
object and call
the methods).
Class ShoppingCart
will be used to track the items you want
to buy from an online store.
ShoppingCart
Variables (You must NOT declare any instance variables
other than these)Type | Description |
---|---|
String | The name of the store. |
MyDate | The date of purchase |
|
The items currently in the shopping cart.
The
where productName is the name of the product to buy, quantity is an integer indicating how many units you want to buy, and unitPrice is a double value indicating price per unit. Each item is separated by a comma with no extra space.
There should not be a comma
at the beginning or end of the
"Wireless mouse:1:15.99,1GB SD Flash:4:39.0,LCD monitor:1:175.95"
If the shopping cart contains no products, then it is the empty string (""); |
The ShoppingCart
class will support the following
public methods. Pay close attention to the parameters and return values of
each method. The descriptions, while informal, are complete.
Constructor | Description |
---|---|
|
Constructor for the |
Method | Description |
|
Return the items in the cart (as a String). (Note: the string that is returned should be the same as that described in the instance varaible) |
|
This method has 3 parameters, which represent the name of product to
buy, the quantity, and the price per unit, in that order. It adds the
request to the cart. The item is appended to the end of the cart
in the proper format specified for the instance variable. Assume the product is not already in the cart. |
|
This method has one parameter, which represent the name of the product to be removed from the shopping cart. If the product is in the shopping cart the item is removed from the cart and the method returns true, otherwise the method returns false and leaves the cart unchanged. |
|
This method has two parameters, which represent the name of the
product to update its quantity, and the desired new quantity of the
product. This method changes the quantity of the given product to
the new quantity if the product is already in the cart and returns
|
purchase(CreditCard) |
This method has one parameter, which represent the credit card used to purchase the items in the shopping cart. This method
purchases the items in the shopping cart (if it is not empty) from
this store in this date by calling the
Note that if the shopping cart is empty no transaction is posted to
the credit card and the method returns
|
The CreditCard
class contains information about transactions
(purchases, refunds, and bill payments). The instance variables are
as follows (you may NOT add any other instance varaibles)
Type | Description |
---|---|
int |
Credit card number |
String |
The name of credit card owner |
MyDate |
The expiry date of the credit card |
String |
The transactions posted to the credit card.
The String has zero or more transactions stored in this form:
Each set of transactions is separated by a comma with NO extra
space.
There should not be a comma
at the beginning or end of this Here is an example (note that we make no assumption about the date ordering of the transactions):
Use the empty string ("") to indicate no transaction exists. |
CreditCard
MethodsConstructor | Description |
---|---|
|
Constructor for the |
Method | Description |
|
Return the transactions (as a String ). (Note: the
transactions should be the same as that described in the instance
variable above.)
|
|
This method takes three parameters, which represent the date of
transaction, name of the store, and the amount of money (a positive value)
to be charged to the credit card. The transaction is possible only if its
date is the same or before the expiry date of the credit card. If the
transaction is possible the method should append the given transaction to
the end of the transactions in the proper format and return true. The
method
returns false otherwise and leaves transactions unchanged. |
|
This method takes three parameters, which represent the date of
transaction, name of the store, and the amount of money (a positive value)
to be credited back to the credit card. The transaction is possible only
if there is at least one purchase (of any amount) from this store on
the transaction date or before it. If the transaction is possible the
method should append the given transaction to the end of the transactions
in the proper format and return true. The method returns false
otherwise and leaves transactions unchanged. |
|
This method takes two parameters, which represent the date of transaction and the amount of money (a positive value) to be credited back to the credit card. It appends the given transaction to the end of the transactions in the proper format. The method returns nothing. |
genTransactionsRange(MyDate, MyDate) |
This method takes two parameters, which represent a start date and an end date, respectively. It returns (as a String) all the transactions that took place between start and end dates (inclusive). The format and order of transactions in the returned string should be the same as they appear in the transactions instance variable. If there is no transaction, it returns the string "No transaction found!" (it is important that the string be EXACTLY this with capital N and ! at the end). |
calcBalance() |
This method returns the current balance (as a double) of the credit card. The balance is calculated by scanning all the transactions and adding up all the amounts. |
genPaymentHistory(MyDate d1) |
This method takes a start date and returns (as a String) all the bill
payment transactions that took place on that date or after it.
The format of the returned string must be as follows:
If there is no such a payment, it returns the string "No payment found!" (it is important that the string be EXACTLY this with capital N and ! at the end). |
biggestPurchase(MyDate)
|
This method takes a start date and returns (as a String) the
biggest purchase transaction that took place on that date or after it.
The format of this string must be "date:storename:amount"
with no extra space or comma. If there is no purchase during that
period, it returns the string "No purchase found!" (it is important
that the string be EXACTLY this with capital N and ! at the end). For
simplicity, ignore refunds.
|
|
Return the information about this credit card as a
where |
Note: You may assume that all
parameters to the methods are valid: no negative values for the quantities
and no null
values. No need to check for such values in your
methods or to write test cases for them.
In particular, assume
product
with the same name.
You may not use arrays, Vector
s,
ArrayList
s, or any other sort of list. Doing so
will result in a significant deduction.
Submit the completed signoff.txt
and
four .java
files ShoppingCart.java, CreditCard.java,
ShoppingCartTester.java
and CreditCardTester.java
.