/**
* A simple model of an auction.
*
* @author (your name)
* @version (a version number or a date)
*/
import java.util.ArrayList;
import java.util.Iterator;
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;
/**
* 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(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 succesful 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 a list of the unsold lots.
*/
public void close()
{
System.out.println("The auction is closed.");
for(Lot lot : lots) {
System.out.println(lot.getNumber() + ": " +lot.getDescription());
Bid bid = lot.getHighestBid();
if (bid==null){
System.out.println("(No Bids for this lot.)");
}
else {
System.out.println( "sold to " +
bid.getBidder().getName() + " for "
+ bid.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 invalid lot.
selectedLot = null;
}
return selectedLot;
}
else {
System.out.println("lot number: " + lotNumber + " does not exist.");
return null;
}
}
}