public class ShoppingCart
extends java.lang.Object
ShoppingCart.xml
specification. This uses the State Pattern (a Design Pattern from Gamma,
et al.) for its implementation. All requests are delegated to an abstract
State, which has concrete subclasses Ready, Shopping, Checkout and
Payment, which respond to certain requests and ignore others. This is a
good implementation strategy to ensure that no information is returned by
a service, when it is in an inappropriate state.
Otherwise, the shopping cart and the current stock are stored in Maps. As items are added to the cart, they are subtracted from the stock and the total cost of purchases is recalculated. Once there are items in the cart it is possible to go to the checkout. At the checkout, if valid billing information is supplied, payment can be initiated. This must be confirmed for the payment to go through. At any previous step, the user may exit the shop with no obligation. This example uses a user-defined symbolic type, called Dvd, to model the products sold in the shop.
Suggestions are given for how to modify the source code to seed faults deliberately, which will be detected during testing.
Constructor and Description |
---|
ShoppingCart()
Creates this ShoppingCart.
|
Modifier and Type | Method and Description |
---|---|
java.lang.Integer |
addItem(Dvd dvd)
In the Shopping state, adds one Dvd purchase to the cart, if enough
stock exists.
|
java.lang.Boolean |
checkout()
Attempts to checkout from the Shopping state.
|
void |
clearItems()
In the Shopping state, removes all Dvd purchases from the cart.
|
java.lang.Float |
confirm()
In the Payment state, confirms the payment and shipping address that
were previously supplied and makes the payment.
|
void |
enterShop()
Enters the Shopping state from either the Ready state (to start
shopping) or from the Checkout state (to return to shopping for extra
items before finally making payment).
|
void |
exitShop()
Exits the shop from every state, cancelling every action made so far.
|
java.lang.Float |
getBill()
In the Checkout or Payment states, enacts getBill/ok and returns the
total cost of items in the cart.
|
java.lang.String |
getScenario()
Returns the last scenario that was enacted.
|
java.lang.String |
getState()
Returns the last state that was entered.
|
java.lang.Boolean |
payBill(int billingInfo,
java.lang.String addressInfo)
In the Checkout state, attempts to pay the bill.
|
java.lang.Integer |
removeItem(Dvd dvd)
In the Shopping state, removes one Dvd purchase from the cart, if a
copy exists in the cart.
|
public ShoppingCart()
public java.lang.String getScenario()
public java.lang.String getState()
public void enterShop()
public void exitShop()
public java.lang.Integer addItem(Dvd dvd)
dvd
- the Dvd to add to the cart.public java.lang.Integer removeItem(Dvd dvd)
dvd
- the Dvd to take out of the cart.public void clearItems()
public java.lang.Boolean checkout()
public java.lang.Float getBill()
public java.lang.Boolean payBill(int billingInfo, java.lang.String addressInfo)
billingInfo
- an Integer payment authorisation.addressInfo
- the shipping address.public java.lang.Float confirm()