2010-11-30 64 views
0

嘿,所有。我被要求創建一個使用迭代器來打印'lots'細節的方法。我可以創建一個迭代器來打印所有的細節,但是,對於沒有被購買的任何批次,一條消息應該打印出這個事實,我不確定如何添加該代碼。我正在關注的公共無效關閉方法。這是我迄今爲止所擁有的。非常感謝幫助。迭代器和打印細節

public class Auction{ 

    // The list of Lots in this auction. 
    private final ArrayList<Lot> lots; 
    // The number that will be given to the next lot entered 
    // into this auction. 
    private int nextLotNumber; 

    /** 
    * Create a new auction. 
    */ 
    public Auction(){ 
     lots = new ArrayList<Lot>(); 
     nextLotNumber = 1; 
    } 

    /** 
    * Enter a new lot into the auction. 
    * 
    * @param description 
    *   A description of the lot. 
    */ 
    public void enterLot(final String description){ 
     lots.add(new Lot(nextLotNumber, description)); 
     nextLotNumber++; 
    } 

    /** 
    * Show the full list of lots in this auction. 
    */ 
    public void showLots(){ 
     for(final Lot lot : lots){ 
      System.out.println(lot.toString()); 
     } 
    } 

    public void close(){ 
     final Iterator<Lot> it = lots.iterator(); 
     while(it.hasNext()){ 

     } 
    } 

    /** 
    * Bid for a lot. A message indicating whether the bid is 
    * successful or not is printed. 
    * 
    * @param number 
    *   The lot number being bid for. 
    * @param bidder 
    *   The person bidding for the lot. 
    * @param value 
    *   The value of the bid. 
    */ 
    public void bidFor(final int lotNumber, 
     final Person bidder, 
     final long value){ 
     final Lot selectedLot = getLot(lotNumber); 
     if(selectedLot != null){ 
      final boolean successful = 
       selectedLot.bidFor(new Bid(bidder, value)); 
      if(successful){ 
       System.out.println("The bid for lot number " + lotNumber 
        + " was successful."); 
      } else{ 
       // Report which bid is higher. 
       final Bid highestBid = selectedLot.getHighestBid(); 
       System.out.println("Lot number: " + lotNumber 
        + " already has a bid of: " + highestBid.getValue()); 
      } 
     } 
    } 

} 
+1

這是一個功課題嗎? – Wyzard 2010-11-30 12:37:56

+0

你能提供更多關於完整`Lot` API的細節嗎? – 2010-11-30 12:38:26

回答

0

我會加入你想要的地塊打印到Lot.toString()我建議你close()方法應該關閉()每個很多,而且很多應該打印任何東西,這就需要將信息打印。

2

Lot類是否有一個標誌指示它是否被購買?如果是這樣的話

for(final Lot lot : lots){ 
    if (!lot.purchased) { 
    System.out.println("not bought"); 
    } 
} 

順便說一句 - 我注意到你正在關閉方法中使用pre-each樣式迭代器。沒有理由這樣做,因爲您可以訪問for-each中的單個Lot實例。

0

將屬性添加到稱爲「獲勝者」或類似人的類型的Lot類中。

在bidFor方法中,如果(成功)塊,設置贏家屬性:通過大量的迭代時

selectedLot.setWinner(bidder); 

然後,如果獲勝者屬性爲空,打印您的信息,即很多不是招沒有買過。

或者你可以使用:

if (lot.getHighestBid() == null) 

取決於很多課是如何實現的,要知道辛苦沒有看到很多課。