java - 使用正确参数查找对象的代码出错
问题描述
我有拍卖任务的代码。有 4 个类:Item、Bid、Auction、Person。 项目包含:项目的名称,项目的描述,项目的最低价格,所有投标的链接列表 拍卖包含:所有项目的链接列表,投标者的链接列表 投标包含:投标的价格, Person类的 对象人员人员包含:名称投标人。
因此,在此 sho briving 之后,我想总结一下我的问题。如果您有其他类型的问题,我会提供我的班级图表。 https://drive.google.com/open?id=19mjayMIWFRNygvzP2xIGEWVzZcKNXIZD
Auction类中有一个addBid(String itemName ,String nameOfBidder,long price)
方法,应该从投标人LinkedList 中找到投标人(如果它不存在,则创建它)然后根据项目的名称找到正确的,然后使用项目类中的addBid方法添加新的投标项目对象。
我的代码中有一个错误,当我试图根据itemName找出一个项目时,如果不存在具有该名称的项目对象,它应该返回NoSuchElementException 。但每次我没有通过这个检查,其实我不明白为什么。
我试图通过使用不同类型的循环(例如 foreach)来解决我的问题。但是在几天内无法解决它。
这是我在 addBid 方法的 Auction 类中的方法
public void addBid(String ItemName, String nameOfBidder, long price) {
if(ItemName==null||nameOfBidder==null){
throw new NullPointerException("Name of the bidder cannot be null");
}
if(ItemName==""||nameOfBidder==""||price==0||price<0){
throw new IllegalArgumentException("Name of the bidder cannot be empty");
}
for(Person p:bidders) {
if (bidders.contains(p.getName()==nameOfBidder)) {
for (Item i:allItems ) {
if(!(allItems.contains(i.getName()))){
throw new NoSuchElementException("There is no such Item in the Auction");
}
if(allItems.contains(i.getName()==ItemName)){
i.addBid(p,price);
}
}
}
else {
Person person = new Person(nameOfBidder);
bidders.add(person);
for (Item i:allItems ) {
if(!(allItems.contains(i.getName()))){
throw new NoSuchElementException("There is no such Item in the Auction");
}
if(allItems.contains(i.getName()==ItemName)){
i.addBid(person,price);
}
}
}
}
}
还有我的 Junit 测试,我最后一次检查失败(NoSuchElementException)
public void testAddBidIllegalArgument() {
a.registerItem(new Item("Clock", "Ancient clock", 1000));
try {
a.addBid("", "Max", 5);
fail("Auction.addBid() should throw an IllegalArgumentException if the itemName argument is empty!");
} catch (IllegalArgumentException e) {
}
try {
a.addBid("Clock", "", 5);
fail("Auction.addBid() should throw an IllegalArgumentException if the nameOfBidder argument is empty!");
} catch (IllegalArgumentException e) {
}
try {
a.addBid("Clock", "Max", 0);
fail("Auction.addBid() should throw an IllegalArgumentException if the price argument is zero!");
} catch (IllegalArgumentException e) {
}
try {
a.addBid("Clock", "Max", -1);
fail("Auction.addBid() should throw an IllegalArgumentException if the price argument is negative!");
} catch (IllegalArgumentException e) {
}
try {
a.addBid("New", "Max", 5);
fail("Auction.addBid() should throw a NoSuchElementException if no item in the auction has the given name!");
} catch (NoSuchElementException e) {
}
}
请帮我解决我的错误!并帮助我通过最后的检查!
解决方案
以下情况看起来不正确:
for(Person p:bidders) {
// bidders holds Person objects and you are checking for boolean. p.getName() == nameOfBidder will evaluate to true. Perhaps you want to check for name equality first and then contains.
if (bidders.contains(p.getName()==nameOfBidder)) {
}
}
for (Item i:allItems ) {
// allItems holds Item objects and you are checking by name string
if(!(allItems.contains(i.getName()))){
}
}
此外,可以简化初始空值和检查条件。
给你,大大简化的代码:
public void addBid(String itemName, String nameOfBidder, double price) {
if (itemName == null || nameOfBidder == null) {
throw new NullPointerException("Name of the bidder cannot be null");
}
if (itemName.equals("") || nameOfBidder.equals("") || price <= 0) {
throw new IllegalArgumentException("Name of the bidder cannot be empty");
}
Optional<Person> person = bidders.stream().filter(e -> e.getName().equals(nameOfBidder)).findAny();
Optional<Item> item = items.stream().filter(e -> e.getName().equals(itemName)).findAny();
if (person.isPresent()) {
checkItemAndAddBid(item, person.get(), price);
} else {
Person newPerson = new Person(nameOfBidder);
bidders.add(newPerson);
System.out.println("Creating a new bidder: "+newPerson.getName());
checkItemAndAddBid(item, newPerson, price);
}
}
public void checkItemAndAddBid(Optional<Item> item, Person person, double price) {
if (!item.isPresent()) {
throw new NoSuchElementException("There is no such Item in the Auction");
} else {
item.get().addBid(person, price);
}
}
完整的运行示例代码可用:在 github
推荐阅读
- python - 在 python 和 appium 中,为什么不能使用 send_keys() 到输入框
- python - 如何在python中自动安排和发送python生成报告的电子邮件
- c++ - 如何使用自制软件、cMake 和 cLion 将库添加到 C++ 项目
- mysql - 如何从 Whm 永久禁用 only_full_group_by
- c# - ?? 和 || 操作员计时
- c - 难以理解沿函数指针的 typedef
- curl - 使用 wget 或 curl 通过外部调用请求页面时,页面中的站点链接以默认语言呈现,尽管语言“L”标志
- javascript - 请求后如何将页面保存为 pdf?
- ruby-on-rails - 尝试使用 postgresql 创建新应用程序时出错(rails new myapp -d postgresql)
- c# - 如果两个接口具有相同的方法名称,那么我们如何在 C# 中实现多重继承