开发者

Iterator and printing details

Hey all. I've been asked to create a method that uses an iterator to print details of 'lots'. I'm able to create an iterator that prints all of the details, however, for any lots that haven't been bought, a message should print out this fact and I'm unsure how I can add that code in. It is the public void close method I'm focusing on. This is what I have so far. Help is greatly appreciated.

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.开发者_如何学JAVAprintln("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());
            }
        }
    }

}


Does the Lot class have a flag indicating if it's purchased or not? If so then

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

BTW - I noticed you're using the pre for-each style iterator in the close method. There's no reason to do this since you'll have access to individual Lot instances in the for-each also.


I would add the information you want the Lot to print to the Lot.toString() I would suggest your close() method should close() each lot, and the lot should print anything which needs to be printed.


Add an attribute to the Lot class of type Person called "winner" or something similar.

In the bidFor method, in the if (successful) block, set the winner attribute:

selectedLot.setWinner(bidder);

Then when iterating through the lots, if the winner attribute is null, print your message that the lot hasn't been bought.

Or you could use:

if (lot.getHighestBid() == null)

depending on how the Lot class is implemented, hard to know without seeing the Lot class.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