商品类``

  1. public class Product {
  2. private String pId;
  3. private String pName;
  4. private String pSpecification;
  5. public Product() {
  6. }
  7. public Product(String pId, String pName, String pSpecification) {
  8. this.pId = pId;
  9. this.pName = pName;
  10. this.pSpecification = pSpecification;
  11. }
  12. public String getpId() {
  13. return pId;
  14. }
  15. public void setpId(String pId) {
  16. this.pId = pId;
  17. }
  18. public String getpName() {
  19. return pName;
  20. }
  21. public void setpName(String pName) {
  22. this.pName = pName;
  23. }
  24. public String getpSpecification() {
  25. return pSpecification;
  26. }
  27. public void setpSpecification(String pSpecification) {
  28. this.pSpecification = pSpecification;
  29. }
  30. }

卖家类

  1. public class Seller {
  2. private String sName;
  3. public Seller() {
  4. }
  5. public Seller(String sName) {
  6. this.sName = sName;
  7. }
  8. public String getsName() {
  9. return sName;
  10. }
  11. public void setsName(String sName) {
  12. this.sName = sName;
  13. }
  14. }

买家类

  1. public class Buyer {
  2. private String bId;
  3. private String bName;
  4. public Buyer() {
  5. }
  6. public Buyer(String bId, String bName) {
  7. this.bId = bId;
  8. this.bName = bName;
  9. }
  10. public String getbId() {
  11. return bId;
  12. }
  13. public void setbId(String bId) {
  14. this.bId = bId;
  15. }
  16. public String getbName() {
  17. return bName;
  18. }
  19. public void setbName(String bName) {
  20. this.bName = bName;
  21. }
  22. }

收货者类

  1. public class Receiver {
  2. private String rName;
  3. private String rPhone;
  4. private String rAddress;
  5. public Receiver() {
  6. }
  7. public Receiver(String rName, String rPhone, String rAddress) {
  8. this.rName = rName;
  9. this.rPhone = rPhone;
  10. this.rAddress = rAddress;
  11. }
  12. public String getrName() {
  13. return rName;
  14. }
  15. public void setrName(String rName) {
  16. this.rName = rName;
  17. }
  18. public String getrPhone() {
  19. return rPhone;
  20. }
  21. public void setrPhone(String rPhone) {
  22. this.rPhone = rPhone;
  23. }
  24. public String getrAddress() {
  25. return rAddress;
  26. }
  27. public void setrAddress(String rAddress) {
  28. this.rAddress = rAddress;
  29. }
  30. }

