商品类``
public class Product {
private String pId;
private String pName;
private String pSpecification;
public Product() {
}
public Product(String pId, String pName, String pSpecification) {
this.pId = pId;
this.pName = pName;
this.pSpecification = pSpecification;
}
public String getpId() {
return pId;
}
public void setpId(String pId) {
this.pId = pId;
}
public String getpName() {
return pName;
}
public void setpName(String pName) {
this.pName = pName;
}
public String getpSpecification() {
return pSpecification;
}
public void setpSpecification(String pSpecification) {
this.pSpecification = pSpecification;
}
}
卖家类
public class Seller {
private String sName;
public Seller() {
}
public Seller(String sName) {
this.sName = sName;
}
public String getsName() {
return sName;
}
public void setsName(String sName) {
this.sName = sName;
}
}
买家类
public class Buyer {
private String bId;
private String bName;
public Buyer() {
}
public Buyer(String bId, String bName) {
this.bId = bId;
this.bName = bName;
}
public String getbId() {
return bId;
}
public void setbId(String bId) {
this.bId = bId;
}
public String getbName() {
return bName;
}
public void setbName(String bName) {
this.bName = bName;
}
}
收货者类
public class Receiver {
private String rName;
private String rPhone;
private String rAddress;
public Receiver() {
}
public Receiver(String rName, String rPhone, String rAddress) {
this.rName = rName;
this.rPhone = rPhone;
this.rAddress = rAddress;
}
public String getrName() {
return rName;
}
public void setrName(String rName) {
this.rName = rName;
}
public String getrPhone() {
return rPhone;
}
public void setrPhone(String rPhone) {
this.rPhone = rPhone;
}
public String getrAddress() {
return rAddress;
}
public void setrAddress(String rAddress) {
this.rAddress = rAddress;
}
}
订单类
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Objects;
public class Order {
private String oId;
private LocalDate oTime;
private String oNotes;
private Buyer buyer;
private Product product;
private Seller seller;
private Receiver receiver;
private double payMoney;
private LocalDateTime payTime;
public Order() {
}
public Order(String oId, LocalDate oTime, String oNotes, Buyer buyer, Product product, Seller seller, Receiver receiver, double payMoney, LocalDateTime payTime) {
this.oId = oId;
this.oTime = oTime;
this.oNotes = oNotes;
this.buyer = buyer;
this.product = product;
this.seller = seller;
this.receiver = receiver;
this.payMoney = payMoney;
this.payTime = payTime;
}
public String getoId() {
return oId;
}
public void setoId(String oId) {
this.oId = oId;
}
public LocalDate getoTime() {
return oTime;
}
public void setoTime(LocalDate oTime) {
this.oTime = oTime;
}
public String getoNotes() {
return oNotes;
}
public void setoNotes(String oNotes) {
this.oNotes = oNotes;
}
public Buyer getBuyer() {
return buyer;
}
public void setBuyer(Buyer buyer) {
this.buyer = buyer;
}
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
public Seller getSeller() {
return seller;
}
public void setSeller(Seller seller) {
this.seller = seller;
}
public Receiver getReceiver() {
return receiver;
}
public void setReceiver(Receiver receiver) {
this.receiver = receiver;
}
public double getPayMoney() {
return payMoney;
}
public void setPayMoney(double payMoney) {
this.payMoney = payMoney;
}
public LocalDateTime getPayTime() {
return payTime;
}
public void setPayTime(LocalDateTime payTime) {
this.payTime = payTime;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Order order = (Order) o;
if (Double.compare(order.payMoney, payMoney) != 0) return false;
if (!Objects.equals(oId, order.oId)) return false;
if (!Objects.equals(oTime, order.oTime)) return false;
if (!Objects.equals(oNotes, order.oNotes)) return false;
if (!Objects.equals(buyer, order.buyer)) return false;
if (!Objects.equals(product, order.product)) return false;
if (!Objects.equals(seller, order.seller)) return false;
if (!Objects.equals(receiver, order.receiver)) return false;
return Objects.equals(payTime, order.payTime);
}
@Override
public int hashCode() {
int result;
long temp;
result = oId != null ? oId.hashCode() : 0;
result = 31 * result + (oTime != null ? oTime.hashCode() : 0);
result = 31 * result + (oNotes != null ? oNotes.hashCode() : 0);
result = 31 * result + (buyer != null ? buyer.hashCode() : 0);
result = 31 * result + (product != null ? product.hashCode() : 0);
result = 31 * result + (seller != null ? seller.hashCode() : 0);
result = 31 * result + (receiver != null ? receiver.hashCode() : 0);
temp = Double.doubleToLongBits(payMoney);
result = 31 * result + (int) (temp ^ (temp >>> 32));
result = 31 * result + (payTime != null ? payTime.hashCode() : 0);
return result;
}
}
main函数
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
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 Test {
public static void main(String[] args) throws IOException {
List<Order> orderList = order();
statistic(orderList);
}
public static List<Order> order() {
Buyer buyer1 = new Buyer("liLei96", "李雷");
Buyer buyer2 = new Buyer("hmm9822", "韩梅梅");
Buyer buyer3 = new Buyer("mDongMei", "马冬梅");
Seller seller1 = new Seller("小米旗舰店");
Seller seller2 = new Seller("苹果旗舰店");
Seller seller3 = new Seller("李佳奇优选");
Receiver receiver1 = new Receiver("李有才", "13666666666", "上海市上海市静安区安心家园2号楼1门501");
Receiver receiver2 = new Receiver("李雷", "13888888888", "北京市朝阳区望京中国尊86层");
Receiver receiver3 = new Receiver("韩梅梅", "13999999999", "浙江省杭州市西湖区湖中心小岛");
Receiver receiver4 = new Receiver("马冬梅", "13777777777", "四川省成都市双流机场T2航站楼206-7");
Product product1 = new Product("SKU01", "小米手机", "8G+128G");
Product product2 = new Product("SKU02", "苹果电脑", "2021款");
Product product3 = new Product("SKU03", "MAC口红", "红色");
Order order1 = new Order("D202101", LocalDate.of(2021, 1, 2), "蓝色", buyer1, product1, seller1, receiver1, 3999, LocalDateTime.of(2021, 1, 2, 12, 8, 28));
Order order2 = new Order("D202102", LocalDate.of(2021, 1, 2), "银色", buyer1, product2, seller2, receiver2, 13999, LocalDateTime.of(2021, 1, 2, 10, 12, 21));
Order order3 = new Order("D202103", LocalDate.of(2021, 1, 2), "银色", buyer2, product2, seller2, receiver3, 13999, LocalDateTime.of(2021, 2, 2, 11, 51, 37));
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));
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));
List<Order> list = new ArrayList<>();
list.add(order1);
list.add(order2);
list.add(order3);
list.add(order4);
list.add(order5);
return list;
}
public static void statistic(List<Order> orderList) throws IOException {
try (Writer output = new FileWriter("./file1")) {
int num = 0;
double money = 0;
//将商品编号和名称存进map集合
Map<String, String> map = new HashMap<>();
for (Order o : orderList) {
if (!map.containsKey(o.getProduct().getpId())) {
map.put(o.getProduct().getpId(), o.getProduct().getpName());
}
}
output.write("月份|商品编号|商品名称|销量|销售额\n");
for (Map.Entry<String, String> me : map.entrySet()) {
for (Order o : orderList) {
if (o.getoTime().getMonthValue() == 1) {
if (o.getProduct().getpId().equals(me.getKey())) {
num++;
money += o.getPayMoney();
}
}
}
output.write("2021年01月|" + me.getKey() + "|" + me.getValue() + "|" + num + "|" + money + "\n");
//统计完一轮,将购买数和消费金额清零,进行下一轮循环
num = 0;
money = 0;
}
}
try (Writer output = new FileWriter("./file2")) {
int num = 0;
double money = 0;
//将买家id和姓名存储到map集合
Map<String, String> map = new HashMap<>();
for (Order o : orderList) {
if (!map.containsKey(o.getBuyer().getbId())) {
map.put(o.getBuyer().getbId(), o.getBuyer().getbName());
}
}
output.write("月份|买家ID|买家名称|购买商品数量|消费总额\n");
//将订单的用户id和map的key值比较,相同则进行统计
for (Map.Entry<String, String> me : map.entrySet()) {
for (Order o : orderList) {
if (o.getoTime().getMonthValue() == 1) {
if (o.getBuyer().getbId().equals(me.getKey())) {
num++;
money += o.getPayMoney();
}
}
}
output.write("2021年01月|" + me.getKey() + "|" + me.getValue() + "|" + num + "|" + money + "\n");
//统计完一轮,将购买数和消费金额清零,进行下一轮循环
num = 0;
money = 0;
}
}
try (Writer output = new FileWriter("./file3")) {
double m = 0;
//将卖家名字存入集合
List<String> list = new ArrayList<>();
for (Order o : orderList) {
//如果名字不存在则添加进ArrayList集合
if (!list.contains(o.getSeller().getsName())) {
list.add(o.getSeller().getsName());
}
}
output.write("月份|卖家名称|营业额\n");
//将订单中的卖家名字同集合中卖家名字对比,相同则进而统计数据
for (String s : list) {
for (Order o : orderList) {
if (o.getoTime().getMonthValue() == 1) {
if (o.getSeller().getsName().equals(s)) {
m += o.getPayMoney();
}
}
}
output.write("2021年01月|" + s + "|" + m + "\n");
//每统计完一家店则将金额重新置零,进入下一家店的统计
m = 0;
}
}
}
}