-1

指定された番号のロットを削除する必要があります。毎回nullで返すか、lot not foundで返すようです。調子はどう?

このコード以外に問題がある可能性はありますか? removeLot は修正が必要なメソッドです。ここで少し混乱しています。

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;

private ArrayList<Lot> Unsold;

private int  lotNumber;


/**
 * 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.
 * Adds lot to ArrayList
 */
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());
    }
}

/**
 * Make a bid for a lot.
 * A message is printed indicating whether the bid is
 * successful or not.
 * 
 * @param lotNumber The lot being bid for.
 * @param bidder The person bidding for the lot.
 * @param value  The value of the bid.
 * If successful bid it removes lot from ArrayList
 */
public void makeABid(int lotNumber, Person bidder, long value)
{
    Lot selectedLot = getLot(lotNumber);
    if(selectedLot != null) {
        Bid bid = new Bid(bidder, value);
        boolean successful = selectedLot.bidFor(bid);
        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.
 * No longer determines the lot number according to index number.
 */
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;
    }
}


    /**
 * Look for closed lots. Return highest bid and bidder name if sold.
 * If lot not sold print not sold.
 */
public void close(int lotNumber, String description)
{
    for(Lot lot : lots) 
    {
        System.out.println(lotNumber + description); //print lot number and description.
        Bid highestBid = lot.getHighestBid(); //get the highest bid for the lot.
        if (highestBid != null) 
        {
            String name = highestBid.getBidder().getName(); 
            System.out.println(name + " " + highestBid.getValue()); //print bidder and highest bid value
        }     
        else
        {
            System.out.println("Not sold"); //if not sold print "Not sold"
        }
    }
}

       /**
 * Returns the list of unsold lots.
 * If sold print sold statement.
 */
public ArrayList<Lot> getUnsold()
{  
    ArrayList<Lot> unsold = new ArrayList<Lot>();
    for(Lot lot : lots) 
    {
        Bid highestBid = lot.getHighestBid();
        lotNumber = lot.getNumber();
      if (highestBid != null) 
        {

        System.out.println("Lot number " + lotNumber + " is sold"); //retuern "Sold" is highestBid
        }
       else
        {
        System.out.println(lotNumber); //print bidder and highest bid value
        unsold.add(lot); // you are missing this
        }
    }
    return unsold;
}

    /**
 * Remove the lot with the given lot number.
 * @param number The number of the lot removed.
 * @return The Lot with the given number, or null if there is no such lot.
 */
public Lot romoveLot(int number)
{
    if((number >= 1) && (number < nextLotNumber)) {
        // The number seems to be reasonable.
        Lot selectedLot = lots.get(number);
        // Include a confidence check to be sure we have the
        // right lot.
        if(selectedLot.getNumber() != number) {
            System.out.println("Internal error: Lot number " +
                               selectedLot.getNumber() +
                               " was returned instead of " +
                               number);
            // Don't return an invalid lot.
            selectedLot = null;
        }
        else {
             lots.remove(number);
            }
            return selectedLot;

    }
    else {
        System.out.println("Lot number: " + number +
                           " does not exist.");
        return null;
    } 
 }

}
4

1 に答える 1

1

HashMap必要に応じて、またはその他のハッシュ テーブルの実装を使用することをお勧めします。何かのようなものHashMap<Lot> lots= new HashMap<Lot>();

すべてのLot変数をマップに保存し、単純に適用します

lot.contains(numberYouWantToCheck);
于 2013-03-05T22:05:47.160 に答える