和風細雨

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

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

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

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


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

          代碼如下:

          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;

          /**
           * 水果統(tǒng)計信息類
           * 
           * 
          @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;

              
          // 統(tǒng)計信息
              private Hashtable<String,FruitSumary> fruitSumaries;

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

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

                  String fruitType
          =fruit.getType();
                  
                  
          // 取出同類型的統(tǒng)計信息
                  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類型對應的統(tǒng)計信息記錄
               * 
               * 
          @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;
              }

              
          /**
               * 取得水果進貨統(tǒng)計信息
               * 
               * 
          @return
               
          */
              
          public String getSummaryFruitInfo() {
                  String retval 
          = "----------水果進貨統(tǒng)計信息-----------\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
          ----------------------------------------
          ----------水果進貨統(tǒng)計信息-----------
          類型
          =Orange 總數量=33 總合計=198.5
          類型
          =Apple 總數量=22 總合計=23.0
          類型
          =Banana 總數量=42 總合計=149.0
          ----------------------------------------

           

          代碼下載:

          http://www.aygfsteel.com/Files/sitinspring/StockHouseManager20071125101919.rar

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


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


          網站導航:
           
          主站蜘蛛池模板: 兖州市| 淮滨县| 昆明市| 南皮县| 博客| 崇阳县| 通州市| 雷州市| 安阳市| 普洱| 永吉县| 调兵山市| 禹州市| 清丰县| 辽源市| 青浦区| 珠海市| 洞口县| 古丈县| 吉林市| 桑日县| 古蔺县| 河南省| 呼玛县| 榆林市| 洪泽县| 白河县| 安徽省| 临泽县| 大田县| 米林县| 沈阳市| 基隆市| 静安区| 金堂县| 山丹县| 峨边| 石景山区| 安阳县| 吉安县| 监利县|