Home > database >  How to remove a value stored in an array list
How to remove a value stored in an array list

Time:11-13

So I have some code here and I want to search for the book using the id that it has and then remove however many from the amount.

ArrayList = new ArrayList();

Book b1 = new Book(110, 5);

110 is the id and 5 being the amount, how could I search the ArrayList for the id and then remove 2 books or so from the amount.

CodePudding user response:

So I have some code here .... What code? It would be a really good idea if you would post that code (and not an image of it if at all possible). It helps to clarify a lot of different things and it help members here to determine where you may be having a specific problem. All we can really do here is guess. In any case.....

Let's assume the Book class is something like this:

public class Book {
    
    int bookID;
    String bookTitle = "Unknown";
    String bookAuthor = "Unknown";
    int bookQuantity;

    
    // Constructor #1
    public Book() {  }
    
    // Constructor #2
    public Book(int bookID, int bookQuantity) {
        this.bookID = bookID;
        this.bookQuantity = bookQuantity;
    }
    
    // Constructor #3
    public Book(int bookID, String bookTitle, String bookAuthor, int bookQuantity) {
        this.bookID = bookID;
        this.bookTitle = bookTitle;
        this.bookAuthor = bookAuthor;
        this.bookQuantity = bookQuantity;
    }

    public int getBookID() {
        return bookID;
    }

    public void setBookID(int bookID) {
        this.bookID = bookID;
    }

    public String getBookTitle() {
        return bookTitle;
    }

    public void setBookTitle(String bookTitle) {
        this.bookTitle = bookTitle;
    }

    public String getBookAuthor() {
        return bookAuthor;
    }

    public void setBookAuthor(String bookAuthor) {
        this.bookAuthor = bookAuthor;
    }

    public int getBookQuantity() {
        return bookQuantity;
    }

    public void setBookQuantity(int bookQuantity) {
        this.bookQuantity = bookQuantity;
    }

    @Override
    public String toString() {
        return new StringBuilder(String.valueOf(bookID)).append(", ").append(bookTitle).append(", ")
                .append(bookAuthor).append(", ").append(String.valueOf(bookQuantity)).toString();
    }
}

Then somewhere in another class you have code perhaps something like this...

// An ArrayList to hold instances of Book
    java.util.ArrayList<Book> books = new java.util.ArrayList<>();

    // Add Books to ArryList
    books.add(new Book(110, 5));
    books.add(new Book(111, 2));
    books.add(new Book(112, 7));
    books.add(new Book(113, 3));
    
    // Display the current Books available
    System.out.println("Current Book Inventory:");
    System.out.println("=======================");
    for (Book b : books) {
        System.out.println(b.toString());
    }
    System.out.println();
    
    // REMOVAL
    int bookIDtoRemoveBooksFrom = 112; // Book ID to remove Books from
    int numberOfBooksToRemove = 3;     // The number of books to remove
    
    
    
    // Remove the desired quantity from desired Book instance.
    Book bookInstanceWhereBooksWereRemoved = null;
    for (Book b : books) {
        if (b.bookID == bookIDtoRemoveBooksFrom) {
            bookInstanceWhereBooksWereRemoved = b;
            int currentBookInventory = b.getBookQuantity();
            System.out.println("Current quantity of Book ID: "   bookIDtoRemoveBooksFrom 
                                  " is --> "   currentBookInventory);
            System.out.println();
            // Are there enough books of this ID to remove?
            if (currentBookInventory < numberOfBooksToRemove) {
                // No...
                System.out.println("There are only "   currentBookInventory   
                        " books with the ID of "   bookIDtoRemoveBooksFrom   
                        " in inventroy to remove the desired amount of (" 
                          numberOfBooksToRemove   ") books!");
                System.out.println();
            }
            else {
                // Yes...
                b.setBookQuantity(currentBookInventory - numberOfBooksToRemove);
            }
            break; // Done - Break out of loop.
        }
    }
    
    // Display the current Books available AFTER the removal.
    System.out.println("NEW Book Inventory:");
    System.out.println("===================");
    for (Book b : books) {
        System.out.println(b.toString());
    }
    System.out.println();
    System.out.println("New quantity of Book ID: " 
                                  bookIDtoRemoveBooksFrom 
                                  " is --> " 
                                  bookInstanceWhereBooksWereRemoved.getBookQuantity());
  • Related