20.11. Introduction to the Program Solution

This section presents a sample domain object layer program solution 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

							public class Payment
							{
							private Money amount;
							public Payment( Money cashTendered ){ amount = cashTendered; }
							public Money getAmount() { return amount; }
							}
						

Class ProductCatalog

							public class ProductCatalog
							{
							private Map productSpecifications = new HashMap();
							public ProductCatalog()
							{
							// sample data
							ItemID id1 = new ItemID( 100 );
							ItemID id2 = new ItemID( 200 );
							Money price = new Money( 3 );
							ProductSpecification ps;
							ps = new ProductSpecification( id1, price, "product 1" );
							productSpecifications.put( id1, ps );
							ps = new ProductSpecification( id2, price, "product 2" );
							productSpecifications.put( id2, ps );
							}
							public ProductSpecification getSpecification( ItemID id )
							{
							return (ProductSpecification)productSpecifications.get( id );
							}
							}
						

Class Register

							public class Register
							{
							private ProductCatalog catalog;
							private Sale sale;
							public Register( ProductCatalog catalog )
							{
							this.catalog = catalog;
							}
							public void endSale()
							{
							sale.becomeComplete();
							}
							public void enterItem( ItemID id, int quantity )
							{
							ProductSpecification spec = catalog.getSpecification( id );
							sale.makeLineItem( spec, quantity );
							}
							public void makeNewSale()
							{
							sale = new Sale();
							}
							public void makePayment( Money cashTendered )
							{
							sale.makePayment( cashTendered );
							}
							}
						

Class ProductSpecification

							public class ProductSpecification
							{
							private ItemID id;
							private Money price;
							private String description;
							public ProductSpecification
							( 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 lineItems = new ArrayList();
							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
							( ProductSpecification spec, int quantity )
							{
							lineItems.add( new SalesLineItem( spec, quantity ) );
							}
							public Money getTotal()
							{
							Money total = new Money();
							Iterator i = lineItems.iterator();
							while( i.hasNext() )
							{
							SalesLineItem sli = (SalesLineItem) i.next();
							total.add( sli.getSubtotal() );
							}
							return total;
							}
							public void makePayment( Money cashTendered )
							{
							payment = new Payment( cashTendered );
							}
							}
						

Class SalesLineItem

							public class SalesLineItem
							{
							private int quantity;
							private ProductSpecification productSpec;
							public SalesLineItem (ProductSpecification spec, int quantity )
							{
							this.productSpec = spec;
							this.quantity = quantity;
							}
							public Money getSubtotal()
							{
							return productSpec.getPrice().times( quantity );
							}
							}
						

Class Store

							public class Store
							{
							private ProductCatalog catalog = new ProductCatalog();
							private Register register = new Register( catalog );
							public Register getRegister() { return register; }
							}
						

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.40.129