This repository was archived by the owner on Dec 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsumer.java
More file actions
59 lines (49 loc) · 1.49 KB
/
Consumer.java
File metadata and controls
59 lines (49 loc) · 1.49 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
package com.nhnacademy.gaeun.assignment01;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
public class Consumer implements Runnable {
private Store store;
private String name;
private Random random;
public static int count = 0;
public Consumer(String name, Store store) {
this.store = store;
this.name = name + ++count;
}
public String getName() {
return name;
}
@Override
public void run() {
try {
store.enter(this);
sellRandomTime();
store.exit(this);
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
}
private void sellRandomTime() {
try {
Thread.sleep(ThreadLocalRandom.current().nextInt(1000, 10000));
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
System.out.println("Thread Interrupted");
}
pickFoods();
}
public void pickFoods() {
int productNum = randomProductNum();
int amount = randomAmount();
Product product = store.getFoodStand().getFoodsList().get(productNum);
store.getFoodStand().sell(product, amount);
}
private int randomAmount() {
random = new Random();
return random.nextInt(10) + 1;
}
private int randomProductNum() {
random = new Random();
return random.nextInt(store.getFoodStand().getFoodsList().size());
}
}