2010-11-30 6 views
0

こんにちは。私は、イテレータを使って 'lots'の詳細を印刷するメソッドを作成するように求められてきました。しかし、すべての詳細を印刷するイテレータを作成することはできますが、購入されていないロットの場合は、このメッセージが表示され、そのコードをどのように追加できるかはわかりません。 public void closeメソッド私は焦点を当てています。これは私がこれまで持っていたものです。ヘルプは非常に高く評価されます。イテレータと印刷の詳細

私はあなたがロットLot.toString(に印刷したい情報を追加します
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

+0

完全な 'Lot' APIについてもっと詳しく説明できますか? –

答えて

0

)私はあなたのclose()メソッドが)(各ロットを閉じなければならない、と多くがあることが必要である何かを印刷する必要があります示唆しています印刷されます。

2

ロットクラスには購入済みかどうかを示すフラグがありますか?もしそうならば、

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

私はあなたがcloseメソッドでpre-eachスタイルのイテレータを使用していることに気付きました。 for-eachの個々のLotインスタンスにもアクセスできるので、これを行う理由はありません。

+0

は、フィールドではなく、メソッドでなければならないので、if(!lot.purchased()){... – karakuricoder

+0

を読んでください。 – karakuricoder

+0

あなたはコメントする代わりにあなたの投稿を編集することができる場合は、 –

0

「勝者」などと呼ばれるタイプのPersonのLotクラスに属性を追加します。勝者属性がnullの場合、多くを反復するとき、多くのhasnあなたのメッセージを印刷し、その後

selectedLot.setWinner(bidder); 

:bidFor方法で

は、もし(成功した)ブロックで、勝者の属性を設定します購入されたことはありません。

それとも、使用することができます

if (lot.getHighestBid() == null) 

ロットクラスはロットクラスを見ることなく知ることが、ハードの実装方法によって異なります。

関連する問題