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

📄 htmlsalesformatter.java

📁 这是我修读美国卡耐基梅隆大学Carnegie Mellon University(CMU)课程ssd3:Object-Oriented Programming and Design时完成的课程设计
💻 JAVA
字号:
import java.util.*;

/**
 * The class HTMLSalesFormatter implements the interface <code>SalesFormatter</code>. 
 * This class is implemented as a singleton so a new object will not be 
 * created every time the HTML format is used.
 *
 * @author 张维
 * @version 1.0.0
 * @see SalesFormatter
 * @see Order
 * @see Product
 * @see OrderItem
 * @see Sales
 * @see Catalog
 * @see Coffee
 * @see CoffeeBrewer
 */
 public class HTMLSalesFormatter implements SalesFormatter {
 	
 	/* Line separator */
  private static final String NEW_LINE = System.getProperty("line.separator");
        
 	/*The single instance of class HTMLSalesFormatter */
 	private static HTMLSalesFormatter  singletonInstance = null;

  /**
   * Returns the single instance of class <code>HTMLSalesFormatter</code>
   *
   * @return the single instance  of class <code>HTMLSalesFormatter</code>
   */
   public static HTMLSalesFormatter getSingletonInstance( ) {

      if (singletonInstance == null) {
          singletonInstance = new HTMLSalesFormatter();
      }
      return singletonInstance;
   }

   /**
    * The constructor is declared private so other classes cannot
    * create an instance of this class.
    */
    private HTMLSalesFormatter( ) {
    	
    }

   /**
    * Returns an HTML representation of the specified sales.
    *
    * @param sales  the Sales object that contains the orders that have been sold.
    * @return  an HTML representation of the specified <code. Sales} object.
    */
    public String formatSales(Sales sales) {
    	
       String s = "<html>" + NEW_LINE + "  <body>" + NEW_LINE 
                  + "    <center><h2>Orders</h2></center>" + NEW_LINE;
        
       for(Iterator iterator = sales.getOrdersIterator( ); iterator.hasNext( );) {
        	
          Order order = (Order)iterator.next( );
            
          s = s + "    <hr>" + NEW_LINE + "    <h4>Total = "
              + order.getTotalCost( ) + "</h4>" + NEW_LINE;
            
          Iterator iterator1 = order.getItemsIterator( );
            
          while(iterator1.hasNext( )) {
              OrderItem orderitem = (OrderItem)iterator1.next( );
              s = s + "      <p>" + NEW_LINE + "        <b>code:</b> " 
                  + orderitem.getProduct( ).getCode( ) + "<br>" + NEW_LINE 
                  + "        <b>quantity:</b> " + orderitem.getQuantity( ) + "<br>" + NEW_LINE 
                  + "        <b>price:</b> " + orderitem.getProduct( ).getPrice( ) + NEW_LINE 
                  + "      </p>" + NEW_LINE;
            }
        }
        s = s + "  </body>" + NEW_LINE + "</html>";
        return s;
    }      
}

⌨️ 快捷键说明

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