⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bestbookprice.java

📁 java web services how to program
💻 JAVA
字号:
// BestBookPrice.java
// BestBookPrice gets book price from each book store and 
// find the best price.
package jws1casestudy.pricefinder;

// Java core packages
import java.util.*;
import java.rmi.RemoteException;

// Deitel packages
import jws1casestudy.pricefinder.common.*;

public class BestBookPrice {

   // number of bookstores
   private static final int BOOKSTORES = 3; 
   
   // contains proxies to different bookstores
   private BookPriceProxy[] bookPriceProxies;
   
   // no-argument constructor
   public BestBookPrice() 
   {
      // get BookPriceProxy for each book store
      try {

         // instantiate BookPriceProxy array
         bookPriceProxies = new BookPriceProxy[ BOOKSTORES ];
         
         // populate array with BookPriceProxys
         bookPriceProxies[ 0 ] = 
            BookPriceProxyFactory1.createProxy();
         
         bookPriceProxies[ 1 ] =
            BookPriceProxyFactory2.createProxy();
         
         bookPriceProxies[ 2 ] =
            BookPriceProxyFactory3.createProxy();
                  
      } // end try
      
      // handle exception when creating URLs
      catch( Exception exception ) {
        exception.printStackTrace();
      } 
      
   } // end constructor
      
   // return best price from bookstores for given ISBN
   public PriceQuote getBestPrice( String isbn )
      throws RemoteException
   {
      
      // contains priceQuotes for given ISBN
      PriceQuote[] priceQuotes = new PriceQuote[ BOOKSTORES ];
      
      // get PriceQuote from each book store
      for ( int i = 0; i < BOOKSTORES ; i++ ) { 
         PriceQuote quote = null;

         // get book price from service proxy
         try {
            quote = bookPriceProxies[ i ].getPrice( isbn );
         }

         // handle exception in retrieving quote
         catch ( Exception exception ) {
            exception.printStackTrace();
         }

         priceQuotes[ i ] = quote;
      }

      // sort prices array
      Arrays.sort( priceQuotes, new PriceComparator());

      // throw exception if book ISBN does not exist
      if ( priceQuotes[ 0 ] == null ) {
         throw new RemoteException(
            "Bookstores do not carry " + isbn ); 
      }

      // return lowest price
      return priceQuotes[ 0 ];

   } // end method getBestPrice
   
   // utility class used to compare prices
   private class PriceComparator implements Comparator
   {
      // compare two PriceQuote objects
      public int compare( Object price1, Object price2 )
      {
         // base cases
         if ( price1 == null && price2 == null ) 
            return 0;

         if ( price1 == null ) 
            return -1;

         if ( price2 == null ) 
            return 1;

         PriceQuote priceQuote1 = ( PriceQuote ) price1;
         PriceQuote priceQuote2 = ( PriceQuote ) price2;

         return ( int ) 
            ( priceQuote1.getPrice() - priceQuote2.getPrice() );
      }

      // two PriceQuote are equal
      public boolean equals( Object object )
      {
         return object.equals ( this );
      }

   } // end class PriceComparator
   
} // end class BestBookPrice

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -