Need help with ArrayList loop
Hey everyone, I've been stuck on this problem for a long time. I can't seem to get my last method to return anything. It's supposed to add all of the unsold lots of an auction (if the highestBid == null) to an ArrayList, then at the end of the method it's supposed to return that list. My teacher told me to look closely at the header for the last for-each loop, but I still can't figure out what's wrong with it. Any help is much appreciated.
Code :
import java.util.ArrayList;
/**
* A simple model of an auction.
* The auction maintains a list of lots of arbitrary length.
*
* @author David J. Barnes and Michael Kolling.
* @version 2008.03.30
*/
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;
//Edit
//Matthew Slagle, June 3, 2012
//The list of unsold lots in this auction.
private ArrayList<Lot> getUnsold;
/**
* 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 successful 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 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 an invalid lot.
selectedLot = null;
}
return selectedLot;
}
else {
System.out.println("Lot number: " + lotNumber +
" does not exist.");
return null;
}
}
/**
* Ex. 4.28
* Matthew Slagle
* May 28, 2012
* Prints out a list of all lots and their info.
*/
public void close()
{
for(Lot lot : lots)
{
int number = lot.getNumber();
System.out.println("Lot Number: " + number);
String lotName = lot.getDescription();
System.out.println(lotName);
Bid highestBid = lot.getHighestBid();
if (highestBid != null)
{
System.out.println("Winner: " + highestBid.getBidder().getName());
System.out.println("Highest Bid: " + highestBid.getValue());
System.out.println();
}
else
{
System.out.println("This lot has not been sold.");
}
}
}
/**
* Ex 4.29
* Matthew Slagle
* May 28, 2012
* Displays the unsold lots
* If no lots are unsold, it lets the user know.
*/
public ArrayList<Lot> getUnsold()
{
ArrayList<Lot> unsoldLots = new ArrayList<Lot>();
for(Lot lot : unsoldLots)
{
int number = lot.getNumber();
String lotName = lot.getDescription();
Bid highestBid = lot.getHighestBid();
if (highestBid == null)
{
unsoldLots.add(new Lot(number, lotName));
}
nextLotNumber++;
}
System.out.println(unsoldLots);
return unsoldLots;
}
}
Re: Need help with ArrayList loop
Hello mattslagle44!
There are some classes missing so it's difficult to say what's going on with your code but there is a problem with your logic in your getUnsold() method.
Code Java:
ArrayList<Lot> unsoldLots = new ArrayList<Lot>();
for(Lot lot : unsoldLots){
// ...
}
In the above piece of code you create an new ArrayList which is -by default- empty. And then you run through it to do something. Can you see the problem now?
And please edit your post and wrap your code in code tags.
Hope this helps.
Re: Need help with ArrayList loop
I see that now, thanks. I set unsoldLots to equal a new list, looked for values in the empty list, then returned the empty list.
Re: Need help with ArrayList loop
Yep. Instead just use your lots ArrayList rather than creating a new object.