<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    和風細雨

    世上本無難事,心以為難,斯乃真難。茍不存一難之見于心,則運用之術自出。

    關于分類匯總的一個常見技巧

    如果有以下需求,你是一個貨棧的倉庫保管員,貨棧進口以下多種水果,目前主要是蘋果,香蕉和桔子,貨棧不但需要記錄每批次的品種,單價,還要得出每種水果的總個數,總錢數. 請問該如何編制程序.

    記錄每批次不難,一個批次鏈表就可以解決問題,有點意思的部分在于得出每種水果的總個數,總錢數,如果說在加入批次信息時進行處理,根據類型分別判斷,用六個量(分別針對每種水果的總個數,總錢數)分別進行統計.這種方法容易使程序固化,如果增加一個水果品種的話會導致增加兩個變量,所以說這里統計信息需要動態起來,用List或Hashtable是個不錯的選擇,相對而言Hashtable在種類較多時更好.


    具體來說做法是用一個Hashtable存儲統計信息,Hashtable的鍵為水果類型,Hashtable的值為統計信息;每當有新批次水果到達時,查看Hashtable中是否有這種類型的統計信息,有則取出信息,在原基礎上加上新值,否則加入一個新統計信息.

    代碼如下:

    package com.sitinspring;

    /**
     * 水果基類
     * 
     * 
    @author: sitinspring(junglesong@gmail.com)
     * @date: 2007-11-25
     
    */
    public class Fruit {
        
    // 水果批次類型
        protected String type;

        
    // 水果批次數量
        protected int count;

        
    // 單價
        protected double unitPrice;

        
    public Fruit(int count, double unitPrice) {
            
    this.count = count;
            
    this.unitPrice = unitPrice;
        }

        
    public int getCount() {
            
    return count;
        }

        
    public String getType() {
            
    return type;
        }

        
    public double getUnitPrice() {
            
    return unitPrice;
        }

        
    public String toString() {
            
    return "類型=" + type + " 數量=" + count + " 單價=" + unitPrice + " 合計="
                    
    + count * unitPrice + "\r\n";
        }
    }

     

    package com.sitinspring;

    /**
     * 蘋果類
     * 
     * 
    @author: sitinspring(junglesong@gmail.com)
     * @date: 2007-11-25
     
    */
    public class Apple extends Fruit {
        
    public Apple(int count, double unitPrice) {
            
    super(count, unitPrice);

            
    this.type = "Apple";
        }
    }

     

    package com.sitinspring;

    /**
     * 香蕉類
     * 
     * 
    @author: sitinspring(junglesong@gmail.com)
     * @date: 2007-11-25
     
    */
    public class Banana extends Fruit {
        
    public Banana(int count, double unitPrice) {
            
    super(count, unitPrice);

            
    this.type = "Banana";
        }
    }
    package com.sitinspring;

    /**
     * 桔子類
     * 
    @author: sitinspring(junglesong@gmail.com)
     * @date: 2007-11-25
     
    */
    public class Orange extends Fruit{
        
    public Orange(int count,double unitPrice){
            
    super(count,unitPrice);
            
            
    this.type="Orange";
        }
    }


    package com.sitinspring;

    /**
     * 水果統計信息類
     * 
     * 
    @author: sitinspring(junglesong@gmail.com)
     * @date: 2007-11-25
     
    */
    public class FruitSumary {
        
    // 類型
        protected String type;

        
    // 總數
        protected int count;

        
    // 總價
        protected double price;

        
    public FruitSumary(String type, int count, double unitPrice) {
            
    this.type = type;
            
    this.count = count;
            
    this.price = unitPrice;
        }

        
    public String toString() {
            
    return "類型=" + type + " 總數量=" + count + " 總合計=" + price + "\r\n";
        }

        
    public int getCount() {
            
    return count;
        }

        
    public void setCount(int count) {
            
    this.count = count;
        }

        
    public double getPrice() {
            
    return price;
        }

        
    public void setPrice(double price) {
            
    this.price = price;
        }

        
    public String getType() {
            
    return type;
        }

        
    public void setType(String type) {
            
    this.type = type;
        }
    }


    package com.sitinspring;

    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Hashtable;
    import java.util.List;

    /**
     * 貨棧進貨記錄
     * 
     * 
    @author: sitinspring(junglesong@gmail.com)
     * @date: 2007-11-25
     
    */
    public class StockHouseMemo {
        
    // 水果批次記錄
        private List<Fruit> fruits;

        
    // 統計信息
        private Hashtable<String,FruitSumary> fruitSumaries;

        
    /**
         * 添加水果批次記錄
         * 
         * 
    @param fruit
         
    */
        
    public void addFruit(Fruit fruit) {
            
    // 添加批次記錄
            if (fruits == null) {
                fruits 
    = new ArrayList<Fruit>();
            }
            fruits.add(fruit);

            
    // 添加統計記錄
            if (fruitSumaries == null) {
                fruitSumaries 
    = new Hashtable<String,FruitSumary>();
            }

            String fruitType
    =fruit.getType();
            
            
    // 取出同類型的統計信息
            FruitSumary sumaryFound = findSummaryByType(fruitType);
            
    if (sumaryFound != null) {
                
    // 有記錄
                sumaryFound.setCount(sumaryFound.getCount()
                        
    + fruit.getCount());
                sumaryFound.setPrice(sumaryFound.getPrice()
                        
    + fruit.getUnitPrice() * fruit.getCount());
            } 
    else {
                
    // 無記錄
                fruitSumaries.put(fruitType,new FruitSumary(fruit.getType(),
                        fruit.getCount(), fruit.getUnitPrice() 
    * fruit.getCount()));
            }
        }

        
    /**
         * 取得和fruitInput類型對應的統計信息記錄
         * 
         * 
    @param fruitInput
         * 
    @return
         
    */
        
    private FruitSumary findSummaryByType(String fruitType) {
            
    if(fruitSumaries.containsKey(fruitType)){
                
    return fruitSumaries.get(fruitType);
            }
            
            
    return null;
        }

        
    /**
         * 取得水果進貨詳細信息
         * 
         * 
    @return
         
    */
        
    public String getDetailFruitInfo() {
            String retval 
    = "----------水果進貨詳細信息-----------\r\n";
            
            
    for (Fruit fruit : fruits) {
                retval 
    += fruit; // 等于fruit.toString()
            }

            retval 
    += "----------------------------------------";
            
    return retval;
        }

        
    /**
         * 取得水果進貨統計信息
         * 
         * 
    @return
         
    */
        
    public String getSummaryFruitInfo() {
            String retval 
    = "----------水果進貨統計信息-----------\r\n";

            
    for (FruitSumary fruitSumary : fruitSumaries.values()) {
                retval 
    += fruitSumary; // 等于fruitSumary.toString()
            }

            retval 
    += "----------------------------------------";
            
    return retval;
        }

        
    public void writeToFile(String fileName) {
            
    try {
                BufferedWriter out 
    = new BufferedWriter(new FileWriter(fileName));

                String outputText 
    = getDetailFruitInfo()+"\r\n";
                outputText 
    += getSummaryFruitInfo();

                out.write(outputText);
                out.close();
            } 
    catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    package com.sitinspring;

    /**
     * 程序入口
     * 
    @author: sitinspring(junglesong@gmail.com)
     * @date: 2007-11-25
     
    */
    public class Main{
        
    public static void main(String[] args){
            StockHouseMemo stackHouseMemo
    =new StockHouseMemo();
            
            stackHouseMemo.addFruit(
    new Apple(10,0.5));
            stackHouseMemo.addFruit(
    new Apple(12,1.5));
            stackHouseMemo.addFruit(
    new Banana(13,2.5));
            stackHouseMemo.addFruit(
    new Banana(14,3.5));
            stackHouseMemo.addFruit(
    new Banana(15,4.5));
            stackHouseMemo.addFruit(
    new Orange(16,5.5));
            stackHouseMemo.addFruit(
    new Orange(17,6.5));
            
            System.out.println(stackHouseMemo.getDetailFruitInfo());    
            System.out.println(stackHouseMemo.getSummaryFruitInfo());
            
            stackHouseMemo.writeToFile(
    "輸出信息.txt");
        }
    }


    輸出為:

    ----------水果進貨詳細信息-----------
    類型
    =Apple 數量=10 單價=0.5 合計=5.0
    類型
    =Apple 數量=12 單價=1.5 合計=18.0
    類型
    =Banana 數量=13 單價=2.5 合計=32.5
    類型
    =Banana 數量=14 單價=3.5 合計=49.0
    類型
    =Banana 數量=15 單價=4.5 合計=67.5
    類型
    =Orange 數量=16 單價=5.5 合計=88.0
    類型
    =Orange 數量=17 單價=6.5 合計=110.5
    ----------------------------------------
    ----------水果進貨統計信息-----------
    類型
    =Orange 總數量=33 總合計=198.5
    類型
    =Apple 總數量=22 總合計=23.0
    類型
    =Banana 總數量=42 總合計=149.0
    ----------------------------------------

     

    代碼下載:

    http://m.tkk7.com/Files/sitinspring/StockHouseManager20071125101919.rar

    posted on 2008-02-22 10:31 和風細雨 閱讀(341) 評論(0)  編輯  收藏


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 亚洲黄色网站视频| 91麻豆国产自产在线观看亚洲| 国产l精品国产亚洲区在线观看| 成年免费大片黄在线观看com| 国产美女无遮挡免费网站| 亚洲中文字幕日本无线码| 亚洲综合免费视频| 亚洲AV色吊丝无码| 成人免费男女视频网站慢动作| 亚洲中文无码线在线观看| 国产91色综合久久免费分享| 亚洲精品美女视频| 一本无码人妻在中文字幕免费 | 亚洲乱码中文字幕在线| 一二三四在线播放免费观看中文版视频| 亚洲日韩在线视频| 免费看韩国黄a片在线观看| 亚洲乱码日产精品一二三| 日本免费观看网站| 一区二区三区免费在线视频| 亚洲男人的天堂在线va拉文| 国产日韩在线视频免费播放| 亚洲av日韩综合一区在线观看| 亚洲视频免费在线看| 亚洲字幕AV一区二区三区四区| 国产公开免费人成视频| a级毛片免费观看视频| 亚洲综合久久成人69| 天天看免费高清影视| 人人爽人人爽人人片av免费| 亚洲av无码不卡一区二区三区 | 免费播放在线日本感人片| 亚洲天堂中文资源| 无码一区二区三区免费视频| 国产精品亚洲一区二区三区| 亚洲国产精品无码AAA片| 黄色网址免费大全| 人妖系列免费网站观看| 亚洲精品资源在线| 亚洲第一视频在线观看免费| 最近2019免费中文字幕6|