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

📄 dwprice.java

📁 用来为垂直搜索引擎抓取数据的采集系统
💻 JAVA
字号:
/*
 * *****************************************************
 * Copyright (c) 2005 IIM Lab. All  Rights Reserved.
 * Created by xuehao at Dec 5, 2005
 * Contact: zxuehao@mail.ustc.edu.cn
 * *****************************************************
 */
package org.indigo.db;

import java.util.ArrayList;
import java.util.List;
/**
 * 对价格数据中的单价进行处理类。
 * @author wbz
 *
 */

public class DwPrice
{
    private static DwPrice itsPrice=new DwPrice();
    private DwPrice()
    {
    }
    public static DwPrice getInstance()
    {
        return itsPrice;
    }
    /**
     * 对单价处理方法。
     * @param strPrice
     * @return
     */
    public String formatPrice( String strPrice )
    {
        if( strPrice==null )
        {
            return "";
        }
        strPrice = strPrice.trim();
        List list = new ArrayList();
        StringBuffer buf = new StringBuffer();
        char []ch = strPrice.toCharArray();
        int i=0; 
        double ptmp=0;
        /**
         * 循环读入价格。
         */
        for( i=0; i<ch.length; i++ )
        {
            if( Character.isDigit(ch[i]) || ch[i]=='.' )
                buf.append( ch[i] );
            else
            {
                if( buf.length()!=0 )
                {
	                list.add( buf.toString() );
	                buf = new StringBuffer();
                }
            }
        }
        if( i==ch.length )
        {
            list.add( buf.toString() );
            buf = null;
        }
        String str = null;
        double price=0;
        /**
         * 如果只有一个价格数据则就是所需要的价格。
         */
        if( list.size()==1 )
            str = (String) list.get( 0 );
        else//如果有几个价格数据,则取平均值。
        {
            int total=list.size();
            for( i=0; i<list.size(); i++ )
            {
                try
                {
                    ptmp =  Double.parseDouble( list.get(i).toString() );
                }catch( NumberFormatException e )
                {
                    ptmp = 0;
                    total--;
                }
                price += ptmp;
            }
            price /= total;
            str = String.valueOf( price );
        }
        return str;
    }

}

⌨️ 快捷键说明

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