September 17, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

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;

   @Before
   public void initialize() {
      StockLookupService service = new StockLookupService() {
         @Override
         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);
   }

   @Test
   public void isEmptyOnCreation() {
      assertSize(0);
      assertEquals(0, portfolio.value());
   }

   @Test
   public void storesSharesPerSymbol() {
      portfolio.purchase(MICROSOFT, 2);
      assertSize(1);
      assertEquals(2, portfolio.shares(MICROSOFT));
      assertEquals(2 * MICROSOFT_VALUE, portfolio.value());
   }

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

   @Test
   public void segregatesPurchasesBySymbol() {
      portfolio.purchase(MICROSOFT, 5);
      portfolio.purchase(IBM, 10);
      assertSize(2);
      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.

 

 


Sitemap | Contact Us

Rocket Fuel