2013-03-05 55 views
-1

我需要删除指定编号的批次。它似乎每次都返回null,或者没有找到很多响应。这是怎么回事?删除指定编号的批次

问题可以在别处而不是此代码? 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; 
    } 
} 

} 
+0

作为一个侧面说明,你似乎是试图推出自己的地图(可能带有链接列表状结构? ?!)。如果这不是学校作业的一部分,您可能只想使用Java提供的地图之一。 – Aurand 2013-03-05 22:03:41

+0

你通过什么方法?这是你的逻辑 - 你决定何时返回null。您传入的号码是否符合您要编码到您的方法中的所有标准以被删除? – ninnemannk 2013-03-05 22:04:44

+0

'lots'变量的类型是什么? – rgettman 2013-03-05 22:05:31

回答

1

我会推荐使用HashMap或任何其他哈希表根据您的需要执行。 像HashMap<Lot> lots= new HashMap<Lot>();

存储所有Lot变量地图,然后简单地套用

lot.contains(numberYouWantToCheck);