2013-03-05 2 views
-1

Мне нужно удалить лот с заданным номером. Кажется, что он возвращается с нулем каждый раз или отвечает не найденным лотом. Что происходит?Удалить лот с заданным номером

Может ли проблема быть в другом месте, а не в этом коде? 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

+0

Что вы передаете методу? Это ваша логика - вы решаете, когда вы возвращаете null. Пропускает ли номер, который вы передаете, все критерии, которые вы кодируете, в свой метод, который нужно удалить? – ninnemannk

+0

Каков тип переменной 'lots'? – rgettman

ответ

1

Я recommed использованием HashMap или любой другой реализации хэш-таблицу, основанную на вашей потребности. что-то вроде HashMap<Lot> lots= new HashMap<Lot>();

Храни все Lot переменных в карту и просто применить

lot.contains(numberYouWantToCheck); 
Смежные вопросы