Home‎ > ‎ICT Programming‎ > ‎Java‎ > ‎

The Auction

The Auction project

This project simulates an online auction. In an auction people make bids on lots (items for sale in an
auction are called lots). Once the auction closes, the person who has made the highest bid gets to buy the lot.

Activity 1

  • Open the auction project in BlueJ
  • Create an instance of the Auction class.
  • Invoke the enterLot method to add a "Chair" lot to the auction.
  • Invoke the showLots method.
  • Create at least three Person objects.
  • Have each person make a bid on the chair lot by invoking the bidFor method. The first person's bid will be successful because there is no previous bid on the lot. Make the second person's bid lower than the first person's bid. This bid will be unsuccessful. Finally, make the this person's bid greater than the first person's bid. This will be a successful bid.

The Lot class

The bidFor method of the Lot class handles bids made on that lot.

 /**
* Attempt to bid for this lot. A successful bid
* must have a value higher than any existing bid.
* @param bid A new bid.
* @return true if successful, false otherwise
*/
public boolean bidFor(Bid bid)
{
    if((highestBid == null) || (bid.getValue() > highestBid.getValue())) {
        // This bid is the best so far.
        highestBid = bid;
        return true;
    }
    else {
        return false;
    }
}

Initially, there will be no bid for the lot. In Java, the reserve word null is used to denote that an object variable has not been assigned an object. So the check to see if highestBid contains a bid is ...

highestBid == null

This condition will be true if highestBid contains no value and false otherwise.

Variable of primitive data types contain the actual value assigned to them. That is the assignment statements ...
int x = 0;
double sum = 5.0;
char initial = 'A';

cause Java to find three available memory cells in RAM and names them x, sum and initial. It also stores the values 0, 5.0 and 'A' in x, sum and initial, respectively.

But object variables like highestBid ...

private Bid highestBid;

store the address in RAM of a Bid object. This address is called a pointer and if it does not point at an object, it is said to have a null pointer.

Can you see what's wrong with the following code?

String str1 = "Computer Science";
String str2 = "Computer " + "Science";
if( str1 == str2 ) {
   return true;
}
return false;
Answer: the condition str1 == str2 checks the addresses of the two String objects stored in str1 and str2 NOT the values stored in those objects. the String class has an equals and a compareTo method to compare String values. The correct code would be ...
String str1 = "Computer Science";
String str2 = "Computer " + "Science";
if( str2.equals(str2) ) {
   return true;
}
return false;

The Auction class

The enterLot method in the Auction class contains the following code:

/**
* 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++;
}

The first statement of this method illustrates a common practice in Java programming: the use of anonymous objects. The code could look like this ...

Lot lot = new Lot(nextLotNumber, description);
lots.add(lot);

but there is no need to create a named object here. The variable lot is local to this method and doesn't exist outside the method.

Use a named object when you need to refer back to that specific object later.


Assignment

  1. Add a close method to the Auction class. This method should display details about all the lots. Any lot with at least one bid will be considered to be sold. For lots that have been sold, the details should include the name of the successful bidder. For lots that have not been sold, print a message to indicating this.

  2. Add a getUnsold method to the Auction class that will return an ArrayList of Lot objects that have unsold lots.

  3. Create a number of lots for an Auction. Display the lots. What is the relationship between the Lot Number and the index of the Lot object in the ArrayList of Lot objects? Rewrite getLot so that it does not rely on a lot with a certain number to have an index value of (lotNumber-1). You may assume that lots are always stored in increasing order of lot number.

  4. Add a removeLot method to the Auction class that accepts an integer lot number (NOT the index of a lot in the ArrayList of lot objects) and then removes the Lot with the corresponding lot number.

Comments