//This method will search the books array for a given book //If the book is found by title or isbn, and the quantity > 0, and it's for sale, it returns the array index of the book // otherwise, it returns a -1 public static int searchByTitleOrISBN(Book[] b) { int i = 0; // print a message "Enter the Title or ISBN of the book: " System.out.println("Enter the Title or ISBN of the book: "); String input = scan.next(); // use a while loop to search through the array books using a counter and as long as no match is found while (i < b.length) { // if there's a match for the title or a match for the isbn and the book quantity > 0 and the book is for sale if ((b.getTitle().equals(title) || b.getISBN().equals(isbn)) & b.getQty() > 0 && b.isForSale()) { // print a message "We have that book in stock and it is available for purchase!!" System.out.println("We have that book in stock and it is available for purchase!!"); // print column headers " Title ISBN Quantity Price For Sale" System.out.println(" Title ISBN Quantity Price For Sale"); // print the information for the book (call method toString()) System.out.println(b[i].toString()); // indicate a match was found } // otherwise, else // increase counter i i++; // if a match was found, if (b.getTitle().equals(title) || b.getISBN().equals(isbn)) // return the array index of the book return i; // otherwise, else { // print a message "Sorry, we do not have that book in stock or it not for sale." System.out.println("Sorry, we do not have that book in stock or it not for sale."); } } // return -1 (code for book not found, or not in stock, or not for sale) return -1; }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)