Hi guys,

Can you help me out with this project. My head is fried with this now. I am getting confused when you have to call on functions outside the class and variables from outside the class.

What i need to get done is the following:

a. Add a close method to the auction class. This should iterate over the collection of lots and print out details of all the lots. You may use a “for each” or “while” loop and any lot that has had at least one bid is considered sold. 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 indicating this fact.


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

public ArrayList<Lot> getUnsold()

This method should iterate over the lots field, storing unsold lots in a new ArrayList local variable. At the end of the method, return the list of unsold lots.

c. Add a removeLot method to the Auction class with the following signature:


/**Remove the lot with the given lot number.
*@param number the number of the lot to be removed.
*@return The Lot with the given number, or null of there is no such lot.
*/
public Lot removeLot(int number) {


……..//method body goes here….

}

This method should not assume that a lot with a given number is stored at any particular location within the collection.


Im not looking for the answers, im looking for someone to point me in the right direction. All four classes are below. Please give me a hand as im getting lost here.

--------------------------------------------------------------------------------------

import java.util.ArrayList;
import java.util.Iterator;

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

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

--------------------------------------------------------------------------------------

/**
 * A class to model an item (or set of items) in an
 * auction: a lot.
 * 
 * @author David J. Barnes and Michael Kolling.
 * @version 2008.03.30
 */
public class Lot
{
    // A unique identifying number.
    private final int number;
    // A description of the lot.
    private String description;
    // The current highest bid for this lot.
    private Bid highestBid;

    /**
     * Construct a Lot, setting its number and description.
     * @param number The lot number.
     * @param description A description of this lot.
     */
    public Lot(int number, String description)
    {
        this.number = number;
        this.description = description;
    }

    /**
     * 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;
        }
    }
    
    /**
     * @return A string representation of this lot's details.
     */
    public String toString()
    {
        String details = number + ": " + description;
        if(highestBid != null) {
            details += "    Bid: " + 
                       highestBid.getValue();
        }
        else {
            details += "    (No bid)";
        }
        return details;
    }

    /**
     * @return The lot's number.
     */
    public int getNumber()
    {
        return number;
    }

    /**
     * @return The lot's description.
     */
    public String getDescription()
    {
        return description;
    }

    /**
     * @return The highest bid for this lot.
     *         This could be null if there is
     *         no current bid.
     */
    public Bid getHighestBid()
    {
        return highestBid;
    }
}

--------------------------------------------------------------------------------------

/**
 * A class that models an auction bid.
 * It contains a reference to the Person bidding and the amount bid.
 * 
 * @author David J. Barnes and Michael Kolling.
 * @version 2008.03.30
 */
public class Bid
{
    // The person making the bid.
    private final Person bidder;
    // The value of the bid. This could be a large number so
    // the long type has been used.
    private final long value;

    /**
     * Create a bid.
     * @param bidder Who is bidding for the lot.
     * @param value The value of the bid.
     */
    public Bid(Person bidder, long value)
    {
        this.bidder = bidder;
        this.value = value;
    }

    /**
     * @return The bidder.
     */
    public Person getBidder()
    {
        return bidder;
    }

    /**
     * @return The value of the bid.
     */
    public long getValue()
    {
        return value;
    }
}

--------------------------------------------------------------------------------------

/**
 * Maintain details of someone who participates in an auction.
 * @author David J. Barnes and Michael Kolling.
 * @version 2008.03.30
 */
public class Person
{
    // The name of this person.
    private final String name;

    /**
     * Create a new person with the given name.
     * @param name The person's name.
     */
    public Person(String name)
    {
        this.name = name;
    }

    /**
     * @return The person's name.
     */
    public String getName()
    {
        return name;
    }
}

--------------------------------------------------------------------------------------

You do know how to create methods, don't you?
The answer is easy everything has been given to you and most of the suggestions here will repeat what the assignment says to do:

a. Add a close method to the auction class. This should iterate over the collection of lots and print out details of all the lots. You may use a “for each” or “while” loop and any lot that has had at least one bid is considered sold. 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 indicating this fact.

Just create method that does what it says. Check the java.util.ArrayList class. Iterate the lots (ArrayList) (for loop) and print them. The description also says when a lot is considered sold, and what to do with it.
Now if you don't know how to call methods or a write a for-loop then that is up to you to find out.

This is the code i used to remove an element from the array but it is telling me it is missing a return statement.

This is from part c of what i have to do.

public Lot removeLot(int number)
    {
        if(number == 0)
        {
            System.out.println("Invalid Entry: You need to enter a valid number");
        }
        
        else if (number<= lots.size())
        
        {
        lots.remove(number);
        System.out.println("Item Number " +number+ " has been deleted.");
        }
        

}

This is the start of part a. This lists out all the elements in the array called lots. But i dont know how i can go about checking if an item has been bid on and how to set it to sold.

public void close()
    {
        for (Lot lot : lots)
        {
          
           System.out.println(lot);
       
        }   
        
       
        
    }

Well if it is says that is missing a return statement, then it's missing a return a statement. The problem is exactly that, it is missing a 'return'. You are supposed to know how to create methods:

public [B]Lot[/B] removeLot(int number)

In the way you have created it, you must return a Lot object. So either return one or change the method.

Call the methods of the Lot object. Based on what they return determine whether it is sold or not. Use if statements. If you look at the toString method of the Lot object you will understand which method to use and what is the rule. It is also described at your requirements.

I have returned type null. I know i am to return an object of type Lot but i am only learning and i dont understand quite what that means. If was public int removeLot(int number), i could do the following ? - return number; ? Please be gentle with me, as this is the first time i am calling and passing stuff from different classes. When i run this code, it does remove the lot, but i get a popup telling me it is returning null. I dont think this is right though. Can you help ?

public Lot removeLot(int number)
    {
        if(number == 0)
        {
            System.out.println("Invalid Entry: You need to enter a valid number");
        }
        
        else if (number<= lots.size())
        
        {
            
        lots.remove(number-1);
        System.out.println("Item Number " +number+ " has been deleted.");

        }
        
       return null;
}

I didn't notice the requirements. And you have already been given the signature with this description:
return The Lot with the given number, or null of there is no such lot.
Also the requirements say:
param number the number of the lot to be removed.

Meaning the parameter will not be the position of the lot in the list. When you do this remove(number) you remove the lot which is at the position number.
But what you have been asked is to remove the lot with the given number:

public class Lot
{
    // A unique identifying number.
    private final int number;
..
}

Meaning that in your method, you will need to loop the list. Take each Lot and compare its number with the one given. If they are equal you have found it and return immediately. If the loop finishes without finding anything, then such lot doesn't exists so outside the loop return null;

loop the list {
  take the Lot
  if (Lot.number == number) {
     // Lot found and you must return it AFTER YOU REMOVE IT using the remove method you have been using.
    // only now remember that the parameter of the remove method is the index of the Lot in the list. And now you know the position of the Lot
  }
}
// if you haven't returned yet then just return null because you haven't found anything
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.