-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuctionItem.java
More file actions
65 lines (62 loc) · 2.1 KB
/
AuctionItem.java
File metadata and controls
65 lines (62 loc) · 2.1 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
public class AuctionItem extends Item{
private int daysLeft;
private double currentBid;
private User highBidder;
public AuctionItem(String name, double listCost, User owner, int daysLeft){
super(name, listCost, owner, true);
currentBid = listCost;
highBidder = null;
this.daysLeft = daysLeft;
}
public int getDaysLeft(){
return daysLeft;
}
public double getCurrentBid(){
return currentBid;
}
public void advanceDay(){
if(this.getForSale() == true && this.daysLeft <= 0){
if (highBidder != null){
this.getOwner().updateBalance(this.currentBid);
highBidder.updateBalance(-this.currentBid);
this.setOwner(highBidder);
highBidder = null;
this.resetAuction(0.0, 0);
this.toggleForSale();
}
}else if (daysLeft > 0){
daysLeft--;
}else{
return;
}
}
public String makeBid(User person, double bid){
if (this.getForSale() == true && bid > currentBid){
highBidder = person;
currentBid = bid;
}
return highBidder.getName();
}
public void resetAuction(double startingBid, int numDaysLeft){
if (highBidder == null){
currentBid = startingBid;
daysLeft = numDaysLeft;
}
}
public String toString(){
if(getForSale() == true){
if (highBidder == null){
if (daysLeft == 1){
return getName() + " ($" + currentBid + ", None, " + daysLeft + " day left)";
}
return getName() + " ($" + currentBid + ", None, " + daysLeft + " days left)";
}
if (daysLeft == 1){
return getName() + " ($" + currentBid + ", " + highBidder.getName() + ", " + daysLeft + " day left)";
}
return getName() + " ($" + currentBid + ", " + highBidder.getName() + ", " + daysLeft + " days left)";
} else{
return super.toString();
}
}
}