October 19, 2018
Hot Topics:

Working With Design Patterns: Adapter

  • February 6, 2008
  • By Jeff Langr
  • Send Email »
  • More Articles »

The Portfolio class provides the basis for an application that allows users to track stock purchases. Of course, the most desired portfolio functionality is the ability to calculate the worth of all those stock purchases.

The PortfolioTest class, shown in Listing 1, demonstrates use of a simple stub to assist in verifying that the Portfolio can correctly obtain a value. The Portfolio class (see Listing 2) derives the price for each symbol by calling out through a StockLookupService interface. StockLookupService is as simple as it gets: Given a stock symbol, the service returns the symbol's current price (in dollars):

public interface StockLookupService {
   int currentPrice(String symbol);

Listing 1: PortfolioTest.

import static org.junit.Assert.*;
import org.junit.*;

public class PortfolioTest {
   private static final String MICROSOFT = "MSFT";
   private static final int MICROSOFT_VALUE = 100;
   private static final String IBM = "IBM";
   private static final int IBM_VALUE = 80;

   private Portfolio portfolio;

   public void initialize() {
      StockLookupService service = new StockLookupService() {
         public int currentPrice(String symbol) {
            if (symbol.equals(MICROSOFT))
               return MICROSOFT_VALUE;
            if (symbol.equals(IBM))
               return IBM_VALUE;
            return 0;

      portfolio = new Portfolio(service);

   public void isEmptyOnCreation() {
      assertEquals(0, portfolio.value());

   public void storesSharesPerSymbol() {
      portfolio.purchase(MICROSOFT, 2);
      assertEquals(2, portfolio.shares(MICROSOFT));
      assertEquals(2 * MICROSOFT_VALUE, portfolio.value());

   public void sumsSharesPurchasedForSameSymbol() {
      portfolio.purchase(MICROSOFT, 1);
      portfolio.purchase(MICROSOFT, 2);
      assertEquals(3, portfolio.shares(MICROSOFT));
      assertEquals(3 * MICROSOFT_VALUE, portfolio.value());

   public void segregatesPurchasesBySymbol() {
      portfolio.purchase(MICROSOFT, 5);
      portfolio.purchase(IBM, 10);
      assertEquals(5, portfolio.shares(MICROSOFT));
      assertEquals(10, portfolio.shares(IBM));
      int expectedValue = 5 * MICROSOFT_VALUE + 10 * IBM_VALUE;
      assertEquals(expectedValue, portfolio.value());

   void assertSize(int expected) {
      assertEquals(0 == expected, portfolio.isEmpty());
      assertEquals(expected, portfolio.size());

Listing 2: Portfolio.

import java.util.*;

public class Portfolio {
   private Map<String, Integer> symbols =
      new HashMap<String, Integer>();
   private StockLookupService service;

   public Portfolio(StockLookupService service) {
      this.service = service;

   public boolean isEmpty() {
      return 0 == size();

   public int size() {
      return symbols.size();

   public void purchase(String symbol, int shares) {
      symbols.put(symbol, shares(symbol) + shares);

   public int shares(String symbol) {
      if (!symbols.containsKey(symbol))
         return 0;
      return symbols.get(symbol);

   public int value() {
      int total = 0;
      for (Map.Entry<String, Integer> holding:
         symbols.entrySet()) {
         String symbol = holding.getKey();
         int shares = holding.getValue();
         total += service.currentPrice(symbol) * shares;
      return total;

Page 1 of 2

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

By submitting your information, you agree that developer.com may send you developer offers via email, phone and text message, as well as email offers about other products and services that developer believes may be of interest to you. developer will process your information in accordance with the Quinstreet Privacy Policy.


Thanks for your registration, follow us on our social networks to keep up-to-date