AP Computer Science --- Haas --- Auction

The objective of this project is to help you understand passing objects as parameters.



/** <<< CODE NOT COMPLETE >>>
 *  Item can be Bid on by a bidder
 *  Keeps track of highest two bids and number of bids
 */
public class Item
{
  private int numberOfBids = 0;
  private int itemNumber;
  private double price;  // current amount of highest bid
  private Bidder highBidder = null;  // highest bidder
  private Bidder backBidder = null;  // second highest bidder (in case high
                                     // bidder backs out of deal)
  
  /**
   * construct an item with an initial price
   */
  Item(int num, double p) {
      itemNumber = num;
      price = p;
  }
    
  /**
   * <<< complete the code >>>
   *   1) Increments numbers of bids
   *   2) If bid is greater than current price
   *      than highBidder becomes current bidder,
   *      and backBidder is previous highBidder.
   */
  public void Bid(Bidder b, double bid)
  {

         // <<< complete the code >>>


  }
  
  /**
   *  <<< complete the code >>>
   * returns formatted string with information about item:
   * item Number, price, number of bids, names of highest
   * and second highest bidders
   */
  public String toString() {

         // <<< complete the code >>>

  }
}


/** <<< CODE NOT COMPLETE >>>
 *  Class Bidder 
 *  a Bidder can bid on an item 
 */
public class Bidder
{
   private String name;
   private String userID;

   /**
   *  Construct a Bidder
   */
   public Bidder(String nm, String ID)  {
      name = nm; 
      userID = ID;
   }
   
   /** <<< complete the code >>>
    * write the placeBid method which calls the Bid 
    * method in the Item class 
    */
   public void placeBid(Item i, double amt) {

         // <<< complete the code >>>

   }
   
   
   /**
    * write getter methods needed by the 
    * toString method in class Item
    */
 
}



/*************************************************************
             <<< THIS CODE IS COMPLETE >>>

 Simulates an auction with one item and three bidders.

 <<< The following is the resulting output >>>

 Item number: 456746, Current Price: $25.0, Number Of Bids: 0
 High Bidder: NO HIGH BIDDER, Back up Bidder: NO BACK UP BIDDER

 Item number: 456746, Current Price: $26.0, Number Of Bids: 1
 High Bidder: Jim, Back up Bidder: NO BACK UP BIDDER

 Item number: 456746, Current Price: $28.0, Number Of Bids: 2
 High Bidder: Sue, Back up Bidder: Jim

 Item number: 456746, Current Price: $33.0, Number Of Bids: 3
 High Bidder: Sam, Back up Bidder: Sue

 Item number: 456746, Current Price: $33.0, Number Of Bids: 4
 High Bidder: Sam, Back up Bidder: Sue

 Item number: 456746, Current Price: $34.0, Number Of Bids: 5
 High Bidder: Jim, Back up Bidder: Sam
 
***************************************************************/
public class Auction
{
   public static void main(String[] args)
   {
      Item i1 = new Item(456746, 25.00); // create new item

      Bidder b1 = new Bidder("Jim", "jimbo90210"); // create bidder 1
      Bidder b2 = new Bidder("Sue", "SJM5491");  // create bidder 2
      Bidder b3 = new Bidder("Sam", "IAmSam001");  // create bidder 3
      
      System.out.println(i1);

      b1.placeBid(i1,26.00);
      System.out.println(i1);

      b2.placeBid(i1,28.00);
      System.out.println(i1);
      
      b3.placeBid(i1,33.00);
      System.out.println(i1);
      
      b2.placeBid(i1,30.00);
      System.out.println(i1);
      
      b1.placeBid(i1,34.00);
      System.out.println(i1);
   }
}