-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCommand.java
More file actions
67 lines (52 loc) · 1011 Bytes
/
Command.java
File metadata and controls
67 lines (52 loc) · 1011 Bytes
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
interface Order {
void execute();
}
public class Stock {
private String name = "ABC";
private int quantity = 10;
public void buy() {
System.out.println("Buy stock");
}
public void sell() {
System.out.println("Sell stock");
}
}
public class BuyStock implements Order {
Stock stock;
public BuyStock(Stock stock) {
this.stock = stock;
}
public void execute() {
stock.buy();
}
}
public class SellStock implements Order {
Stock stock;
public SellStock(Stock stock) {
this.stock = stock;
}
public void execute() {
stock.sell();
}
}
class Broker {
List<Order> orderList = new ArrayList<Order>();
public void takeOrder(Order order) {
orderList.add(order);
}
public void executeOrders() {
for (Order order : orderList) {
order.execute();
}
orderList.clear();
}
}
class Test {
public static void main(String[] args) {
Broker b = new Broker();
Stock s = new Stock();
b.takeOrder(new BuyStock(s));
b.takeOrder(new SellStock(s));
b.executeOrders();
}
}