Enterprise Java Development@TOPIC@
/**
* Purchasing handles payment of purchased products.
*/
public interface Purchasing {
/**
* Creates an account for the user to use in purchasing products.
* @param email
* @param firstName
* @param lastName
* @return the Account created with primary key assigned
*/
Account createAccount(String email, String firstName, String lastName);
/**
* Completes the purchase of the items in the user's shopping cart,
* empties the cart, and returns the total cost paid.<p/>
*
* Note that this capability is not yet fully defined.
* @param email
* @param password
* @return amount charged as part of this checkout
*/
double checkout(String email, String password);
}
/**
* The catalog maintains a view of the inventory known to our application.
*/
public interface Catalog {
/**
* Returns a list of products in the catalog chunked into page sizes.
* @param offset
* @param limit
* @return list of products matching the paging criteria
*/
List<Product> getProducts(int offset, int limit);
/**
* Adds the selected product to the users' shopping cart and returns
* the count of items.
* @param id
* @param validEmail
* @return number of items in cart
*/
int addToCart(int id, String validEmail);
}
@Entity
@NamedQueries({
@NamedQuery(name="blPurchasing.findAccountByEmail",
query="select a from Account a where a.email=:email")
})
public class Account {
public static final String FIND_BY_EMAIL="blPurchasing.findAccountByEmail";
@Id @GeneratedValue
private int id;
@Column(nullable=false, unique=true)
private String email;
@Column(nullable=false)
private String password;
@Column(nullable=false)
private String firstName;
@Column(nullable=false)
private String lastName;
...
@Entity
public class Product {
@Id @GeneratedValue
private int id;
@Column(nullable=false)
private String name;
@Column(nullable=false)
private double price;
@Column(nullable=false)
private int count;
...
@Entity
public class Cart {
@Id
private String email;
@OneToOne
@PrimaryKeyJoinColumn(referencedColumnName="email")
private Account account;
@ManyToMany
private List<Product> products = new ArrayList<Product>();
...
Start with simple empty class and then added details over time. Just the fact that we have the concept of a Product is valuable. The fact that it can be described using a single name is an extra detail. The fact that name description should be required is additional detail. These details come over time and should not be a barrier for proposing the data concept and moving forward with the early design.
Test-first approach
/**
* A user shall be able to establish an account with just a first
* and last name and a unique email address.
*/
@Test
public void establishAccount() {
log.info("*** establishAccount ***");
//the user will supply their email address, first and last name
String email="jharb@ravens.com";
String firstName="john";
String lastName="harbaugh";
Account account = purchasing.createAccount(email, firstName, lastName);
//they will get back a generated password to use as a login for the account
assertNotNull("no account returned", account);
assertNotNull("no password assigned", account.getPassword());
}
/**
* A user shall be able browse products in the catalog.
*/
@Test
public void browseCatalog() {
log.info("*** browseCatalog ***");
//the user will ask for product summaries in pages
int pageSize=10;
int offset=0;
List<Product> products = catalog.getProducts(offset, pageSize);
//they will receive <= a page size of product information
assertNotNull("no products returned", products);
assertTrue("no products provided", products.size() > 0);
//they can page thru the entire set
for (int i=0; products.size() != 0; i++) {
offset += products.size();
products = catalog.getProducts(offset, pageSize);
assertTrue("this catalog never ends!!!", i<100);
}
}
/**
* A user shall be able to purchase a product in the catalog.
*/
@Test
public void purchaseProduct() {
log.info("*** purchaseProduct ***");
//the user selects a product
Product product=null;
Random random=new Random();
for (int i=0; product == null; i++) {
List<Product> products=catalog.getProducts(random.nextInt(100), 1);
product=products.iterator().next();
assertTrue("I can't find anything to buy!!!", i<1000);
}
//the user adds the product to their shopping cart by providing the
//product id and their credentials
int count=catalog.addToCart(product.getId(), validEmail);
//the user receives a count of the items in the cart
assertEquals("somebody tweeked my cart!!!!", 1, count);
//the user checks out with the cashier -- payment not yet implemented
double total=purchasing.checkout(validEmail, validPassword);
//the user gets a total amount back as their receipt
assertEquals("price doesn't add up", product.getPrice(), total, .01);
}
|-- pom.xml `-- src |-- main | |-- java | | `-- ejava | | `-- examples | | `-- blpurchase | | |-- bo | | | |-- Account.java | | | |-- Cart.java | | | `-- Product.java | | |-- bl | | | |-- Catalog.java | | | `-- Purchasing.java | | `-- blimpl | | |-- CatalogImpl.java | | `-- PurchasingImpl.java | `-- resources `-- test |-- java | `-- ejava | `-- examples | `-- blpurchase | `-- bl | |-- PurchaseTest.java | `-- PurchasingFactory.java `-- resources |-- hibernate.properties |-- log4j.xml `-- META-INF `-- persistence.xml