如果有以下需求,你是一个货栈的仓库保管员,货栈进口以下多种水果,目前主要是苹果,香蕉和桔子,货栈不但需要记录每批次的品种,单价,还要得出每种水果的总个数,总钱数. 请问该如何编制程序.
记录每批次不难,一个批次链表就可以解决问题,有点意思的部分在于得出每种水果的总个数,总钱数,如果说在加入批次信息时进行处理,根据类型分别判断,用六个量(分别针对每种水果的总个数,总钱数)分别进行统计.这种方法容易使程序固化,如果增加一个水果品种的话会导致增加两个变量,所以说这里统计信息需要动态起来,用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://www.blogjava.net/Files/sitinspring/StockHouseManager20071125101919.rar