基于Java Swing编写的简易运费计算工具

两年前给媳妇儿做的一个基于Java Swing编写的简易运费计算工具,现开源,关键是思路(https://github.com/honghailiang/FreightSystem)。主要有两个部分实现:1)初始化数据  2)用swing绘制系统并执行业务逻辑

一、初始化数据

从excel中读取数据,转化为对象(只保存基础数据:序列号、始发站、目的站、省份、100kg以下(元/kg)、100kg以上(元/kg)、到货(元/kg)、自提(元/kg)、补贴里程、公路里程、单价(元/吨公里))并保存到map(key为“出发地-目的地城市-省份”,value为AirFreight对象)中 表名和sheet名都是在配置文件中配置的(mango.properties),行和列及特殊字段也是在配置文件中配置的目的地和省份存在cityProvinceList中


	private static void readAirExcel(String fileName) {
		boolean isE2007 = false; 
		if (fileName.endsWith("xlsx")) {
			isE2007 = true;
		}

		Workbook wb = null;
		try {
			InputStream input = new FileInputStream(new File(UrlUtil
					.getRootUrl()
					+ "data/" + fileName)); 
			
			if (isE2007) {
				wb = new XSSFWorkbook(input);
			} else {
				wb = new HSSFWorkbook(input);
			}
			Sheet sheet = wb.getSheet(FreightConst.AIRSHEETNAME); 
			
			for (int j = FreightConst.ROWNUMBER; j < sheet
					.getPhysicalNumberOfRows(); j++) {
				Row row = sheet.getRow(j);
				AirFreight af = new AirFreight();
				af.setId((int) row.getCell(FreightConst.ID)
						.getNumericCellValue());
				af.setOriginStation(row.getCell(FreightConst.ORIGINSTATION)
						.getStringCellValue());
				af.setDestinationStation(row.getCell(
						FreightConst.DESTINATIONSTATION).getStringCellValue());
				af.setProvince(row.getCell(FreightConst.PROVINCE)
						.getStringCellValue());
				af.setUnitPriceF(row.getCell(FreightConst.UNITPRICEF)
						.getNumericCellValue());
				af.setUnitPriceT(row.getCell(FreightConst.UNITPRICET)
						.getNumericCellValue());
				af.setUnitPriceDH(row.getCell(FreightConst.UNITPRICEDH)
						.getNumericCellValue());
				af.setUnitPriceZT(row.getCell(FreightConst.UNITPRICEZT)
						.getNumericCellValue());
				af.setSubsidyMileage(row.getCell(FreightConst.SUBSIDYMILEAGE)
						.getNumericCellValue());
				af.setLandMileage(row.getCell(FreightConst.LANDMILEAGE)
						.getNumericCellValue());
				af.setLandUnitPrice(row.getCell(FreightConst.LANDUNITPRICE)
						.getNumericCellValue());

				
				AirContainer.airFreightMap.put(af.getOriginStation() + "-"
						+ af.getDestinationStation() + "-" + af.getProvince(),
						af);
				
				AirContainer.cityProvinceList.add(af.getDestinationStation()
						+ "-" + af.getProvince());
			}
			
		} catch (IOException ex) {
			ex.printStackTrace();
		} finally {
			if (wb != null) {
				try {
					wb.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}

二、用swing绘制系统并执行业务逻辑

建立FreightSwing,并初始化数据从cityProvinceList中获取城市和省份目的地做成可检索的(清除输入框的内容后键盘输入拼音(全拼)或者汉字点击enter键)用JAutoCompleteComboBox实现,其中包括用pinyin4j实现的中文向拼音的转换及输入拼音或者汉字匹配检索功能根据选择目的地、输入卷烟数量、选择的配送方式进行运费的计算,从map中取出AirFreight中的基础数据进行计算。

public static AirFreight computeFreight(String str, String takeType,
			String quantity) {
		double quan = Double.valueOf(quantity);
		AirFreight af = AirContainer.airFreightMap.get(str);
		af.setQuantity(quan);
		af.setTakeType(takeType);
		af.setFacToAirCost(quan * FreightConst.FACTOAIRF);
		af.setQuantityT(quan * FreightConst.KGF);
		double kg = af.getQuantityT();
		if (kg < 100) {
			af.setAirFreightCost(kg * af.getUnitPriceF());
		} else {
			af.setAirFreightCost(kg * af.getUnitPriceT());
		}

		af.setInsuranceRate(FreightConst.INSURANCERATE);
		af.setInsuranceCost(quan * FreightConst.INSURANCECOSTF);
		af.setInsurance(af.getInsuranceCost() * af.getInsuranceRate());
		af.setPackingCost(quan * FreightConst.PACKINGCOSTF);
		if (ZITI.equals(takeType)) {
			af.setTakeCost(af.getUnitPriceZT() * kg);
			af.setSendCost(0);
		} else {
			if (str.contains(FreightConst.DESTINATION)) {
				af.setSendCost(af.getUnitPriceDH() * kg
						+ FreightConst.DESTINATIONF);
			} else {
				af.setSendCost(af.getUnitPriceDH() * kg);
			}
			af.setTakeCost(0);
		}
		af.setAirTotalCost(FormatUtil.formatDouble(af.getFacToAirCost()
				+ af.getAirFreightCost() + af.getInsurance()
				+ af.getPackingCost() + af.getTakeCost() + af.getSendCost()));

		af.setTotalMilage(af.getSubsidyMileage() + af.getLandMileage());

		af.setLandTotalCost(FormatUtil.formatDouble(FreightConst.LANDCOSTF
				/ FreightConst.LANDCOSTT * af.getTotalMilage()
				* af.getLandUnitPrice()));
		af.setMarginCost(FormatUtil.formatDouble(af.getAirTotalCost()
				- af.getLandTotalCost()));

		return af;
	}

三、实现效果

基于Java Swing编写的简易运费计算工具_第1张图片

四、代码地址

https://github.com/honghailiang/FreightSystem

你可能感兴趣的:(java)