developer.com
Search EarthWeb
CodeGuru | Gamelan | Jars | Wireless | Discussions
Navigate developer.com
Architecture & Design  
Database  
Java
Languages & Tools
Microsoft & .NET
Open Source  
Project Management  
Security  
Techniques  
Voice  
Web Services  
Wireless/Mobile
XML  
Technology Jobs  

   Developer.com Webcasts:
  The Impact of Coding Standards and Code Reviews

  Project Management for the Developer

  Defining Your Own Software Development Methodology

  more Webcasts...




See the Winners!


Developer Jobs

Be a Commerce Partner
Domain registration
Holiday Gift Ideas
Cell Phones
Imprinted Gifts
Memory
Televisions
Best Price
Promotional Gifts
Prepaid Phone Card
Corporate Gifts
Car Donations
Baby Photo Contest
Compare Prices
Promotional Products

 

Click Here
Download these IBM resources today!
e-Kit: IBM Rational Systems Development Solution
With systems teams under so much pressure to develop products faster, reduce production costs, and react to changing business needs quickly, communication and collaboration seem to get lost. Now, theres a way to improve product quality and communication.

Webcast: Asset Reuse Strategies for Success--Innovate Don't Duplicate!
Searching for, identifying, updating, using and deploying software assets can be a difficult challenge.

eKit: Rational Build Forge Express
Access valuable resources to help you increase staff productivity, compress development cycles and deliver better software, fast.

Download: IBM Data Studio v1.1
Effectively design, develop, deploy and manage your data, databases, and database applications throughout the data management life.

eKit: Rational Asset Manager
Learn how to do more with your reusable assets, learn how Rational Asset Manager tracks and audits your assets in order to utilize them for reuse.
Developer News -
SaaS Tool Offers Custom Database Development    May 9, 2008
Microsoft’s Automated Agent: Can We Talk?    May 7, 2008
Borland Finally Sells CodeGear    May 7, 2008
Red Hat Heads For The JON 2.0    May 7, 2008
Free Tech Newsletter -

Best Practices for Developing a Web Site: Checklists, Tips, Strategies & More. Download Exclusive eBook Now.

Working With Design Patterns: Adapter
By Jeff Langr

Go to page: 1  2  Next  

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;
   }
}

Go to page: 1  2  Next  


Tools:
Add www.developer.com to your favorites
Add www.developer.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed


Architecture & Design Archives

Is it time to make your move to the multi-threaded and parallel processing world? Find out!
Whitepaper: Embeddable Content Platform for OEM's
Best Practices for Developing a Web Site. Checklists, Tips & Strategies. Download Exclusive eBook Now.
Developing Intelligent Communications? Visit the Avaya DevConnect Center on DevX.
Learn about expanding business opportunities for the reseller channel. Visit IT Channel Planet.



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Win Server ‘08
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES