CS-161 / Lab #5: Auction System (Object Collection)

General

At the top of the every document that you create (word processing files or source files) include:

/**

* Description of the class or document.

*

* @author YOUR NAME

* @version CS161 Lab #, mm/dd/yyyy(replace with the last edit date)

*/

Submit in this lab via Moodle using the “Lab #” link. Your assignment must be uploaded by midnight of the due date. Moodle will automatically close the link at that time. It is strongly highly recommend you do not wait until the very last minute to submit your work.

Concepts

This lab introduces you to the concept of “collections” of objects and introduces iterative (looping) syntax in Java. Additionally, it will further your experience with problem solving and programming logic sequencing.

Background

Read and review Chapter number 4 from the text. I STRONGLY recommend doing as many of the exercises on the notebook example as possible. This will give you the proper background on using collections in Java.

Also, pay particular attention to the ideas on separation of “Business Logic” from “User Interface or Input/Output”. The key idea is that a method should do one or the other, but not both. The “logic” processing of an application should be done in methods that contain NO UI or INPUT/OUTPUT. Other methods should be used to format data and output it or to gather data. These methods should NOT HAVE ANY processing logic.

In the world of Auctions, a LOT is considered the item that is for sale. Each “lot” that is for sale might be a single item, or it might be a group of items, but a bidder must purchase the entire lot, it cannot be broken up by the bidder. An auction will thus consist of several “lots” that are available for purchase.

In the code, pay close attention to the identifiers “Lot”, “lots”, and “lot”. They each refer to something different;it does make sense if you pay attention:

“Lot” is the class name for the definition of lot instances.

“lot” is an instance of the class Lot.

“lots” is a container (array list) of Lot instances

There are a couple of lines of code where all three of these appear:

for(Lot lot : lots) {

}

In the above example we are using a for-each loop that declares a reference variable “lot” that is an instance of the class “Lot”. In each pass through the loop, the “lot” instance will be assigned to one of the instances in the container “lots”. The for-each loop will continue until every instance in the container has been worked with. We often call this “visiting” an instance, and any code that visits every instance of a container is known as a “traversal”.

Assignment Instructions

Part 1 (no coding, just using the Auction project to get familiar with its functionality)

For the first part of the lab you will be simply using BlueJ and the AUCTION project provided by the author to create instances of an AUCTION object, several LOT objects, several BID objects, and several PERSON objects. This part of the lab will orient you to how the basics of this code works and how to put all of the pieces together.

STEPS:

Do these using the BlueJ interactive move to create and use Java object instances.

1)Create 2 or 3 “Person” objects that will represent “Bidders”. Give each object a sensible name.

2)Now, create ONE Auction object.

3)Using the enterLot() method of the AUCTION object, create several lots (i.e. for different items that are for sale).

4)Execute the showLots method of the AUCTION object and look at the results.

5)Using the makeABid() method of the AUCTION object, add several bids for each of the lots that you created. These will need to use the reference ID of the person objects that you created in step #1. Do not use the same person for all of the bids.

6)Execute the showLots method of the AUCTION object and look at the results.

7)Open an INSPECTOR on your AUCTION object. Then “Inspect” the “lots” field. Next “Inspect” the elementData field. Then “inspect” one of array object (that have a pointer). Next, inspect the highestBid field, and then finally, inspect the bidder field.

8)DO A PRINT SCREEN SHOWING ALL OF THE INSPECTORS THAT YOU HAVE OPENED UP. And, more importantly, study the objects and inspectors so that you get the picture of how the objects are organized and related in this code.

9)TURN IN a MS-Word, RTF, or Open Document file containing your screen shot for this part of the lab.

Part 2 (coding)

1)Exercise 4.48

Add a close method to the Auction class. This should iterate over the collection of lots and print out details of all the lots. Use a for-each loop. Any lot that has had at least one bid for it is considered to be sold, so what you are looking for is Lot objects whose highestBid field is not null. Use a local variable inside the loop to store the value returned from call to the getHighestBid method, and then test that variable for the null value.

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.

