亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

查找具有正確參數的對象的代碼出錯

查找具有正確參數的對象的代碼出錯

幕布斯6054654 2023-04-26 14:26:03
我有拍賣任務的代碼。有 4 個類:Item,Bid,Auction,Person。 Item包含:Item 的名稱, Item 的描述, Item 的minPrice , LinkedList of allBids 拍賣包含:LinkedList of allItems,LinkedList of bidings Bid包含:Bid 的價格, Person 類的 對象person包含:姓名投標人。所以在這次 sho brifing 之后我想總結一下我的問題。如果您有其他問題,我會提供我的類圖。 https://drive.google.com/open?id=19mjayMIWFRNygvzP2xIGEWVzZcKNXIZDAuction類中有一個addBid(String itemName ,String nameOfBidder,long price) 方法,應該從投標人LinkedList中找到投標人(如果它不存在則創建它)然后根據 Item 的名稱找到正確的,然后使用Item類中的addBid方法添加新的投標項對象。我的代碼中有一個錯誤,當我試圖根據它的itemName找出一個 Item 時,如果不存在具有這樣名稱的項目對象,它應該返回我NoSuchElementException 。但是每次我都沒有通過這個檢查,其實我不明白為什么。我試圖通過使用不同類型的循環(例如 foreach)來解決我的問題。但是在幾天內無法解決它。這是我從 Auction 類的 addBid 方法中獲取的方法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);                    }                }            }        }    }
查看完整描述

1 回答

?
喵喵時光機

TA貢獻1846條經驗 獲得超7個贊

以下情況看起來不正確:


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);

? ? }

}


查看完整回答
反對 回復 2023-04-26
  • 1 回答
  • 0 關注
  • 119 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號