// Implementation of the Point-of-Sale Terminal (POST) System in JAVA import java.util.*; // Class Payment public class Payment { private float amount; public Payment( float cashTendered ) { this.amount = cashTendered; } public float getAmount() { return amount; } } // Class ProductCatalog public class ProductCatalog { private Hashtable productSpecifications = new Hashtable(); public ProductCatalog() { ProductSpecification ps = new ProductSpecification( 100,1,"product1"); productSpecifications.put( new Integer( 100 ), ps ); ps = new ProductSpecification( 200,1,"product2" ); productSpecifications.put( new Integer( 200 ), ps ); } public ProductSpecification getSpecification( int upc ) { return (ProductSpecification) productSpecifications.get( new Integer( upc )); } } // Class POST class POST { private ProductCatalog productCatalog; private Sale sale; public POST( ProductCatalog catalog ) { productCatalog = catalog; } public void endSale() { sale.becomeComplete(); } public void enterItem( int upc, int quantity ) { if ( isNewSale() ) sale = new Sale(); ProductSpecification spec = productCatalog.getSpecification( upc ); sale.makeLineItem( spec,quantity ); } public void makePayment( float cashTendered ) { sale.makePayment( cashTendered ); } private boolean isNewSale() { return (( sale==null ) || ( sale.isComplete() )); } } // Class ProductSpecification public class ProductSpecification { private int upc = 0; private float price = 0; private String description = ""; public ProductSpecification( int upc, float price, String description ) { this.upc = upc; this.price = price; this.description = description; } public int getUPC() { return upc; } public float getPrice() { return price; } public String getDescription() { return description; } } // Class Sale class Sale { private Vector lineItems = new Vector(); private Date date = new Date(); private boolean isComplete = false; private Payment payment; public float getBalance() { return payment.getAmount() - total(); } public void becomeComplete() { isComplete = true; } public boolean isComplete() { return isComplete; } public void makeLineItem( ProductSpecification spec, int quantity ) { lineItems.addElement( new SaleLineItem( spec,quantity )); } public float total() { float total = 0; Enumeration e = lineItems.elements(); while ( e.hasMoreElements() ) { total += ((SaleLineItem)e.nextElement()).subtotal(); } return total; } public void makePayment( float cashTendered ) { payment = new Payment( cashTendered ); } } // Class SaleLineItem class SaleLineItem { private int quantity; private ProductSpecification productSpec; public SaleLineItem( ProductSpecification spec, int quantity ) { this.productSpec = spec; this.quantity = quantity; } public float subtotal() { return quantity*productSpec.getPrice(); } } // Class Store class Store { private ProductCatalog productCatalog = new ProductCatalog(); private POST post = new POST( productCatalog ); public POST getPOST() { return post; } }