progress on ex Z4

This commit is contained in:
2025-01-13 20:47:24 +01:00
parent afc2fbfa8a
commit 886bf5a1cd
4 changed files with 112 additions and 41 deletions

View File

@@ -1,30 +1,53 @@
package exercises.ex_z4;
import exercises.Utils;
import java.util.Random;
import java.util.ArrayList;
import java.util.List;
import static exercises.ex_z4.AuctionState.BIDDING;
import static exercises.ex_z4.AuctionState.FINISHED;
public class Bidder implements Runnable {
private static int nextId = 0;
private final String name;
private final int id;
private final List<Item> items = new ArrayList<>();
private Double lastBid = null;
private final Auctioneer auctioneer;
public Bidder(String name) {
public Bidder(String name, Auctioneer auctioneer) {
this.name = name;
this.id = nextId++;
this.auctioneer = auctioneer;
}
@Override
public void run() {
while(true){
Random rand = new Random();
double addon = rand.nextDouble(500);
lastBid += addon;
while (true) {
AuctionState state = auctioneer.getAuctionState();
if (state == BIDDING) {
Random rand = new Random();
double addon = rand.nextDouble(500);
double base = auctioneer.getMaximumBid().amount();
lastBid = base + addon;
auctioneer.placeBid(new Bid(this, lastBid));
try {
Utils.randomSleep(100, 5000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
} else if (state == FINISHED) {
break;
}
}
}
public void addItem(Item item){
items.add(item);
}
public String getName() {
return name;
}