This should have a signature identical to:

public void closeAuction()

2)Exercise 4.49

Add a getUnsold method to the Auction class with the following signature:

publicArrayList<LotgetUnsold()

This method should iterate over the “lots” container, storing each unsold lot in anArrayList local variable. What you are looking for is Lot objects whose highestBid field is null. At the end of the method, it should return the local ArrayList that should now contain all of the unsold lots.

3)Test your methods

When you write your "getUnsold" method, remember that this method is a business logic method; it should not have any I/O (no print statements). It's only job is to return a new ArrayList of the unsold items. It is not easy to inspect this and to verify that you have done this correctly. The best way is to write some additional testing logic to verify your code. I have done this for you.

If you cut and paste code out of Adobe, you will need to first paste it into a simple text editor like NotePad, then copy the code from NotePad into BlueJ; otherwise, Adobe Reader sometimes changes some of the characters which will result in compiler errors.

Here is the code youare to add along with the 2 methods that you have written ( getUnsold() and closeAuction() ):

public void testGetUnsold( )

{

ArrayList<Lot>unsoldLots;

unsoldLots = getUnsold( );

System.out.println( "*** UnSold Items Report ***");

for ( Lot currentLot : unsoldLots) {

System.out.println("UnSold Item: " +

currentLot.getDescription( ));

}

}

public void testAuction( )

{

//Setup test data: people

Person bidderOne = new Person( "Sara");

Person bidderTwo = new Person( "Chuck");

//Setup test data: Lot items

enterLot( "Android Phones");

enterLot( "iPhones");

enterLot( "Razr");

enterLot( "NetBooks");

enterLot( "Laptops");

enterLot( "Generic Desktops");

//Setup test data: bids

makeABid( 1, bidderOne, 200);

makeABid( 3, bidderTwo, 50);

makeABid( 1, bidderOne, 225);

makeABid( 4, bidderTwo, 150);

//Call the methods to test them

closeAuction( ); //if this is the name of your close method

testGetUnsold( );

}

(continued on next page)

4)Run the Test

Once you have added these methods to the Auction class, and have finished writing the "closeAuction" and "getUnsold" methods, create an instance of the Auction class and execute the “testAuction( )” method.

PUT A WINDOW CAPTURE (ALT-PRINTSCREEN) OF YOUR OUTPUT INTO THE LAB DOCUMENT THAT YOU CREATED FOR THE PART 1 OF THIS LAB.

You should get output that looks almost identical to the following:

The bid for lot number 1 was successful.

The bid for lot number 3 was successful.

The bid for lot number 1 was successful.

The bid for lot number 4 was successful.

*** Closed Auction report ***

Sold Item: Android Phones Highest Bid: $225 Bidder: Sara

UnSold Item: iPhones

Sold Item: Razr Highest Bid: $50 Bidder: Chuck

Sold Item: NetBooks Highest Bid: $150 Bidder: Chuck

UnSold Item: Laptops

UnSold Item: Generic Desktops

*** UnSold Items Report ***

UnSold Item: iPhones

UnSold Item: Laptops

UnSold Item: Generic Desktops

Turn in your entire BlueJ Project that you created/modified. Starting with Lab #3, it is required that you run the code formatter (BlueJ Auto-layout) over ALL of the classes before you Zip and submit your lab. Any code you add or modify should also be commented when appropriate (look at the example code in the book and study the comment style).

1)ZIP the entire project folder, DO NOT delete any of the files. You will upload the single ZIP compressed file. Please ONLY submit Zip formats. Other compression formats require the grader to spend more time opening various tools to handle other formats, so only ZIP will be accepted.

2)Submit any required document as a separate file.

3)Log in to Moodle.

4)Click the Lab # link in the week # activity block in the class Moodle site, then browse to your document and upload the document you created.

5)If you need to submit an updated version, click on the submit link and you will see the file you previously uploaded, click the edit these files button. On the right of the file name you will see this icon . Click on the icon, delete your previous submission, and then upload your updated version.

Western Oregon University Page 1 of 4