订单类

  1. import java.time.LocalDate;
  2. import java.time.LocalDateTime;
  3. import java.util.Objects;
  4. public class Order {
  5. private String oId;
  6. private LocalDate oTime;
  7. private String oNotes;
  8. private Buyer buyer;
  9. private Product product;
  10. private Seller seller;
  11. private Receiver receiver;
  12. private double payMoney;
  13. private LocalDateTime payTime;
  14. public Order() {
  15. }
  16. public Order(String oId, LocalDate oTime, String oNotes, Buyer buyer, Product product, Seller seller, Receiver receiver, double payMoney, LocalDateTime payTime) {
  17. this.oId = oId;
  18. this.oTime = oTime;
  19. this.oNotes = oNotes;
  20. this.buyer = buyer;
  21. this.product = product;
  22. this.seller = seller;
  23. this.receiver = receiver;
  24. this.payMoney = payMoney;
  25. this.payTime = payTime;
  26. }
  27. public String getoId() {
  28. return oId;
  29. }
  30. public void setoId(String oId) {
  31. this.oId = oId;
  32. }
  33. public LocalDate getoTime() {
  34. return oTime;
  35. }
  36. public void setoTime(LocalDate oTime) {
  37. this.oTime = oTime;
  38. }
  39. public String getoNotes() {
  40. return oNotes;
  41. }
  42. public void setoNotes(String oNotes) {
  43. this.oNotes = oNotes;
  44. }
  45. public Buyer getBuyer() {
  46. return buyer;
  47. }
  48. public void setBuyer(Buyer buyer) {
  49. this.buyer = buyer;
  50. }
  51. public Product getProduct() {
  52. return product;
  53. }
  54. public void setProduct(Product product) {
  55. this.product = product;
  56. }
  57. public Seller getSeller() {
  58. return seller;
  59. }
  60. public void setSeller(Seller seller) {
  61. this.seller = seller;
  62. }
  63. public Receiver getReceiver() {
  64. return receiver;
  65. }
  66. public void setReceiver(Receiver receiver) {
  67. this.receiver = receiver;
  68. }
  69. public double getPayMoney() {
  70. return payMoney;
  71. }
  72. public void setPayMoney(double payMoney) {
  73. this.payMoney = payMoney;
  74. }
  75. public LocalDateTime getPayTime() {
  76. return payTime;
  77. }
  78. public void setPayTime(LocalDateTime payTime) {
  79. this.payTime = payTime;
  80. }
  81. @Override
  82. public boolean equals(Object o) {
  83. if (this == o) return true;
  84. if (o == null || getClass() != o.getClass()) return false;
  85. Order order = (Order) o;
  86. if (Double.compare(order.payMoney, payMoney) != 0) return false;
  87. if (!Objects.equals(oId, order.oId)) return false;
  88. if (!Objects.equals(oTime, order.oTime)) return false;
  89. if (!Objects.equals(oNotes, order.oNotes)) return false;
  90. if (!Objects.equals(buyer, order.buyer)) return false;
  91. if (!Objects.equals(product, order.product)) return false;
  92. if (!Objects.equals(seller, order.seller)) return false;
  93. if (!Objects.equals(receiver, order.receiver)) return false;
  94. return Objects.equals(payTime, order.payTime);
  95. }
  96. @Override
  97. public int hashCode() {
  98. int result;
  99. long temp;
  100. result = oId != null ? oId.hashCode() : 0;
  101. result = 31 * result + (oTime != null ? oTime.hashCode() : 0);
  102. result = 31 * result + (oNotes != null ? oNotes.hashCode() : 0);
  103. result = 31 * result + (buyer != null ? buyer.hashCode() : 0);
  104. result = 31 * result + (product != null ? product.hashCode() : 0);
  105. result = 31 * result + (seller != null ? seller.hashCode() : 0);
  106. result = 31 * result + (receiver != null ? receiver.hashCode() : 0);
  107. temp = Double.doubleToLongBits(payMoney);
  108. result = 31 * result + (int) (temp ^ (temp >>> 32));
  109. result = 31 * result + (payTime != null ? payTime.hashCode() : 0);
  110. return result;
  111. }
  112. }

