Java Reference
In-Depth Information
customers, but you definitely don't want to create entries for foods if they don't
already exist in the database.
The final piece is a Shop service that coordinates purchases across the inventory
and customer accounting classes. It can be created by Blueprint and automatically
wired to the inventory and accounting beans:
<service
interface="fancyfoods.food.Shop">
<bean
class="fancyfoods.persistence.ShopImpl">
<tx:transaction
method="*"
value="Required" />
<property
name="inventory"
ref="inventory" />
<property
name="accounting"
ref="accounting" />
</bean>
</service>
The shop implementation will have its inventory and account dependencies injected.
To handle a purchase, it decrements the stock levels of the food, and debits the total
cost to the customer's account:
public int removeStock(String name, int quantity) {
FoodImpl food = getFood(name);
int currentLevel = food.getQuantityInStock();
int newLevel = currentLevel - quantity;
if (newLevel >= 0) {
food.setQuantityInStock(newLevel);
em.persist(food);
return newLevel;
} else {
throw new IllegalArgumentException(
"Cannot have level below 0: " + newLevel);
}
}
You don't want customers to be able to rack up credit indefinitely. If they've exceeded
their credit limit, no changes are made to the database and an exception is thrown.
ADDING A WEB FRONTEND
All that remains now is to hook the Shop service up to the web frontend. Start by liven-
ing up the display of the special offers. Instead of a static list of special offers, users
should be able to click on a food and bring up a purchase page (figure 3.12).
The link should lead to a new servlet that allows users to buy the food by filling in a
customer name and a purchase quantity (figure 3.13).
Search WWH ::




Custom Search