package edu.mama.ls16.homework;
import java.io.;
import java.math.BigDecimal;
import java.nio.charset.Charset;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.;
import java.time.LocalDate;
import java.time.LocalDateTime;
public class Homework {
_/**<br /> * 商城订单数据<br /> * _**@return<br /> **_*/<br /> _public static List<Order> order() {<br /> /*<br /> _TODO 定义若干个类,描述商城订单信息。创建订单对象,构造集合。<br /> _订单编号、 下单时间、 商品编号、 商品名称、 商品规格、订单备注、实付金额、支付时间、 卖家名称、 买家ID、 买家名称、 收货人姓名、收货人电话、 收货地址(省、市、区、街道小区门牌号)<br /> D202101 2021-01-02 SKU01 小米手机 8G+128G 蓝色 3999 2021-01-02 12:08:28 小米旗舰店 lilei96 李雷 李有才 13666666666 上海市上海市静安区安心家园2号楼1门501<br /> D202102 2021-01-02 SKU02 苹果电脑 2021款 银色 13999 2021-01-02 10:12:21 苹果旗舰店 lilei96 李雷 李雷 13888888888 北京市朝阳区望京中国尊86层<br /> D202103 2021-02-02 SKU02 苹果电脑 2021款 银色 13999 2021-02-02 11:51:37 苹果旗舰店 hmm9822 韩梅梅 韩梅梅 13999999999 浙江省杭州市西湖区湖中心小岛<br /> D202104 2021-01-08 SKU03 MAC口红 红色 139.9 2021-01-08 09:46:33 李佳奇优选 hmm9822 韩梅梅 韩梅梅 13999999999 浙江省杭州市西湖区湖中心小岛<br /> D202105 2021-01-31 SKU03 MAC口红 红色 139.9 2021-02-01 01:22:06 李佳奇优选 mdongmei 马冬梅 马冬梅 13777777777 四川省成都市双流机场T2航站楼206-7<br /> */<br /> //商品<br /> Commodity commodity1=new Commodity("SKU01","小米手机","8G+128G");<br /> Commodity commodity2=new Commodity("SKU02","苹果电脑","2021款");<br /> Commodity commodity3=new Commodity("SKU03","MAC口红","红色");<br /> //卖家<br /> Seller seller1=new Seller("小米旗舰店");<br /> Seller seller2=new Seller("苹果旗舰店");<br /> Seller seller3=new Seller("李佳奇优选");<br /> //买家<br /> Buyer buyer1=new Buyer("lilei96","李雷");<br /> Buyer buyer2=new Buyer("hmm9822","韩梅梅");<br /> Buyer buyer3=new Buyer("mdongmei","马冬梅");<br /> //快递信息<br /> Consignee consignee1=new Consignee("李有才","13666666666","上海","上海市","静安区","安心家园2号楼1门501");<br /> Consignee consignee2=new Consignee("李雷","13888888888","北京","北京市","朝阳区","望京中国尊86层");<br /> Consignee consignee3=new Consignee("韩梅梅","13999999999","浙江省","杭州市","西湖区","湖中心小岛");<br /> Consignee consignee4=new Consignee("马冬梅","13777777777","四川省","成都市","双流区","双流机场T2航站楼206-7");<br /> //订单<br /> Order order1=new Order("D202101",LocalDate._of_(2021,1,2),commodity1,seller1,"蓝色",new BigDecimal("3999"),buyer1,consignee1);<br /> order1.setPayTime(LocalDateTime._of_(2021,1,2,12,8,28));<br /> Order order2=new Order("D202102",LocalDate._of_(2021,1,2),commodity2,seller2,"银色",new BigDecimal("13999"),buyer1,consignee2);<br /> order2.setPayTime(LocalDateTime._of_(2021,1,2,10,12,21));<br /> Order order3=new Order("D202103",LocalDate._of_(2021,2,2),commodity2,seller2,"银色",new BigDecimal("13999"),buyer2,consignee3);<br /> order3.setPayTime(LocalDateTime._of_(2021,2,2,11,51,37));<br /> Order order4=new Order("D202104",LocalDate._of_(2021,1,8),commodity3,seller3,"",new BigDecimal("139.9"),buyer2,consignee3);<br /> order4.setPayTime(LocalDateTime._of_(2021,1,8,9,46,33));<br /> Order order5=new Order("D202105",LocalDate._of_(2021,1,31),commodity3,seller3,"",new BigDecimal("139.9"),buyer3,consignee4);<br /> order5.setPayTime(LocalDateTime._of_(2021,2,1,1,22,6));<br /> List<Order> list=new ArrayList<>();<br /> list.add(order1);<br /> list.add(order2);<br /> list.add(order3);<br /> list.add(order4);<br /> list.add(order5);<br /> return list;<br /> }
_/**<br /> * 统计并生成数据文件<br /> * _**@param **_orderList 销售订单<br /> */<br /> _public static void statistic(List<Order> orderList) {<br /> /*<br /> _TODO 2021年1月份每个商品的销量和销售额,生成一份数据文件,内容格式如下:<br /> _月份|商品编号|商品名称|销量|销售额<br /> 2021年01月|SKU01|小米手机|1|3999<br /> 2021年01月|SKU02|苹果电脑|2|27998<br /> 2021年01月|SKU03|MAC口红|2|279.8<br /> _TODO 2021年1月份每个买家的消费总额和购买商品数量,生成一份数据文件,内容格式如下:<br /> _月份|买家ID|买家名称|购买商品数量|消费总额<br /> 2021年01月|lilei96|李雷|2|17998<br /> 2021年01月|hmm9822|韩梅梅|2|14138.9<br /> 2021年01月|mdongmei|马冬梅|1|139.9<br /> _TODO 2021年1月份每个卖家的营业额,生成一份数据文件,内容格式如下:<br /> _月份|卖家名称|营业额<br /> 2021年01月|小米旗舰店|3999<br /> 2021年01月|苹果旗舰店|27998<br /> 2021年01月|李佳奇优选|279.8<br /> */<br /> final YearMonth reportYearMonth=YearMonth._of_(2021,1); //统计年月--2021年1月
Map<Commodity,TotalAmount> mapCommodity=new HashMap<>(); //统计商品<br /> Map<Buyer,TotalAmount> mapBuyer=new HashMap<>(); //统计买家<br /> Map<Seller,TotalAmount> mapSeller=new HashMap<>(); //统计卖家<br /> for(Order order:orderList){<br /> if(order.getDate().getYear()!=reportYearMonth.getYear() || order.getDate().getMonthValue()!=reportYearMonth.getMonthValue())<br /> continue;<br /> //merge方法,没有的添加,存在的调用函数合并得到新的结果<br /> mapCommodity.merge(order.getCommodity(),new TotalAmount(BigDecimal._ONE_,order.getPayAmount()),TotalAmount::add);<br /> mapBuyer.merge(order.getBuyer(),new TotalAmount(BigDecimal._ONE_,order.getPayAmount()),TotalAmount::add);<br /> mapSeller.merge(order.getSeller(),new TotalAmount(BigDecimal._ONE_,order.getPayAmount()),TotalAmount::add);<br /> }
//2021年1月份商品的销量和销售额<br /> try (Writer writer = new FileWriter(reportYearMonth.toString()+"商品销量与销售额.txt", Charset._forName_("UTF8"))) {<br /> writer.write("月份|商品编号|商品名称|销量|销售额"+"\n");<br /> Set<Commodity> commodities = mapCommodity.keySet();<br /> for (Commodity commodity : commodities) {<br /> TotalAmount ta=mapCommodity.get(commodity);<br /> StringBuilder sb = new StringBuilder();<br /> sb.append(reportYearMonth.format(DateTimeFormatter._ofPattern_("yyyy年MM月"))).append("|");<br /> sb.append(commodity.toString("|")).append("|");<br /> sb.append(ta.toString("|")).append("\n");<br /> writer.write(sb.toString());<br /> System._out_.print(sb.toString());<br /> }<br /> }catch (IOException e){<br /> e.printStackTrace();<br /> }
//2021年1月份买家购买数量和消费总额<br /> try (Writer writer = new FileWriter(reportYearMonth.toString()+"买家购买数量和消费总额.txt", Charset._forName_("UTF8"))) {<br /> writer.write("月份|买家ID|买家名称|购买商品数量|消费总额"+"\n");<br /> Set<Buyer> buyers = mapBuyer.keySet();<br /> for (Buyer buyer : buyers) {<br /> TotalAmount ta=mapBuyer.get(buyer);<br /> StringBuilder sb = new StringBuilder();<br /> sb.append(reportYearMonth.format(DateTimeFormatter._ofPattern_("yyyy年MM月"))).append("|");<br /> sb.append(buyer.toString("|")).append("|");<br /> sb.append(ta.toString("|")).append("\n");<br /> writer.write(sb.toString());<br /> System._out_.print(sb.toString());<br /> }<br /> }catch (IOException e){<br /> e.printStackTrace();<br /> }
//2021年1月份卖家营业额<br /> try (Writer writer = new FileWriter(reportYearMonth.toString()+"卖家营业额.txt", Charset._forName_("UTF8"))) {<br /> writer.write("月份|卖家名称|营业额"+"\n");<br /> Set<Seller> sellers = mapSeller.keySet();<br /> for (Seller seller : sellers) {<br /> TotalAmount ta = mapSeller.get(seller);<br /> StringBuilder sb = new StringBuilder();<br /> sb.append(reportYearMonth.format(DateTimeFormatter._ofPattern_("yyyy年MM月"))).append("|");<br /> sb.append(seller.toString("|")).append("|");<br /> sb.append(ta.getAmount().toString()).append("\n");<br /> writer.write(sb.toString());<br /> System._out_.print(sb.toString());<br /> }<br /> }catch (IOException e){<br /> e.printStackTrace();<br /> }<br /> }
public static void main(String[] args) {<br /> List<Order> list = _order_();<br /> for (Order order : list) {<br /> System._out_.println(order.toString(" "));<br /> }
_statistic_(list);
}<br />}