A Composite - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

A Composite

Description:

A Composite – PowerPoint PPT presentation

Number of Views:127
Avg rating:3.0/5.0
Slides: 14
Provided by: rickmercer
Category:
Tags: composite | stocks | tech

less

Transcript and Presenter's Notes

Title: A Composite


1
A Composite
  • C Sc 227, Spring 2005

2
Composite Design Pattern
  • Compose objects into tree structures to represent
    part-whole hierarchies.
  • Composite lets clients treat individual objects
    and compositions of objects uniformly

3
General Form
4
Client
  • Portfolio overall new Portfolio("my IRA")
  • // Add one leaf
  • overall.add(new Stock("Seven Eleven", 500,
    9.15))
  • // Build and add a Portfolio to a Portfolio
  • Portfolio tech new Portfolio("Tech stocks")
  • Stock s1 new Stock("Sun", 20, 30.50)
  • tech.add(s1)
  • tech.add(new MutualFund("Microsoft", 13, 45.20))
  • overall.add(tech)
  • // Can update prices
  • s1.updatePrice(31.50) // up 50 cents
  • // Show all elements with indents
  • overall.listAll()

5
Desired Output
  • my IRA 5762.6
  • 500 shares Seven Eleven 4575.0
  • Tech stocks 1187.6
  • 20 shares Sun 600.0
  • 13.0 shares Microsoft 587.6

6
Team Activity
  • A) Draw the UML Diagram to capture the real
    world example of stocks, mutual funds, and
    portfolios
  • List all class names
  • Draw the relationships
  • Place the listAll method in the correct place
  • Place the add method in the correct place
  • Place the getValue methods in the correct place

7
An Answer
8
Implement Asset Commodity
  • While the Value of a Portfolio is the sum of all
    assets, the value of a Commodity is the shares
    times the price
  • B) Using the UML diagram, write the Java code for
    Asset and Commodity (a leaf)

9
Answer
  • abstract class Asset
  • private String myName
  • public Asset(String name)
  • myName name
  • public String toString()
  • return myName " " getValue()
  • public abstract double getValue()

10
Answer
  • abstract class Commodity extends Asset
  • private double myUnits
  • private double myPrice
  • public Commodity(String name, double shares,
    double price)
  • super(name)
  • myUnits shares
  • myPrice price
  • public double getValue()
  • return myUnits myPrice

11
Stock and MutualFund
  • C) Implement Mutual Fund and Stock
  • D) Implement Portfolio with the getValue method
    only (no listAll)

12
Answer
  • class Stock extends Commodity
  • public Stock(String company, int shares, double
    price)
  • super(shares " shares " company, price,
    shares)
  • class MutualFund extends Commodity
  • public MutualFund(String fund, double shrs,
    double price)
  • super(shares " shares " fund, shrs,
    price)

13
Answer
  • class Portfolio extends Asset
  • private ArrayList myAssets
  • public static final String INDENT " "
  • public Portfolio(String name)
  • super(name)
  • myAssets new ArrayList()
  • public void add(Asset h)
  • myAssets.add(h)
  • public double getValue()
  • double sum 0.0
Write a Comment
User Comments (0)
About PowerShow.com