20.11. Introduction to the NextGen POS Program Solution
This section presents a sample domain layer of classes in Java for this iteration. The code generation is largely derived from the design class diagrams and interaction diagrams defined in the design work, based on the principles of mapping designs to code as previously explored.
The main point of this listing is to show that there is a translation from design artifacts to a foundation of code. This code defines a simple case; it is not meant to illustrate a robust, fully developed Java program with synchronization, exception handling, and so on. |
Class Payment
// all classes are probably in a package named
// something like:
package com.foo.nextgen.domain;
public class Payment
{
private Money amount;
public Payment( Money cashTendered ){ amount = cashTendered; }
public Money getAmount() { return amount; }
}
Class ProductCatalog
public class ProductCatalog
{
private Map<ItemID, ProductDescription>
descriptions = new HashMap()<ItemID, ProductDescription>;
public ProductCatalog()
{
// sample data
ItemID id1 = new ItemID( 100 );
ItemID id2 = new ItemID( 200 );
Money price = new Money( 3 );
ProductDescription desc;
desc = new ProductDescription( id1, price, "product 1" );
descriptions.put( id1, desc );
desc = new ProductDescription( id2, price, "product 2" );
descriptions.put( id2, desc );
}
public ProductDescription getProductDescription( ItemID id )
{
return descriptions.get( id );
}
}
Class Register
public class Register
{
private ProductCatalog catalog;
private Sale currentSale;
public Register( ProductCatalog catalog )
{
this.catalog = catalog;
}
public void endSale()
{
currentSale.becomeComplete();
}
public void enterItem( ItemID id, int quantity )
{
ProductDescription desc = catalog.getProductDescription( id );
currentSale.makeLineItem( desc, quantity );
}
public void makeNewSale()
{
currentSale = new Sale();
}
public void makePayment( Money cashTendered )
{
currentSale.makePayment( cashTendered );
}
}
Class ProductDescription
public class ProductDescription
{
private ItemID id;
private Money price;
private String description;
public ProductDescription
( ItemID id, Money price, String description )
{
this.id = id;
this.price = price;
this.description = description;
}
public ItemID getItemID() { return id; }
public Money getPrice() { return price; }
public String getDescription() { return description; }
}
Class Sale
public class Sale
{
private List<SalesLineItem> lineItems =
new ArrayList()<SalesLineItem>;
private Date date = new Date();
private boolean isComplete = false;
private Payment payment;
public Money getBalance()
{
return payment.getAmount().minus( getTotal() );
}
public void becomeComplete() { isComplete = true; }
public boolean isComplete() { return isComplete; }
public void makeLineItem
( ProductDescription desc, int quantity )
{
lineItems.add( new SalesLineItem( desc, quantity ) );
}
public Money getTotal()
{
Money total = new Money();
Money subtotal = null;
for ( SalesLineItem lineItem : lineItems )
{
subtotal = lineItem.getSubtotal();
total.add( subtotal );
}
return total;
}
public void makePayment( Money cashTendered )
{
payment = new Payment( cashTendered );
}
}
Class SalesLineItem
public class SalesLineItem
{
private int quantity;
private ProductDescription description;
public SalesLineItem (ProductDescription desc, int quantity )
{
this.description = desc;
this.quantity = quantity;
}
public Money getSubtotal()
{
return description.getPrice().times( quantity );
}
}
Class Store
public class Store
{
private ProductCatalog catalog = new ProductCatalog();
private Register register = new Register( catalog );
public Register getRegister() { return register; }
}