-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
AuctionListing.java
68 lines (58 loc) · 1.88 KB
/
AuctionListing.java
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
package onlineauctionsystem;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
public class AuctionListing {
private final String id;
private final String itemName;
private final String description;
private final double startingPrice;
private final long duration;
private final User seller;
private AuctionStatus status;
private double currentHighestBid;
private User currentHighestBidder;
private final List<Bid> bids;
public AuctionListing(String id, String itemName, String description, double startingPrice, long duration, User seller) {
this.id = id;
this.itemName = itemName;
this.description = description;
this.startingPrice = startingPrice;
this.duration = duration;
this.seller = seller;
this.status = AuctionStatus.ACTIVE;
this.currentHighestBid = startingPrice;
this.currentHighestBidder = null;
this.bids = new CopyOnWriteArrayList<>();
}
public synchronized void placeBid(Bid bid) {
if (status == AuctionStatus.ACTIVE && bid.getAmount() > currentHighestBid) {
currentHighestBid = bid.getAmount();
currentHighestBidder = bid.getBidder();
bids.add(bid);
notifyObservers();
}
}
public synchronized void closeAuction() {
if (status == AuctionStatus.ACTIVE) {
status = AuctionStatus.CLOSED;
notifyObservers();
}
}
// Getters and setters
public String getId() {
return id;
}
public String getItemName() {
return itemName;
}
public String getDescription() {
return description;
}
public long getDuration() {
return duration;
}
private void notifyObservers() {
// Notify observers (bidders) about the updated highest bid or auction closure
// ...
}
}