Homework.java
package edu.mama.ls16.homework;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Homework {
/**
* 商城订单数据
* @return
*/
public static List<Order> order() {
/*
TODO 定义若干个类,描述商城订单信息。创建订单对象,构造集合。
订单编号、 下单时间、 商品编号、 商品名称、 商品规格、订单备注、实付金额、支付时间、 卖家名称、 买家ID、 买家名称、 收货人姓名、收货人电话、 收货地址(省、市、区、街道小区门牌号)
D202101 2021-01-02 SKU01 小米手机 8G+128G 蓝色 3999 2021-01-02 12:08:28 小米旗舰店 lilei96 李雷 李有才 13666666666 上海市上海市静安区安心家园2号楼1门501
D202102 2021-01-02 SKU02 苹果电脑 2021款 银色 13999 2021-01-02 10:12:21 苹果旗舰店 lilei96 李雷 李雷 13888888888 北京市朝阳区望京中国尊86层
D202103 2021-02-02 SKU02 苹果电脑 2021款 银色 13999 2021-02-02 11:51:37 苹果旗舰店 hmm9822 韩梅梅 韩梅梅 13999999999 浙江省杭州市西湖区湖中心小岛
D202104 2021-01-08 SKU03 MAC口红 红色 139.9 2021-01-08 09:46:33 李佳奇优选 hmm9822 韩梅梅 韩梅梅 13999999999 浙江省杭州市西湖区湖中心小岛
D202105 2021-01-31 SKU03 MAC口红 红色 139.9 2021-02-01 01:22:06 李佳奇优选 mdongmei 马冬梅 马冬梅 13777777777 四川省成都市双流机场T2航站楼206-7
*/
/*
订单:订单编号、下单时间、 订单备注、实付金额、支付时间、 商品、卖家、买家、收货人
商品:商品编号、 商品名称、 商品规格、
卖家:卖家名称
买家:买家ID、 买家名称
收货人:收货人姓名、收货人电话、 收货地址(省、市、区、街道小区门牌号)
*/
Product sku1 = new Product("SKU01", "小米手机", "8G+128G");
Product sku2 = new Product("SKU02", "苹果电脑", "2021款");
Product sku3 = new Product("SKU03", "MAC口红", "红色");
Store store1 = new Store("小米旗舰店");
Store store2 = new Store("苹果旗舰店");
Store store3 = new Store("李佳奇优选");
User ll = new User("lilei96", "李雷");
User hmm = new User("hmm9822", "韩梅梅");
User mdm = new User("mdongmei", "马冬梅");
Address addr1 = new Address("李有才", "13666666666", "上海市上海市静安区安心家园2号楼1门501");
Address addr2 = new Address("李雷", "13888888888", "北京市朝阳区望京中国尊86层");
Address addr3 = new Address("韩梅梅", "13999999999", "浙江省杭州市西湖区湖中心小岛");
Address addr4 = new Address("马冬梅", "13777777777", "四川省成都市双流机场T2航站楼206-7");
Order order1 = new Order(
"D202101",
LocalDate.of(2021,1,2),
"蓝色",
new BigDecimal("3999"),
LocalDateTime.of(2021,1,2,12,8,28),
sku1,
store1,
ll,
addr1
);
Order order2 = new Order(
"D202102",
LocalDate.of(2021,1,2),
"银色",
new BigDecimal("13999"),
LocalDateTime.of(2021,1,2,10,12,21),
sku2,
store2,
ll,
addr2
);
Order order3 = new Order(
"D202103",
LocalDate.of(2021,2,2),
"银色",
new BigDecimal("13999"),
LocalDateTime.of(2021,2,2,11,51,37),
sku2,
store2,
hmm,
addr3
);
Order order4 = new Order(
"D202104",
LocalDate.of(2021,1,8),
null,
new BigDecimal("139.9"),
LocalDateTime.of(2021,1,8,9,46,33),
sku3,
store3,
hmm,
addr3
);
Order order5 = new Order(
"D202105",
LocalDate.of(2021,1,31),
null,
new BigDecimal("139.9"),
LocalDateTime.of(2021,2,1,1,22,6),
sku3,
store3,
mdm,
addr4
);
List<Order> orderList = new ArrayList<>();
orderList.add(order1);
orderList.add(order2);
orderList.add(order3);
orderList.add(order4);
orderList.add(order5);
// for (Order order : orderList) {
// System.out.println(order.getActualAmount());
// }
return orderList;
}
/**
* 统计并生成数据文件
* @param orderList 销售订单
*/
public static void statistic(List<Order> orderList) {
/*
TODO 2021年1月份每个商品的销量和销售额,生成一份数据文件,内容格式如下:
月份|商品编号|商品名称|销量|销售额
2021年01月|SKU01|小米手机|1|3999
2021年01月|SKU02|苹果电脑|2|27998
2021年01月|SKU03|MAC口红|2|279.8
*/
Map<String, ProductSales> map = new HashMap<>();
//统计
for (Order order : orderList) {
if (order.getOrderDate().getYear() == 2021 && order.getOrderDate().getMonthValue() == 1) {
String skuCode = order.getProduct().getCode();
if (map.containsKey(skuCode)) {
map.get(skuCode).addSoldNum(1).addSoldAmount(order.getActualAmount());
} else {
map.put(skuCode, new ProductSales("2021年01月", order.getProduct(), 1, order.getActualAmount()));
}
}
}
//输出文件
try(Writer writer = new FileWriter("LS16/res/2021年1月份每个商品的销量和销售额.txt")) {
writer.write("月份|商品编号|商品名称|销量|销售额\n");
//输出数据
for (String key : map.keySet()) {
String str = String.format(
"%s|%s|%s|%d|%s\n",
"2021年01月",
map.get(key).getSku().getCode(),
map.get(key).getSku().getName(),
map.get(key).getSoldNum(),
map.get(key).getSoldAmount().toString()
);
writer.write(str);
}
} catch (IOException e) {
e.printStackTrace();
}
/*
TODO 2021年1月份每个买家的消费总额和购买商品数量,生成一份数据文件,内容格式如下:
月份|买家ID|买家名称|购买商品数量|消费总额
2021年01月|lilei96|李雷|2|17998
2021年01月|hmm9822|韩梅梅|2|14138.9
2021年01月|mdongmei|马冬梅|1|139.9
*/
Map<String ,BuyerSales> map1 = new HashMap<>();
for (Order order : orderList) {
if (order.getOrderDate().getYear() == 2021 && order.getOrderDate().getMonthValue() == 1) {
String userName = order.getBuyer().getName();
if (map1.containsKey(userName)) {
map1.get(userName).addSoldNum(1).addSoldAmount(order.getActualAmount());
} else {
map1.put(userName, new BuyerSales("2021年01月", order.getBuyer(), 1, order.getActualAmount()));
}
}
}
try(Writer writer = new FileWriter("LS16/res/2021年1月份每个买家的消费总额和购买商品数量.txt")) {
writer.write("月份|买家ID|买家名称|购买商品数量|消费总额\n");
//输出数据
for (String key : map1.keySet()) {
String str = String.format(
"%s|%s|%s|%d|%s\n",
"2021年01月",
map1.get(key).getUser().getId(),
map1.get(key).getUser().getName(),
map1.get(key).getSoldNum(),
map1.get(key).getSoldAmount().toString()
);
writer.write(str);
}
} catch (IOException e) {
e.printStackTrace();
}
/*
TODO 2021年1月份每个卖家的营业额,生成一份数据文件,内容格式如下:
月份|卖家名称|营业额
2021年01月|小米旗舰店|3999
2021年01月|苹果旗舰店|27998
2021年01月|李佳奇优选|279.8
*/
Map<String ,StoreSales> map2 = new HashMap<>();
for (Order order : orderList) {
if (order.getOrderDate().getYear() == 2021 && order.getOrderDate().getMonthValue() == 1) {
String storeName = order.getSeller().getName();
if (map2.containsKey(storeName)) {
map2.get(storeName).addSoldAmount(order.getActualAmount());
} else {
map2.put(storeName, new StoreSales("2021年01月", order.getSeller(), order.getActualAmount()));
}
}
}
try(Writer writer = new FileWriter("LS16/res/2021年1月份每个卖家的营业额.txt")) {
writer.write("月份|卖家名称|营业额\n");
//输出数据
for (String key : map2.keySet()) {
String str = String.format(
"%s|%s|%s\n",
"2021年01月",
map2.get(key).getStore().getName(),
map2.get(key).getSoldAmount().toString()
);
writer.write(str);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
List<Order> orders = order();
statistic(orders);
}
}
Product.java
商品(SKU)
package edu.mama.ls16.homework;
/**
* 商品(SKU)
*/
public class Product {
//商品编号
private String code;
//商品名称
private String name;
//商品规格
private String specs;
public Product(String code, String name, String specs) {
this.code = code;
this.name = name;
this.specs = specs;
}
public String getCode() {
return code;
}
public String getName() {
return name;
}
}
Store.java
商城店铺
package edu.mama.ls16.homework;
/**
* 商城店铺
*/
public class Store {
//卖家名称
private String name;
public Store(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
User.java
商城用户(买家)
package edu.mama.ls16.homework;
/**
* 商城用户(买家)
*/
public class User {
//买家ID
private String id;
//买家名称
private String name;
public User(String id, String name) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
}
Address.java
收货地址
package edu.mama.ls16.homework;
/**
* 收货地址
*/
public class Address {
//收货人姓名
private String name;
//收货人电话
private String mobile;
//收货地址
private String address;
public Address(String name, String mobile, String address) {
this.name = name;
this.mobile = mobile;
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
Order.java
商城订单
package edu.mama.ls16.homework;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
/**
* 商城订单
*/
public class Order {
//TODO 写下你的代码,包括属性、构造方法、getter和setter、其它方法
//订单编号
private String code;
//下单时间
private LocalDate orderDate;
//订单备注
private String remark;
//实付金额
private BigDecimal actualAmount;
//支付时间
private LocalDateTime payTime;
//所售商品
private Product product;
//卖家
private Store seller;
//买家
private User buyer;
//收货地址
private Address address;
public Order(String code, LocalDate orderDate, String remark, BigDecimal actualAmount, LocalDateTime payTime, Product product, Store seller, User buyer, Address address) {
this.code = code;
this.orderDate = orderDate;
this.remark = remark;
this.actualAmount = actualAmount;
this.payTime = payTime;
this.product = product;
this.seller = seller;
this.buyer = buyer;
this.address = address;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public LocalDate getOrderDate() {
return orderDate;
}
public void setOrderDate(LocalDate orderDate) {
this.orderDate = orderDate;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public BigDecimal getActualAmount() {
return actualAmount;
}
public void setActualAmount(BigDecimal actualAmount) {
this.actualAmount = actualAmount;
}
public LocalDateTime getPayTime() {
return payTime;
}
public void setPayTime(LocalDateTime payTime) {
this.payTime = payTime;
}
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
public Store getSeller() {
return seller;
}
public void setSeller(Store seller) {
this.seller = seller;
}
public User getBuyer() {
return buyer;
}
public void setBuyer(User buyer) {
this.buyer = buyer;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
ProductSales.java
商品销量统计
package edu.mama.ls16.homework;
import java.math.BigDecimal;
/**
* 商品销量统计
*/
public class ProductSales {
private String month;
//商品
private Product sku;
//销售量
private int soldNum;
//销售额
private BigDecimal soldAmount = BigDecimal.ZERO;
public ProductSales(String month, Product sku, int soldNum, BigDecimal soldAmount) {
this.month = month;
this.sku = sku;
this.soldNum = soldNum;
this.soldAmount = soldAmount;
}
//方法
public ProductSales addSoldNum(int soldNum) {
this.soldNum += soldNum;
return this;
}
public ProductSales addSoldAmount(BigDecimal soldAmount) {
this.soldAmount = this.soldAmount.add(soldAmount);
return this;
}
//getter
public String getMonth() {
return month;
}
public Product getSku() {
return sku;
}
public int getSoldNum() {
return soldNum;
}
public BigDecimal getSoldAmount() {
return soldAmount;
}
}
BuyerSales.java
用户消费统计
package edu.mama.ls16.homework;
import java.math.BigDecimal;
/**
* 用户消费统计
*/
public class BuyerSales {
//月份
private String month;
//买家
private User user;
//销售量
private int soldNum;
//销售额
private BigDecimal soldAmount = BigDecimal.ZERO;
public BuyerSales(String month, User user, int soldNum, BigDecimal soldAmount) {
this.month = month;
this.user = user;
this.soldNum = soldNum;
this.soldAmount = soldAmount;
}
public BuyerSales addSoldNum(int soldNum) {
this.soldNum += soldNum;
return this;
}
public BuyerSales addSoldAmount(BigDecimal soldAmount) {
this.soldAmount = this.soldAmount.add(soldAmount);
return this;
}
public String getMonth() {
return month;
}
public void setMonth(String month) {
this.month = month;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public int getSoldNum() {
return soldNum;
}
public void setSoldNum(int soldNum) {
this.soldNum = soldNum;
}
public BigDecimal getSoldAmount() {
return soldAmount;
}
public void setSoldAmount(BigDecimal soldAmount) {
this.soldAmount = soldAmount;
}
}
StoreSales.java
卖家营业额统计
package edu.mama.ls16.homework;
import java.math.BigDecimal;
/**
* 卖家营业额统计
*/
public class StoreSales {
//月份
private String month;
//店铺
private Store store;
//销售额
private BigDecimal soldAmount = BigDecimal.ZERO;
public StoreSales(String month, Store store, BigDecimal soldAmount) {
this.month = month;
this.store = store;
this.soldAmount = soldAmount;
}
public StoreSales addSoldAmount(BigDecimal soldAmount) {
this.soldAmount = this.soldAmount.add(soldAmount);
return this;
}
public String getMonth() {
return month;
}
public void setMonth(String month) {
this.month = month;
}
public Store getStore() {
return store;
}
public void setStore(Store store) {
this.store = store;
}
public BigDecimal getSoldAmount() {
return soldAmount;
}
public void setSoldAmount(BigDecimal soldAmount) {
this.soldAmount = soldAmount;
}
}
结果