Quantcast
Channel: CSharp Forum Latest Questions
Viewing all articles
Browse latest Browse all 32058

Making exceptions to handle duplicates within a LinkedList

$
0
0
Hey, Im having some issues creating my own exeptions when adding items into my LinkedList. First i ask the users for an itemId and description etc. What i want to do is test whether the item id has been used before then throw a custom exception to tell the user to try another item id. Here is my add method so far -

public void addItem(StockItem aItem)
    {
       itemList.add(aItem);           
    }

Im guessing you would need a iterator to search through the list to check for duplicates. I have a delete method that works by implimenting the iterator class. Could i modify this code to do this? -

 public void deleteItem(String ItemId)
    {
       
        Iterator<StockItem> iterator = itemList.iterator();
       
        while (iterator.hasNext())
        {
           
            if (iterator.next().getItemID().equals(ItemId))
            {
                iterator.remove();
            }
        }

the issue is that the user input variable for the itemId is handled within another class. I need help!
Thanks.




Viewing all articles
Browse latest Browse all 32058

Trending Articles