Maping Design To Code
Maping Design To Code
ProductDescription
SalesLineItem
description description : Text
quantity : Integer price : Money
1 itemID : ItemID
getSubtotal() : Money
...
Creating Methods from Interaction
Diagrams (Register.enterItem)
{
ProductDescription desc= catalog.ProductDescription(id); enterItem
currentSale.makeLineItem(desc, qty);
} method
of
Collections
• One-to-many relationships are common.
– For example, a Sale must maintain visibility to a group of many
SalesLineItem instances.
– In OO programming languages, these relationships are usually
implemented with the introduction of a collection object, such as a
List or Map, or even a simple array.
• Java libraries contain collection classes such as ArrayList and
HashMap, which implement the List and Map interfaces,
respectively.
• The choice of collection class is influenced by the
requirements;
– key-based lookup requires the use of a Map,
– a growing ordered list requires a List, and so on.
Collections
Sale
public class Sale
{ isComplete :
... Boolean time : SalesLineItem
DateTime
private List lineItems= new ArrayList(); e lineItems
becomeComplet () quantity : Integer
} 1..*
makeLineItem() getSubtotal()
makePayment()
getTtotal()
1
1 Sale 5
6
Register isComplete : 4
SalesLineItem
Boolean time :
...
DateTime
becomeComplet e() quantity : Integer
endSale() 1 1..*
makeLineItem(...)
enterItem(...) getSubtotal(
makePayme n(...)
makeNewSale() )
getTotal() t
makePayme n(...)
...
t
1
* Payment
amount :
1 Money
...
How to design makeNewSale ?
by Creator
and
Controller Register creates a
Sale by Creator by Creator, Sale
:Register creates an empty
collection (such as a
List) which will
eventually hold
SalesLineItem
instances
makeNewSale
create
:Sale
create lineItems :
List<SalesLineItem>
public
ProductDescription
( ItemID id,
Money price,
String
description )
{
this.id = id;
this.price = price;
this.description =
description;
}
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 SalesLineItem
(ProductDescription desc, int quantity
)
{
this.description = desc;
this.quantity = quantity;
}
public Money
getBalance()
{
return
payment.getAmount().minus( getTo
tal() );
}
{ return isComplete; }
Class Sale
public Money getTotal()
{
Money total = new Money();
Money subtotal = null;
for ( SalesLineItem
lineItem : lineItems )
{
subtotal = lineItem.getSubtotal();
total.add( subtotal );
}
return total;
}
class WorkPackage
{ // Details omitted };
class Project : public WorkPackage