Throwing an exception when item not found – java

I’m creating a remove method that removes an item via lists title(string), seller(string) and price(integer). This method is meant to iterate through each list to find the book item. If the book item is found, then it should be removed from the list (i.e list titleMap). If the book item is not found, I want to throw a BookStoreExeption.

How would I go about throwing an exception in the method? I’ve thought using a while loop but unsure about how to implement it.

If anyone could give me any tips or a solution to the problem, that would be great. Also, feel free to criticize the code, I have a feeling it will not work when I test it. (Beginner)

@Override
public void remove(IBookItem book) throws BookStoreException{


    for(Iterator<BookEntry<String, ArrayList<BookItem>>> it = titleMap.iterator(); it.hasNext();){
        BookEntry<String, ArrayList<BookItem>> bookEntry = (BookEntry<String,ArrayList<BookItem>>)it.next();
        if(bookEntry.getValue().equals(book.getTitle())){
            bookEntry.getValue().remove(book);
            titleMap.remove();
            System.out.println("book Removed (via titleMap): " + book);
        }
    }

    for(Iterator<BookEntry<String, ArrayList<BookItem>>> it = titleMap.iterator(); it.hasNext();){
        BookEntry<String, ArrayList<BookItem>> bookEntry = (BookEntry<String,ArrayList<BookItem>>)it.next();
        if(bookEntry.getValue().equals(book.getSeller())){
            bookEntry.getValue().remove(book);
            sellerMap.remove();
            System.out.println("Book Removed (via sellerMap): " + book);
        }
    }

    for(Iterator<BookEntry<String, ArrayList<BookItem>>> it = titleMap.iterator(); it.hasNext();){
        BookEntry<String, ArrayList<BookItem>> bookEntry = (BookEntry<String,ArrayList<BookItem>>)it.next();
        if(bookEntry.getValue().equals(book.getPrice())){
            bookEntry.getValue().remove(book);
            titleMap.remove();
            System.out.println("Book Removed (via priceMap): " + book);
        }
    }   




}

Answer

Create a boolean variable at the beginning of the method with initial false value. If the item is found one of the loops, set it to true, and at the end of the method check the variable:

@Override
public void remove(IBookItem book) throws BookStoreException{

boolean itemFound = false;

for(Iterator<BookEntry<String, ArrayList<BookItem>>> it = titleMap.iterator(); it.hasNext();){
    BookEntry<String, ArrayList<BookItem>> bookEntry = (BookEntry<String,ArrayList<BookItem>>)it.next();
    if(bookEntry.getValue().equals(book.getTitle())){
        itemFound  = true;
        bookEntry.getValue().remove(book);
        titleMap.remove();
        System.out.println("book Removed (via titleMap): " + book);
    }
}

//here other loops

if (!itemFound){
    throw new BookStoreException("Item not found!");
}}

More tips: 1.use it.remove() instead of titleMap.remove(), it won’t work (if you change the collection, the iterator becomes invalid) 2.correct the collection names in the second and third loop, be careful with copy paste.

Leave a Reply

Your email address will not be published. Required fields are marked *