The problem question is, add a close method to the Auction class. This should iterate over the collection of lots and print out details of all the lots. You can use either a for each loop or a while loop. Any lot that has had at least one bid for it is considered to be sold. For lots that have been sold, the details should include the name of the successful bidder, and the value of the winning bid. For lots that have not been sold print a message that indicates this fact.

By the way, the IDE I'm using is Blue J.

I'm not coming up with any compiler errors. When i click on the close(), I'm not getting a bid, instead i'm getting the memory location of the object.

Another thing, i can't come up with an counter to stop the loop. The counter i want to use all the bids. I tried using couple of different counters: for(int i = 0; i < bids; i++),(int i = 0; i < 10; i++),
for(int i = 0; temp; i++),for(int i = 0; lots.get(0); i++), but i came up with no success, so i just stuck to this for(int i = 0; i < 10; i++)

import java.util.ArrayList;
import java.util.*;

/**
 * added a close() 11-3-09
 * added getUnsold() 11-3-09
 * declared an arrayList unSoldLot 11-3-09
 */
public class Auction
{
    // The list of Lots in this auction.
    private ArrayList<Lot> lots;
    // The number that will be given to the next lot entered
    // into this auction.
    private int nextLotNumber;
    //list of unSoldLot in this auction
    private ArrayList<Lot> unSoldLot;

    /**
     * Create a new auction.
     */
    public Auction()
    {
        lots = new ArrayList<Lot>();
        nextLotNumber = 1;
    }
    
     //ex.4.28 closed() 11/3/09 
    public void close()
    {
        //create loop to iterate over Lot objects
        //this class uses Lot objects

        Lot temp = lots.get(0);             //need to use this statement in a loop
        Bid bids = temp.getHighestBid();    //do an if statement to see if highest bid is null and print a message
        Person person = bids.getBidder();
        person.getName();
        
        //New ArrayList name "lotName"
        Iterator<Lot> lotName = lots.iterator();
        
        //for loop
        for(int i = 0; i < 10; i++)
        {
            if(bids != null)  [B] //the bids are showing the memory location, instead of a bid.[/B]
            {
                System.out.println("Lot sold at" + bids + "the name of the bider is " +  person.getName());
            }
            else
            {
                System.out.println("Lot not sold");
            }   
        }
        //while loop
//         int i = 0;
//         while( i < lots.get(0))
//         {
//             if (bids != null)
//             {
//                 System.out.println("Lot sold at" + bids + "the name of the bider is " + person.getName());
//             }
//             else
//             {
//                 System.out.println("Lot not sold");
//             } 
//         }
    }
    
    //ex.4.29 getUnsold() 11-4-09
    public ArrayList<Lot> getUnsold()
    {   
        //Lot object that stores lots.get(0)
        Lot lot = lots.get(0);
       // Iterator<Lot> unSoldLot = lots.iterator();
       for(int i = 0; i < 20; i++)
       {
            if (lot == null)
            {
                unSoldLot = lots;
                System.out.println("Here are the lots that aren't sold " + unSoldLot);
            }
            i++;
        }
        return unSoldLot;
    }

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

    /**
     * Show the full list of lots in this auction.
     */
    public void showLots()
    {
        for(Lot lot : lots) {
            System.out.println(lot.toString());
        }
    }
    
    /**
     * 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(int lotNumber, Person bidder, long value)
    {
        Lot selectedLot = getLot(lotNumber);
        if(selectedLot != null) {
            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.
                Bid highestBid = selectedLot.getHighestBid();
                System.out.println("Lot number: " + lotNumber +
                                   " already has a bid of: " +
                                   highestBid.getValue());
            }
        }
    }

    /**
     * Return the lot with the given number. Return null
     * if a lot with this number does not exist.
     * @param lotNumber The number of the lot to return.
     */
    public Lot getLot(int lotNumber)
    {
        if((lotNumber >= 1) && (lotNumber < nextLotNumber)) {
            // The number seems to be reasonable.
            Lot selectedLot = lots.get(lotNumber - 1);
            // Include a confidence check to be sure we have the
            // right lot.
            if(selectedLot.getNumber() != lotNumber) {
                System.out.println("Internal error: Lot number " +
                                   selectedLot.getNumber() +
                                   " was returned instead of " +
                                   lotNumber);
                // Don't return an invalid lot.
                selectedLot = null;
            }
            return selectedLot;
        }
        else {
            System.out.println("Lot number: " + lotNumber +
                               " does not exist.");
            return null;
        }
    }
}

Recommended Answers

All 2 Replies

Since you're storing each lot in an ArrayList, you can use the size() method for your for-loop, and output only the items that have a bid.

Also, not that it matters, but

import java.util.ArrayList;
import java.util.*;

is redundant.

Are you referring like this:

for(int i = 0; i < temp.size(); i++)

or

for(int i = 0; i < lot.size(); i++)

Thanks

Since you're storing each lot in an ArrayList, you can use the size() method for your for-loop, and output only the items that have a bid.

Also, not that it matters, but

import java.util.ArrayList;
import java.util.*;

is redundant.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.