我正在建立一个电子拍卖系统,我想为他们赢得的拍卖的每位买家列出一份清单。但是,我似乎无法将字符串从一个类传递到另一个类的链表。我浏览了整个网站,了解如何将字符串从一个类传递到另一个类,但是它们都不起作用。
我尝试仅使用.add方法传递它,但它一直在给我不可行的建议。
这是我的拍卖课
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.LinkedList;
import java.util.List;
public class Auction {
private int auctionID;
private boolean blocked = false;
private double startPrice, reservePrice;
private LocalDateTime closeDate;
private Item item;
private double UP, LOW;
private double currentBid;
private List<Bid> bids = new LinkedList<>();
private Status status;
private User seller;
private boolean itemFound;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd MM yyyy");
private double bidAmount;
private User highestBidder;
public Auction(double startPrice, double reservePrice, double currentBid, User highestBidder,
LocalDateTime closeDate, Item item, User seller, Status status) throws Exception {
if (closeDate.isBefore(LocalDateTime.now().plusDays(7))) {
this.startPrice = startPrice;
this.reservePrice = reservePrice;
this.closeDate = closeDate;
this.item = item;
this.highestBidder = highestBidder;
this.currentBid = currentBid;
this.seller = seller;
UP = currentBid * 0.20;
LOW = currentBid * 0.10;
} else {
throw new Exception("CloseDate error: " + closeDate.format(formatter));
}
}
public void auctionVictory() {
if (status == Status.CLOSED && currentBid >= reservePrice) {
User winner = getHighestBidder();
Item winnerItem = getItem();
winnerItem.getItemName(itemName);
winner.victories.add(itemName);
}
}
}
我的物品分类:
public class Item {
private String itemName;
public Item(String itemName, User seller) {
setDescription(itemName);
}
public boolean checkItemSearch(String itemDescription) {
if (itemName.equals(itemDescription)) {
return true;
}
return false;
}
@Override
public String toString() {
return getItemName(itemName);
}
public void setDescription(String itemName) {
this.itemName = itemName;
}
public String getItemName(String itemName) {
return itemName;
}
}
我的买家类别:
public final class Buyer extends User {
public static List<String> victories = new LinkedList<String>();
public void Victory() {
}
public Buyer(String username, String password) {
super(username, password);
}
}
如果当前出价高于底价,我希望方法'auctionVictory'可以使出价最高的用户获得要拍卖的商品。
一旦我获得了商品和买方,我想从Item类调用getItemName方法,并获取该商品的字符串名称。收到此信息后,我想将此字符串添加到Buyer类的“胜利”列表中。
答案 0 :(得分:1)
您的胜利列表是一个静态变量,这意味着它在Buyer类的每个实例之间共享,并且通过实例变量“ winner”访问它是没有意义的。
我假设您希望自己的胜利是特定于用户的,所以您应该删除static修饰符。