-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathShoppingCartSystem.java
More file actions
59 lines (50 loc) · 1.67 KB
/
Copy pathShoppingCartSystem.java
File metadata and controls
59 lines (50 loc) · 1.67 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
class ShoppingCart {
private int itemCount = 0; // 购物车中的商品数量
// 向购物车添加商品
public synchronized void addItem() {
itemCount++;
System.out.println("Item added. Total items in cart: " + itemCount);
}
// 从购物车移除商品
public synchronized void removeItem() {
if (itemCount > 0) {
itemCount--;
System.out.println("Item removed. Total items in cart: " + itemCount);
} else {
System.out.println("No items to remove.");
}
}
// 显示购物车状态
public synchronized void displayCart() {
System.out.println("Shopping Cart contains " + itemCount + " items.");
}
}
class Shopper implements Runnable {
private ShoppingCart cart;
private int addOperations;
private int removeOperations;
public Shopper(ShoppingCart cart, int addOperations, int removeOperations) {
this.cart = cart;
this.addOperations = addOperations;
this.removeOperations = removeOperations;
}
@Override
public void run() {
for (int i = 0; i < addOperations; i++) {
cart.addItem();
}
for (int i = 0; i < removeOperations; i++) {
cart.removeItem();
}
}
}
public class ShoppingCartSystem {
public static void main(String[] args) {
ShoppingCart cart = new ShoppingCart();
// 创建多个购物者线程
Thread shopper1 = new Thread(new Shopper(cart, 5, 3));
Thread shopper2 = new Thread(new Shopper(cart, 3, 2));
shopper1.start();
shopper2.start();
}
}