-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path32.DesignAuctionSystem.java
More file actions
130 lines (105 loc) · 4.08 KB
/
32.DesignAuctionSystem.java
File metadata and controls
130 lines (105 loc) · 4.08 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.locks.*;
public class AuctionSystem {
// =============================================================
// Auction Class
// =============================================================
static class Auction {
private String id;
private String itemName;
private int startPrice;
private int highestBid;
private String highestBidder;
private long startTime;
private long endTime;
private List<String> bidHistory;
private final ReentrantLock lock;
public Auction(String id, String itemName, int startPrice, int durationSeconds) {
this.id = id;
this.itemName = itemName;
this.startPrice = startPrice;
this.highestBid = startPrice;
this.highestBidder = null;
this.startTime = System.currentTimeMillis();
this.endTime = this.startTime + durationSeconds * 1000L;
this.bidHistory = new ArrayList<>();
this.lock = new ReentrantLock();
}
public boolean isActive() {
return System.currentTimeMillis() < endTime;
}
public String placeBid(String user, int amount) {
lock.lock();
try {
if (!isActive()) {
return "Auction already ended.";
}
if (amount <= highestBid) {
return "Bid too low.";
}
highestBid = amount;
highestBidder = user;
bidHistory.add(user + " -> " + amount);
return "Bid accepted: " + user + " -> " + amount;
} finally {
lock.unlock();
}
}
public String getHighestBidder() {
return highestBidder;
}
public int getHighestBid() {
return highestBid;
}
}
// =============================================================
// AuctionManager (Singleton)
// =============================================================
static class AuctionManager {
private static AuctionManager instance;
private Map<String, Auction> auctions;
private AuctionManager() {
auctions = new ConcurrentHashMap<>();
}
public static synchronized AuctionManager getInstance() {
if (instance == null) {
instance = new AuctionManager();
}
return instance;
}
public Auction createAuction(String id, String item, int startPrice, int durationSeconds) {
Auction a = new Auction(id, item, startPrice, durationSeconds);
auctions.put(id, a);
return a;
}
public Auction getAuction(String id) {
return auctions.get(id);
}
public String placeBid(String id, String user, int amount) {
Auction a = auctions.get(id);
if (a == null) return "Auction not found.";
return a.placeBid(user, amount);
}
public String endAuction(String id) {
Auction a = auctions.get(id);
if (a == null) return "Auction not found.";
if (a.getHighestBidder() == null) {
return "No bids placed. Auction ended with no winner.";
}
return "Winner: " + a.getHighestBidder() + " with bid " + a.getHighestBid();
}
}
// =============================================================
// Demo Run
// =============================================================
public static void main(String[] args) throws InterruptedException {
AuctionManager manager = AuctionManager.getInstance();
manager.createAuction("A1", "Laptop", 500, 5);
System.out.println(manager.placeBid("A1", "Alice", 550));
System.out.println(manager.placeBid("A1", "Bob", 600));
Thread.sleep(6000);
System.out.println(manager.placeBid("A1", "Charlie", 700));
System.out.println(manager.endAuction("A1"));
}
}