APS101S Winter 2008 Assignment 2

Shopping Cart and Credit Card

Purpose

This assignment will give you practice writing if statements, loops, and manipulating Strings. 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.

Introduction

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.

Test Classes

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:

Helper Methods

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.

Javadoc

For this assignment you must use Javadoc comments, including @param and @return tags.

Class MyDate (provided by us, download from here)

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

MyDate(int, int, int)

Constructor for the MyDate class. The parameters are year, month, and day respectively.

Method Description

toString()

Return the date as a String. The format is "year/month/day"

lte(MyDate d2)

Return true if the date is the same as or before date d2 (i.e. <= d2). Otherwise return false.

gte(MyDate d2)

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

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

String

The items currently in the shopping cart. The String has zero or more items stored in this form (there is no space before or after ":"):

       productName:quantity:unitPrice

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 String. Here is an example:

"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 ("");

ShoppingCart Methods

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

ShoppingCart(String, MyDate)

Constructor for the ShoppingCart class. The parameters are store name and date of purchase, respectively. There are no items in the shopping cart initially (i.e. empty string).

Method Description

getCartItems()

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)

addToCart(String, int,
double)

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.
 

removeFromCart(String)

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.

updateQuantity(String, int)

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 true, if product is not in the cart it returns false and leaves the cart unchanged. You may assume the new quantity is always greater than 0.

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 purchase method of the credit card. Only ONE transaction is posted to the credit card (so you must calculate the total amount of the transaction). If the purchase is successful using the credit card (see purchase method of the CreditCard class),  it empties the shopping cart and returns true; otherwise it returns false  (leaving shopping cart unchanged).

Note that if the shopping cart is empty no transaction is posted to the credit card and the method returns false.

 

Class CreditCard

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:

     Date:Name:Amount

  • Date has the form year/month/day as given by toString method of MyDate.
  • Name for purchases and refunds is the the store's name. Name  for bill payments is the string BillPayment
  • Amount is a double value. For purchases it's a positive value. For refunds and bill payments it's a negative value.

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 String.

Here is an example (note that we make no assumption about the date ordering of the transactions):

"2007/2/18:Futureshop:100.23,2006/12/20:BillPayment:-650.0,2007/1/20:Dominion:19.5"

Use the empty string ("") to indicate no transaction exists.

CreditCard Methods

Constructor Description

CreditCard(int, String, MyDate)

Constructor for the CreditCard class. The parameters (in order) are card number, name of the owner, and expiry date. There is no transaction posted initially.

Method Description

getTransactions()

Return the transactions (as a String). (Note: the transactions should be the same as that described in the instance variable above.)

purchase(MyDate, String, double)

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.

refund(MyDate, String, double)

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.

payBill(MyDate, double)

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:

  "Total:totamount,date1:amnt1,date2:amnt2,...,daten:amntn"

Where totamount is the total amount (a positive value) of bill payments during that period, datei:amnti are the date and amount (positive value)  of payments in that period. The order of payment transactions MUST BE the same as they appear in the transactions instance variable (i.e. must NOT sort or change the order).

 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.

 

toString()

Return the information about this credit card as a String in the following form (3 lines):

Number:n
Owner:o
Expiry:e
 

where n,o, and e are credit card number, its owner, and its expiry date
respectively (note there must be no space after :)
 

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

 

Restrictions:

You may not use arrays, Vectors, ArrayLists, or any other sort of list. Doing so will result in a significant deduction.

What to submit

Submit the completed signoff.txt and four .java files ShoppingCart.java, CreditCard.java, ShoppingCartTester.java and CreditCardTester.java.