main函数

  1. import java.io.FileWriter;
  2. import java.io.IOException;
  3. import java.io.Writer;
  4. import java.time.LocalDate;
  5. import java.time.LocalDateTime;
  6. import java.util.ArrayList;
  7. import java.util.HashMap;
  8. import java.util.List;
  9. import java.util.Map;
  10. public class Test {
  11. public static void main(String[] args) throws IOException {
  12. List<Order> orderList = order();
  13. statistic(orderList);
  14. }
  15. public static List<Order> order() {
  16. Buyer buyer1 = new Buyer("liLei96", "李雷");
  17. Buyer buyer2 = new Buyer("hmm9822", "韩梅梅");
  18. Buyer buyer3 = new Buyer("mDongMei", "马冬梅");
  19. Seller seller1 = new Seller("小米旗舰店");
  20. Seller seller2 = new Seller("苹果旗舰店");
  21. Seller seller3 = new Seller("李佳奇优选");
  22. Receiver receiver1 = new Receiver("李有才", "13666666666", "上海市上海市静安区安心家园2号楼1门501");
  23. Receiver receiver2 = new Receiver("李雷", "13888888888", "北京市朝阳区望京中国尊86层");
  24. Receiver receiver3 = new Receiver("韩梅梅", "13999999999", "浙江省杭州市西湖区湖中心小岛");
  25. Receiver receiver4 = new Receiver("马冬梅", "13777777777", "四川省成都市双流机场T2航站楼206-7");
  26. Product product1 = new Product("SKU01", "小米手机", "8G+128G");
  27. Product product2 = new Product("SKU02", "苹果电脑", "2021款");
  28. Product product3 = new Product("SKU03", "MAC口红", "红色");
  29. Order order1 = new Order("D202101", LocalDate.of(2021, 1, 2), "蓝色", buyer1, product1, seller1, receiver1, 3999, LocalDateTime.of(2021, 1, 2, 12, 8, 28));
  30. Order order2 = new Order("D202102", LocalDate.of(2021, 1, 2), "银色", buyer1, product2, seller2, receiver2, 13999, LocalDateTime.of(2021, 1, 2, 10, 12, 21));
  31. Order order3 = new Order("D202103", LocalDate.of(2021, 1, 2), "银色", buyer2, product2, seller2, receiver3, 13999, LocalDateTime.of(2021, 2, 2, 11, 51, 37));
  32. Order order4 = new Order("D202104", LocalDate.of(2021, 1, 8), null, buyer2, product3, seller3, receiver3, 139.9, LocalDateTime.of(2021, 1, 8, 9, 46, 33));
  33. Order order5 = new Order("D202105", LocalDate.of(2021, 1, 31), null, buyer3, product3, seller3, receiver4, 139.9, LocalDateTime.of(2021, 2, 1, 1, 22, 6));
  34. List<Order> list = new ArrayList<>();
  35. list.add(order1);
  36. list.add(order2);
  37. list.add(order3);
  38. list.add(order4);
  39. list.add(order5);
  40. return list;
  41. }
  42. public static void statistic(List<Order> orderList) throws IOException {
  43. try (Writer output = new FileWriter("./file1")) {
  44. int num = 0;
  45. double money = 0;
  46. //将商品编号和名称存进map集合
  47. Map<String, String> map = new HashMap<>();
  48. for (Order o : orderList) {
  49. if (!map.containsKey(o.getProduct().getpId())) {
  50. map.put(o.getProduct().getpId(), o.getProduct().getpName());
  51. }
  52. }
  53. output.write("月份|商品编号|商品名称|销量|销售额\n");
  54. for (Map.Entry<String, String> me : map.entrySet()) {
  55. for (Order o : orderList) {
  56. if (o.getoTime().getMonthValue() == 1) {
  57. if (o.getProduct().getpId().equals(me.getKey())) {
  58. num++;
  59. money += o.getPayMoney();
  60. }
  61. }
  62. }
  63. output.write("2021年01月|" + me.getKey() + "|" + me.getValue() + "|" + num + "|" + money + "\n");
  64. //统计完一轮,将购买数和消费金额清零,进行下一轮循环
  65. num = 0;
  66. money = 0;
  67. }
  68. }
  69. try (Writer output = new FileWriter("./file2")) {
  70. int num = 0;
  71. double money = 0;
  72. //将买家id和姓名存储到map集合
  73. Map<String, String> map = new HashMap<>();
  74. for (Order o : orderList) {
  75. if (!map.containsKey(o.getBuyer().getbId())) {
  76. map.put(o.getBuyer().getbId(), o.getBuyer().getbName());
  77. }
  78. }
  79. output.write("月份|买家ID|买家名称|购买商品数量|消费总额\n");
  80. //将订单的用户id和map的key值比较,相同则进行统计
  81. for (Map.Entry<String, String> me : map.entrySet()) {
  82. for (Order o : orderList) {
  83. if (o.getoTime().getMonthValue() == 1) {
  84. if (o.getBuyer().getbId().equals(me.getKey())) {
  85. num++;
  86. money += o.getPayMoney();
  87. }
  88. }
  89. }
  90. output.write("2021年01月|" + me.getKey() + "|" + me.getValue() + "|" + num + "|" + money + "\n");
  91. //统计完一轮,将购买数和消费金额清零,进行下一轮循环
  92. num = 0;
  93. money = 0;
  94. }
  95. }
  96. try (Writer output = new FileWriter("./file3")) {
  97. double m = 0;
  98. //将卖家名字存入集合
  99. List<String> list = new ArrayList<>();
  100. for (Order o : orderList) {
  101. //如果名字不存在则添加进ArrayList集合
  102. if (!list.contains(o.getSeller().getsName())) {
  103. list.add(o.getSeller().getsName());
  104. }
  105. }
  106. output.write("月份|卖家名称|营业额\n");
  107. //将订单中的卖家名字同集合中卖家名字对比,相同则进而统计数据
  108. for (String s : list) {
  109. for (Order o : orderList) {
  110. if (o.getoTime().getMonthValue() == 1) {
  111. if (o.getSeller().getsName().equals(s)) {
  112. m += o.getPayMoney();
  113. }
  114. }
  115. }
  116. output.write("2021年01月|" + s + "|" + m + "\n");
  117. //每统计完一家店则将金额重新置零,进入下一家店的统计
  118. m = 0;
  119. }
  120. }
  121. }
  122. }