-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathOrder.java
More file actions
70 lines (53 loc) · 1.99 KB
/
Order.java
File metadata and controls
70 lines (53 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package org.geekbang.time.commonmistakes.java8;
import lombok.Data;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Random;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import static java.util.stream.Collectors.toList;
/**
* 订单类
*/
@Data
public class Order {
private Long id;
private Long customerId;
private String customerName;
/**
* 订单商品明细
*/
private List<OrderItem> orderItemList;
/**
* 总价格
*/
private Double totalPrice;
/**
* 下单时间
*/
private LocalDateTime placedAt;
public static List<Order> getData() {
List<Product> products = Product.getData();
List<Customer> customers = Customer.getData();
Random random = new Random();
return LongStream.rangeClosed(1, 10).mapToObj(i -> {
Order order = new Order();
order.setId(i);
order.setPlacedAt(LocalDateTime.now().minusHours(random.nextInt(24 * 365)));
order.setOrderItemList(IntStream.rangeClosed(1, random.ints(1, 1, 8).findFirst().getAsInt()).mapToObj(j -> {
OrderItem orderItem = new OrderItem();
Product product = products.get(random.nextInt(products.size()));
orderItem.setProductId(product.getId());
orderItem.setProductName(product.getName());
orderItem.setProductPrice(product.getPrice());
orderItem.setProductQuantity(random.ints(1, 1, 5).findFirst().getAsInt());
return orderItem;
}).collect(toList()));
order.setTotalPrice(order.getOrderItemList().stream().mapToDouble(item -> item.getProductPrice() * item.getProductQuantity()).sum());
Customer customer = customers.get(random.nextInt(customers.size()));
order.setCustomerId(customer.getId());
order.setCustomerName(customer.getName());
return order;
}).collect(toList());
}
}