Programming Assignment # 3

Due: November 16.


This is a two-part assignment.  The assignment in this first part is to write the back end of a shopping program. A rudimentary driver will test the classes. (The classes and methods are specified below). The second part will be the final programming assignment, where you add a nice front end, including GUIs, interface handling, event handling, inheritance, etc.

The shopping program has ShoppingCarts, CashRegisters, Items, and ChangeBaskets (which are like billfolds, or wallets). The overall idea is that when you shop, you take a shopping cart and put some number of items into it. When you are done shopping, you take your cart to the checkout line, where the total purchase price of all your items is calculated. You then give money to the cashier from your ChangeBasket, change is returned from the cash register, and added back into your wallet. The money you gave the cashier is deposited into the cash register.

You should write at least the following classes and methods as specified below to implement this program. (It is fine if your implementation uses more classes and more methods than these).


Item class: an item has a name (e.g."dairy", "vegetables", "cleanser", "papergoods"), and a price. (See below for suggestion of static arrays). In addition to the constructor, methods include:

getPrice: returns the price of an item.

getName: returns the name of the item.


ChangeBasket class: a ChangeBasket can be thought of as a kind of a wallet. It keeps track of how many coins and bills are in a given instance of ChangeBasket. You only have to keep track of pennies, nickels, dimes, quarters, ones, fives, and tens. There could be several constructors for a ChangeBasket: it might be initially set to 0, or perhaps it initially have a fixed number, say 5, of every coin or bill, or perhaps it is set to exactly have some specified amount of each. Methods in the ChangeBasket class include:

getTotal: adds up the total worth of the ChangeBasket.

addCoins: add coins from another ChangeBasket to the current basket.

makeChange: given a ChangeBasket, and an amount, make up the amount from the given ChangeBasket. For example, if the ChangeBasket is out of tens, you may have to use two fives to make change.

makePayment: given a ChangeBasket, and an amount, make up at least that amount from the given ChangeBasket.  Note that this method has a lot in common with the makeChange method above. However, if a person is paying for goods from the money in their wallet (their ChangeBasket), and doesn't have exact change, the ChangeBasket resulting from the makePayment method can be larger than the required amount. (In this case, the person will get change back.)


CashRegister class: a cash register has a ChangeBasket. (In the next version of this assignment, it will inherit from ChangeBasket - for now, it just has an instance variable that is a ChangeBasket). Useful constructors might have the initially empty register, or a register that starts with a certain number of bills and coins. Methods include:

closeRegister: total the money in the register.

doTransaction: take as input a price, and a ChangeBasket of money to pay for it; return the change made up from the register's ChangeBasket.


ShoppingCart class: A shopping cart contains a list of items. There can be big carts and small carts (depending on how much shopping you want to do), so the constructor might take an argument specifying the maximum number of items the shopping cart holds. Methods include:

addItem: put another item in the shopping cart, (if it fits).

checkout: total up the cost of all items in the shopping cart.


Your main program will test these classes by making a cart, and putting items in it. You can use a dialog box to prompt for this input. It may be helpful to have pre-defined items (for example in a static array in the Item class) with known prices, so that you only have to enter the string that matches the name of the item. Your main program will also make an initial wallet, for example say it has $40 in it. It is this ChangeBasket that you use to pay for all your shopping trips.

Your main program should create a cash register using one of the constructors above. The initial amount of money in the register should be output, along with the denominations. The last thing your program should do is close the register and print the final amount of money (and what denominations they are). Naturally, this should tally with the number of purchases that have been made at this register, and the amount of money left in the wallet. For now, without the GUIs that we will use in the next part of this assignment, it would be easiest if you have your methods simply print what they are doing. It will also help you to include statements for debugging purposes that display the states of your objects.