Homework.java

    1. package edu.mama.ls16.homework;
    2. import java.io.FileWriter;
    3. import java.io.IOException;
    4. import java.io.OutputStream;
    5. import java.io.Writer;
    6. import java.math.BigDecimal;
    7. import java.time.LocalDate;
    8. import java.time.LocalDateTime;
    9. import java.util.ArrayList;
    10. import java.util.HashMap;
    11. import java.util.List;
    12. import java.util.Map;
    13. public class Homework {
    14. /**
    15. * 商城订单数据
    16. * @return
    17. */
    18. public static List<Order> order() {
    19. /*
    20. TODO 定义若干个类,描述商城订单信息。创建订单对象,构造集合。
    21. 订单编号、 下单时间、 商品编号、 商品名称、 商品规格、订单备注、实付金额、支付时间、 卖家名称、 买家ID、 买家名称、 收货人姓名、收货人电话、 收货地址(省、市、区、街道小区门牌号)
    22. D202101 2021-01-02 SKU01 小米手机 8G+128G 蓝色 3999 2021-01-02 12:08:28 小米旗舰店 lilei96 李雷 李有才 13666666666 上海市上海市静安区安心家园2号楼1门501
    23. D202102 2021-01-02 SKU02 苹果电脑 2021款 银色 13999 2021-01-02 10:12:21 苹果旗舰店 lilei96 李雷 李雷 13888888888 北京市朝阳区望京中国尊86层
    24. D202103 2021-02-02 SKU02 苹果电脑 2021款 银色 13999 2021-02-02 11:51:37 苹果旗舰店 hmm9822 韩梅梅 韩梅梅 13999999999 浙江省杭州市西湖区湖中心小岛
    25. D202104 2021-01-08 SKU03 MAC口红 红色 139.9 2021-01-08 09:46:33 李佳奇优选 hmm9822 韩梅梅 韩梅梅 13999999999 浙江省杭州市西湖区湖中心小岛
    26. D202105 2021-01-31 SKU03 MAC口红 红色 139.9 2021-02-01 01:22:06 李佳奇优选 mdongmei 马冬梅 马冬梅 13777777777 四川省成都市双流机场T2航站楼206-7
    27. */
    28. /*
    29. 订单:订单编号、下单时间、 订单备注、实付金额、支付时间、 商品、卖家、买家、收货人
    30. 商品:商品编号、 商品名称、 商品规格、
    31. 卖家:卖家名称
    32. 买家:买家ID、 买家名称
    33. 收货人:收货人姓名、收货人电话、 收货地址(省、市、区、街道小区门牌号)
    34. */
    35. Product sku1 = new Product("SKU01", "小米手机", "8G+128G");
    36. Product sku2 = new Product("SKU02", "苹果电脑", "2021款");
    37. Product sku3 = new Product("SKU03", "MAC口红", "红色");
    38. Store store1 = new Store("小米旗舰店");
    39. Store store2 = new Store("苹果旗舰店");
    40. Store store3 = new Store("李佳奇优选");
    41. User ll = new User("lilei96", "李雷");
    42. User hmm = new User("hmm9822", "韩梅梅");
    43. User mdm = new User("mdongmei", "马冬梅");
    44. Address addr1 = new Address("李有才", "13666666666", "上海市上海市静安区安心家园2号楼1门501");
    45. Address addr2 = new Address("李雷", "13888888888", "北京市朝阳区望京中国尊86层");
    46. Address addr3 = new Address("韩梅梅", "13999999999", "浙江省杭州市西湖区湖中心小岛");
    47. Address addr4 = new Address("马冬梅", "13777777777", "四川省成都市双流机场T2航站楼206-7");
    48. Order order1 = new Order(
    49. "D202101",
    50. LocalDate.of(2021,1,2),
    51. "蓝色",
    52. new BigDecimal("3999"),
    53. LocalDateTime.of(2021,1,2,12,8,28),
    54. sku1,
    55. store1,
    56. ll,
    57. addr1
    58. );
    59. Order order2 = new Order(
    60. "D202102",
    61. LocalDate.of(2021,1,2),
    62. "银色",
    63. new BigDecimal("13999"),
    64. LocalDateTime.of(2021,1,2,10,12,21),
    65. sku2,
    66. store2,
    67. ll,
    68. addr2
    69. );
    70. Order order3 = new Order(
    71. "D202103",
    72. LocalDate.of(2021,2,2),
    73. "银色",
    74. new BigDecimal("13999"),
    75. LocalDateTime.of(2021,2,2,11,51,37),
    76. sku2,
    77. store2,
    78. hmm,
    79. addr3
    80. );
    81. Order order4 = new Order(
    82. "D202104",
    83. LocalDate.of(2021,1,8),
    84. null,
    85. new BigDecimal("139.9"),
    86. LocalDateTime.of(2021,1,8,9,46,33),
    87. sku3,
    88. store3,
    89. hmm,
    90. addr3
    91. );
    92. Order order5 = new Order(
    93. "D202105",
    94. LocalDate.of(2021,1,31),
    95. null,
    96. new BigDecimal("139.9"),
    97. LocalDateTime.of(2021,2,1,1,22,6),
    98. sku3,
    99. store3,
    100. mdm,
    101. addr4
    102. );
    103. List<Order> orderList = new ArrayList<>();
    104. orderList.add(order1);
    105. orderList.add(order2);
    106. orderList.add(order3);
    107. orderList.add(order4);
    108. orderList.add(order5);
    109. // for (Order order : orderList) {
    110. // System.out.println(order.getActualAmount());
    111. // }
    112. return orderList;
    113. }
    114. /**
    115. * 统计并生成数据文件
    116. * @param orderList 销售订单
    117. */
    118. public static void statistic(List<Order> orderList) {
    119. /*
    120. TODO 2021年1月份每个商品的销量和销售额,生成一份数据文件,内容格式如下:
    121. 月份|商品编号|商品名称|销量|销售额
    122. 2021年01月|SKU01|小米手机|1|3999
    123. 2021年01月|SKU02|苹果电脑|2|27998
    124. 2021年01月|SKU03|MAC口红|2|279.8
    125. */
    126. Map<String, ProductSales> map = new HashMap<>();
    127. //统计
    128. for (Order order : orderList) {
    129. if (order.getOrderDate().getYear() == 2021 && order.getOrderDate().getMonthValue() == 1) {
    130. String skuCode = order.getProduct().getCode();
    131. if (map.containsKey(skuCode)) {
    132. map.get(skuCode).addSoldNum(1).addSoldAmount(order.getActualAmount());
    133. } else {
    134. map.put(skuCode, new ProductSales("2021年01月", order.getProduct(), 1, order.getActualAmount()));
    135. }
    136. }
    137. }
    138. //输出文件
    139. try(Writer writer = new FileWriter("LS16/res/2021年1月份每个商品的销量和销售额.txt")) {
    140. writer.write("月份|商品编号|商品名称|销量|销售额\n");
    141. //输出数据
    142. for (String key : map.keySet()) {
    143. String str = String.format(
    144. "%s|%s|%s|%d|%s\n",
    145. "2021年01月",
    146. map.get(key).getSku().getCode(),
    147. map.get(key).getSku().getName(),
    148. map.get(key).getSoldNum(),
    149. map.get(key).getSoldAmount().toString()
    150. );
    151. writer.write(str);
    152. }
    153. } catch (IOException e) {
    154. e.printStackTrace();
    155. }
    156. /*
    157. TODO 2021年1月份每个买家的消费总额和购买商品数量,生成一份数据文件,内容格式如下:
    158. 月份|买家ID|买家名称|购买商品数量|消费总额
    159. 2021年01月|lilei96|李雷|2|17998
    160. 2021年01月|hmm9822|韩梅梅|2|14138.9
    161. 2021年01月|mdongmei|马冬梅|1|139.9
    162. */
    163. Map<String ,BuyerSales> map1 = new HashMap<>();
    164. for (Order order : orderList) {
    165. if (order.getOrderDate().getYear() == 2021 && order.getOrderDate().getMonthValue() == 1) {
    166. String userName = order.getBuyer().getName();
    167. if (map1.containsKey(userName)) {
    168. map1.get(userName).addSoldNum(1).addSoldAmount(order.getActualAmount());
    169. } else {
    170. map1.put(userName, new BuyerSales("2021年01月", order.getBuyer(), 1, order.getActualAmount()));
    171. }
    172. }
    173. }
    174. try(Writer writer = new FileWriter("LS16/res/2021年1月份每个买家的消费总额和购买商品数量.txt")) {
    175. writer.write("月份|买家ID|买家名称|购买商品数量|消费总额\n");
    176. //输出数据
    177. for (String key : map1.keySet()) {
    178. String str = String.format(
    179. "%s|%s|%s|%d|%s\n",
    180. "2021年01月",
    181. map1.get(key).getUser().getId(),
    182. map1.get(key).getUser().getName(),
    183. map1.get(key).getSoldNum(),
    184. map1.get(key).getSoldAmount().toString()
    185. );
    186. writer.write(str);
    187. }
    188. } catch (IOException e) {
    189. e.printStackTrace();
    190. }
    191. /*
    192. TODO 2021年1月份每个卖家的营业额,生成一份数据文件,内容格式如下:
    193. 月份|卖家名称|营业额
    194. 2021年01月|小米旗舰店|3999
    195. 2021年01月|苹果旗舰店|27998
    196. 2021年01月|李佳奇优选|279.8
    197. */
    198. Map<String ,StoreSales> map2 = new HashMap<>();
    199. for (Order order : orderList) {
    200. if (order.getOrderDate().getYear() == 2021 && order.getOrderDate().getMonthValue() == 1) {
    201. String storeName = order.getSeller().getName();
    202. if (map2.containsKey(storeName)) {
    203. map2.get(storeName).addSoldAmount(order.getActualAmount());
    204. } else {
    205. map2.put(storeName, new StoreSales("2021年01月", order.getSeller(), order.getActualAmount()));
    206. }
    207. }
    208. }
    209. try(Writer writer = new FileWriter("LS16/res/2021年1月份每个卖家的营业额.txt")) {
    210. writer.write("月份|卖家名称|营业额\n");
    211. //输出数据
    212. for (String key : map2.keySet()) {
    213. String str = String.format(
    214. "%s|%s|%s\n",
    215. "2021年01月",
    216. map2.get(key).getStore().getName(),
    217. map2.get(key).getSoldAmount().toString()
    218. );
    219. writer.write(str);
    220. }
    221. } catch (IOException e) {
    222. e.printStackTrace();
    223. }
    224. }
    225. public static void main(String[] args) {
    226. List<Order> orders = order();
    227. statistic(orders);
    228. }
    229. }

    Product.java
    商品(SKU)

    1. package edu.mama.ls16.homework;
    2. /**
    3. * 商品(SKU)
    4. */
    5. public class Product {
    6. //商品编号
    7. private String code;
    8. //商品名称
    9. private String name;
    10. //商品规格
    11. private String specs;
    12. public Product(String code, String name, String specs) {
    13. this.code = code;
    14. this.name = name;
    15. this.specs = specs;
    16. }
    17. public String getCode() {
    18. return code;
    19. }
    20. public String getName() {
    21. return name;
    22. }
    23. }

    Store.java
    商城店铺

    1. package edu.mama.ls16.homework;
    2. /**
    3. * 商城店铺
    4. */
    5. public class Store {
    6. //卖家名称
    7. private String name;
    8. public Store(String name) {
    9. this.name = name;
    10. }
    11. public String getName() {
    12. return name;
    13. }
    14. }

    User.java
    商城用户(买家)

    1. package edu.mama.ls16.homework;
    2. /**
    3. * 商城用户(买家)
    4. */
    5. public class User {
    6. //买家ID
    7. private String id;
    8. //买家名称
    9. private String name;
    10. public User(String id, String name) {
    11. this.id = id;
    12. this.name = name;
    13. }
    14. public String getId() {
    15. return id;
    16. }
    17. public String getName() {
    18. return name;
    19. }
    20. }

    Address.java
    收货地址

    1. package edu.mama.ls16.homework;
    2. /**
    3. * 收货地址
    4. */
    5. public class Address {
    6. //收货人姓名
    7. private String name;
    8. //收货人电话
    9. private String mobile;
    10. //收货地址
    11. private String address;
    12. public Address(String name, String mobile, String address) {
    13. this.name = name;
    14. this.mobile = mobile;
    15. this.address = address;
    16. }
    17. public String getName() {
    18. return name;
    19. }
    20. public void setName(String name) {
    21. this.name = name;
    22. }
    23. public String getMobile() {
    24. return mobile;
    25. }
    26. public void setMobile(String mobile) {
    27. this.mobile = mobile;
    28. }
    29. public String getAddress() {
    30. return address;
    31. }
    32. public void setAddress(String address) {
    33. this.address = address;
    34. }
    35. }

    Order.java
    商城订单

    1. package edu.mama.ls16.homework;
    2. import java.math.BigDecimal;
    3. import java.time.LocalDate;
    4. import java.time.LocalDateTime;
    5. /**
    6. * 商城订单
    7. */
    8. public class Order {
    9. //TODO 写下你的代码,包括属性、构造方法、getter和setter、其它方法
    10. //订单编号
    11. private String code;
    12. //下单时间
    13. private LocalDate orderDate;
    14. //订单备注
    15. private String remark;
    16. //实付金额
    17. private BigDecimal actualAmount;
    18. //支付时间
    19. private LocalDateTime payTime;
    20. //所售商品
    21. private Product product;
    22. //卖家
    23. private Store seller;
    24. //买家
    25. private User buyer;
    26. //收货地址
    27. private Address address;
    28. public Order(String code, LocalDate orderDate, String remark, BigDecimal actualAmount, LocalDateTime payTime, Product product, Store seller, User buyer, Address address) {
    29. this.code = code;
    30. this.orderDate = orderDate;
    31. this.remark = remark;
    32. this.actualAmount = actualAmount;
    33. this.payTime = payTime;
    34. this.product = product;
    35. this.seller = seller;
    36. this.buyer = buyer;
    37. this.address = address;
    38. }
    39. public String getCode() {
    40. return code;
    41. }
    42. public void setCode(String code) {
    43. this.code = code;
    44. }
    45. public LocalDate getOrderDate() {
    46. return orderDate;
    47. }
    48. public void setOrderDate(LocalDate orderDate) {
    49. this.orderDate = orderDate;
    50. }
    51. public String getRemark() {
    52. return remark;
    53. }
    54. public void setRemark(String remark) {
    55. this.remark = remark;
    56. }
    57. public BigDecimal getActualAmount() {
    58. return actualAmount;
    59. }
    60. public void setActualAmount(BigDecimal actualAmount) {
    61. this.actualAmount = actualAmount;
    62. }
    63. public LocalDateTime getPayTime() {
    64. return payTime;
    65. }
    66. public void setPayTime(LocalDateTime payTime) {
    67. this.payTime = payTime;
    68. }
    69. public Product getProduct() {
    70. return product;
    71. }
    72. public void setProduct(Product product) {
    73. this.product = product;
    74. }
    75. public Store getSeller() {
    76. return seller;
    77. }
    78. public void setSeller(Store seller) {
    79. this.seller = seller;
    80. }
    81. public User getBuyer() {
    82. return buyer;
    83. }
    84. public void setBuyer(User buyer) {
    85. this.buyer = buyer;
    86. }
    87. public Address getAddress() {
    88. return address;
    89. }
    90. public void setAddress(Address address) {
    91. this.address = address;
    92. }
    93. }

    ProductSales.java
    商品销量统计

    1. package edu.mama.ls16.homework;
    2. import java.math.BigDecimal;
    3. /**
    4. * 商品销量统计
    5. */
    6. public class ProductSales {
    7. private String month;
    8. //商品
    9. private Product sku;
    10. //销售量
    11. private int soldNum;
    12. //销售额
    13. private BigDecimal soldAmount = BigDecimal.ZERO;
    14. public ProductSales(String month, Product sku, int soldNum, BigDecimal soldAmount) {
    15. this.month = month;
    16. this.sku = sku;
    17. this.soldNum = soldNum;
    18. this.soldAmount = soldAmount;
    19. }
    20. //方法
    21. public ProductSales addSoldNum(int soldNum) {
    22. this.soldNum += soldNum;
    23. return this;
    24. }
    25. public ProductSales addSoldAmount(BigDecimal soldAmount) {
    26. this.soldAmount = this.soldAmount.add(soldAmount);
    27. return this;
    28. }
    29. //getter
    30. public String getMonth() {
    31. return month;
    32. }
    33. public Product getSku() {
    34. return sku;
    35. }
    36. public int getSoldNum() {
    37. return soldNum;
    38. }
    39. public BigDecimal getSoldAmount() {
    40. return soldAmount;
    41. }
    42. }

    BuyerSales.java
    用户消费统计

    1. package edu.mama.ls16.homework;
    2. import java.math.BigDecimal;
    3. /**
    4. * 用户消费统计
    5. */
    6. public class BuyerSales {
    7. //月份
    8. private String month;
    9. //买家
    10. private User user;
    11. //销售量
    12. private int soldNum;
    13. //销售额
    14. private BigDecimal soldAmount = BigDecimal.ZERO;
    15. public BuyerSales(String month, User user, int soldNum, BigDecimal soldAmount) {
    16. this.month = month;
    17. this.user = user;
    18. this.soldNum = soldNum;
    19. this.soldAmount = soldAmount;
    20. }
    21. public BuyerSales addSoldNum(int soldNum) {
    22. this.soldNum += soldNum;
    23. return this;
    24. }
    25. public BuyerSales addSoldAmount(BigDecimal soldAmount) {
    26. this.soldAmount = this.soldAmount.add(soldAmount);
    27. return this;
    28. }
    29. public String getMonth() {
    30. return month;
    31. }
    32. public void setMonth(String month) {
    33. this.month = month;
    34. }
    35. public User getUser() {
    36. return user;
    37. }
    38. public void setUser(User user) {
    39. this.user = user;
    40. }
    41. public int getSoldNum() {
    42. return soldNum;
    43. }
    44. public void setSoldNum(int soldNum) {
    45. this.soldNum = soldNum;
    46. }
    47. public BigDecimal getSoldAmount() {
    48. return soldAmount;
    49. }
    50. public void setSoldAmount(BigDecimal soldAmount) {
    51. this.soldAmount = soldAmount;
    52. }
    53. }

    StoreSales.java
    卖家营业额统计

    1. package edu.mama.ls16.homework;
    2. import java.math.BigDecimal;
    3. /**
    4. * 卖家营业额统计
    5. */
    6. public class StoreSales {
    7. //月份
    8. private String month;
    9. //店铺
    10. private Store store;
    11. //销售额
    12. private BigDecimal soldAmount = BigDecimal.ZERO;
    13. public StoreSales(String month, Store store, BigDecimal soldAmount) {
    14. this.month = month;
    15. this.store = store;
    16. this.soldAmount = soldAmount;
    17. }
    18. public StoreSales addSoldAmount(BigDecimal soldAmount) {
    19. this.soldAmount = this.soldAmount.add(soldAmount);
    20. return this;
    21. }
    22. public String getMonth() {
    23. return month;
    24. }
    25. public void setMonth(String month) {
    26. this.month = month;
    27. }
    28. public Store getStore() {
    29. return store;
    30. }
    31. public void setStore(Store store) {
    32. this.store = store;
    33. }
    34. public BigDecimal getSoldAmount() {
    35. return soldAmount;
    36. }
    37. public void setSoldAmount(BigDecimal soldAmount) {
    38. this.soldAmount = soldAmount;
    39. }
    40. }

    结果
    image.png