document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /**
  2.  * A simple model of an auction.
  3.  *
  4.  * @author (your name)
  5.  * @version (a version number or a date)
  6.  */
  7.  
  8. import java.util.ArrayList;  
  9. import java.util.Iterator;  
  10. public class Auction  
  11. {  
  12.    // The List of Lots in this auction.  
  13.    private ArrayList<Lot> lots;  
  14.    // The number that will be given to the next lot entered  
  15.    // into this auction  
  16.    private int nextLotNumber;  
  17.    /**  
  18.     * Create a new auction  
  19.     */  
  20.    public Auction()  
  21.    {  
  22.      lots = new ArrayList<Lot>();  
  23.      nextLotNumber = 1;  
  24.    }  
  25.    /**  
  26.     * Enter a new lot into the auction.  
  27.     * @param description A description of the lot.  
  28.     */  
  29.    public void enterLot(String description)  
  30.    {  
  31.      lots.add(new Lot(nextLotNumber, description));  
  32.      nextLotNumber++;  
  33.    }  
  34.    /***  
  35.     * Show the full list of lots in this auction.  
  36.     */  
  37.    public void showLots()  
  38.    {  
  39.      for(Lot lot : lots){  
  40.        System.out.println(lot.toString());  
  41.      }  
  42.    }  
  43.    /**  
  44.     * Bid for a lot.  
  45.     * A message indicating whether the bid is succesful or not is printed.  
  46.     * @param number The lot number being bid for.  
  47.     * @param bidder The person bidding for the lot.  
  48.     * param value The value of the bid.  
  49.     */  
  50.    public void bidFor(int lotNumber, Person bidder, long value)  
  51.    {  
  52.      Lot selectedLot = getLot(lotNumber);  
  53.      if(selectedLot!=null){  
  54.        boolean successful = selectedLot.bidFor(new Bid(bidder,value));  
  55.        if (successful) {  
  56.          System.out.println("The bid for lot number " + lotNumber +  
  57.          " was successful.");  
  58.        }  
  59.        else {  
  60.          //Report which bid is higher.  
  61.          Bid highestBid = selectedLot.getHighestBid();  
  62.          System.out.println("Lot number: " + lotNumber +  
  63.          " already has a bid of: " + highestBid.getValue());  
  64.        }  
  65.      }  
  66.    }  
  67.    /**  
  68.     * Return a list of the unsold lots.  
  69.     */  
  70.    public void close()  
  71.    {  
  72.      System.out.println("The auction is closed.");  
  73.      for(Lot lot : lots) {  
  74.        System.out.println(lot.getNumber() + ": " +lot.getDescription());  
  75.        Bid bid = lot.getHighestBid();  
  76.        if (bid==null){  
  77.          System.out.println("(No Bids for this lot.)");  
  78.        }  
  79.        else {  
  80.          System.out.println( "sold to " +  
  81.          bid.getBidder().getName() + " for "  
  82.          + bid.getValue());  
  83.      }  
  84.    }  
  85.  }  
  86.    /**  
  87.     * Return the lot with the given number.  
  88.     * Return null if a lot with this number does not exist.  
  89.     * @param lotNumber The number of the lot to return.  
  90.     */  
  91.     public Lot getLot(int lotNumber)  
  92.     {  
  93.         if((lotNumber >= 1) && (lotNumber < nextLotNumber)) {  
  94.         // The number seems to be reasonable.  
  95.         Lot selectedLot = lots.get(lotNumber - 1);  
  96.         // Include a confidence check to be sure we have the right lot.  
  97.         if(selectedLot.getNumber() != lotNumber) {  
  98.           System.out.println("Internal error: lot number " +  
  99.           selectedLot.getNumber() + " was returned instead of " +  
  100.           lotNumber);  
  101.           // Don\'t return invalid lot.  
  102.           selectedLot = null;  
  103.          }  
  104.          return selectedLot;  
  105.         }  
  106.         else {  
  107.         System.out.println("lot number: " + lotNumber + " does not exist.");  
  108.         return null;  
  109.         }  
  110.      }  
  111. }
  112.  
');