-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStockMarket.java
More file actions
64 lines (52 loc) · 1.5 KB
/
Copy pathStockMarket.java
File metadata and controls
64 lines (52 loc) · 1.5 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
import java.util.PriorityQueue;
import java.util.Comparator;
import java.util.Objects;
class Stock {
private String name;
private double price;
public Stock(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
@Override
public boolean equals(Object o) { // раньше сравнивались ссылки
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Stock stock = (Stock) o;
return Double.compare(stock.price, price) == 0 &&
Objects.equals(name, stock.name);
}
@Override
public String toString() {
return "Stock [name=" + name + ", price=" + price + "]";
}
}
interface StockMarket {
void add(Stock stock);
void remove(Stock stock);
Stock mostValuableStock();
}
class StockMarketImpl implements StockMarket {
private PriorityQueue<Stock> stockQueue;
public StockMarketImpl() {
stockQueue = new PriorityQueue<>(Comparator.comparingDouble(Stock::getPrice).reversed());
}
@Override
public void add(Stock stock) {
stockQueue.add(stock);
}
@Override
public void remove(Stock stock) {
stockQueue.remove(stock);
}
@Override
public Stock mostValuableStock() {
return stockQueue.peek();
}
}