Hello folks. I usally don't ask for help but right now I am in desperate need. I am finished with this assignment but it is not working as it is supposed to. For example when I add a new book, and search for it it doesn't find the author's name or his book. Here is the assignment.
Barbara is opening a book store, and she needs an effective, efficient, object-oriented program to keep track of her inventory. Barbara would like to maintain the following information about the books she sells: title, author, price, number in stock, and category. She categorizes her books into fiction, nonfiction and children’s books, because she has a separate room in her store for each category. Each book also needs an inventory number, and Barbara would like these numbers to start with 1000. She also needs to be able to keep track of the total number of books in inventory.
Barbara often helps her customers find books, and the program needs search capabilities to do this. Keeping an accurate inventory count of each book in stock is critical; both for accounting purposes and so she will know when to reorder. Barbara expects to have at most 2,000 differet books in her store.
Barbara is very organized, and would like a nice menu, from which she can make choices that will enable her to do the following:
Add a new book to inventory
This adds a new Book object (title, author, etc) into inventory
It does not increase the number in stock of an existing book
Search by title
Search by author
Search by inventory number
Search by category
Print out all info for all books
This is the only output that should be on the command line
Please hard code the following book info into your program, so Barbara has some test data avaiable to test your program:
title: Crime and Punishment; author: Dostoevsky; price: 39.95; number in stock: 5; category: fiction
title: Brothers Karamazov; author: Dostoevsky; price 34.50; number in stock: 3; category: fiction
title: Java; author: Liang; price: 59.29; number in stock: 12; category: non-fiction
title: Java, author: Horstmann; price: 14.33; number in stock: 4; category: non-fiction
title: The Lion, the Witch and the Wardrobe; author: Lewis; price: 12.50; number in stock: 3; category: childrens
Here is my code so far. The first one is the "Blueprint" and the second one is the main.
public class Book { private String title; private String author; private double price; private int stockNumber; private String category; private int inventoryNumber =1000; private static int numberOfBooks = 0; public Book(String theTitle, String theAuthor, double thePrice, int theStockNumber, String theCategory) { title = theTitle; author = theAuthor; price = thePrice; stockNumber = theStockNumber; category = theCategory; inventoryNumber = inventoryNumber + numberOfBooks; numberOfBooks++; } public String getTitle() {return title;} public void setTitle(String theTitle) {title = theTitle;} public String getAuthor(){return author;} public void setAuthor(String theAuthor){author = theAuthor;} public double getPrice(){return price;} public void setPrice(int thePrice){price = thePrice;} public int getStockNumber(){return stockNumber;} public void setStockNumber(int theStockNumber){stockNumber= theStockNumber;} public String getCategory(){return category;} public void setCategory(String theCategory){category = theCategory;} public int getInventoryNumber(){return inventoryNumber;} public static int getNumberOfBooks(){return numberOfBooks;} public String toString () { StringBuffer sb = new StringBuffer(); sb.append("\n"); sb.append("\nTitle: " + title); sb.append("\nAuthor: " + author); sb.append("\nPrice: $" + price); sb.append("\nNumber in Stock: " + stockNumber); sb.append("\nInventory number: " + inventoryNumber); sb.append("\n"); return (new String(sb)); } // end toString method public static String findTitle(String titleToSearchFor, Book[] books) { String message = ""; for (int i = 0; i < getNumberOfBooks(); i++) { if (titleToSearchFor.equalsIgnoreCase(books[i].getTitle())) { message = message + books[i].toString(); } // end if } // end for return message; } // end findTitle method public static String findAuthor(String authorToSearchFor, Book[] books1) { String message = ""; for (int i = 0; i < getNumberOfBooks(); i++) { if (authorToSearchFor.equalsIgnoreCase(books1[i].getAuthor())) { message = message + books1[i].toString(); } // end if } // end for return message; }// end findAuthor public static String findInventoryNumber(int inventoryNumberToSearchFor, Book[] books2) { String message = ""; for (int i = 0; i < getNumberOfBooks(); i++) { if (inventoryNumberToSearchFor == (books2[i].getInventoryNumber())) { message = message + books2[i].toString(); } // end if } // end for return message; } // end findInventoryNumber public static String findCategory(String categoryToSearchFor, Book[] books3) { String message = ""; for (int i = 0; i < getNumberOfBooks(); i++) { if (categoryToSearchFor.equalsIgnoreCase(books3[i].getCategory())) { message = message + books3[i].toString(); } // end if } // end for return message; } // end findCategory method } // end class
Here is my main so far
import javax.swing.JOptionPane; class BookInventory { public static void main(String[] args) { String message =""; Book[] bookInventory = new Book[2000]; bookInventory[0] = new Book ("Crime and Punishment","Dostoevsky",39.95, 5,"fiction"); bookInventory[1] = new Book ("Brothers Karamazov","Dostoevsky",34.50,3,"fiction"); bookInventory[2] = new Book ("Java", "Horstmann",14.33,4,"non-fiction"); bookInventory[3] = new Book ("The Lion, the Witch and the Wardrobe","Lewis",12.50,3,"childrens"); boolean keepGoing = true; do { int choice = menuOptions(); switch (choice) { case 1: addBook(bookInventory);break; case 2: String searchTitle = JOptionPane.showInputDialog("Please enter the title you are looking for:"); message = Book.findTitle(searchTitle, bookInventory ); JOptionPane.showMessageDialog(null, message);break; case 3: String searchAuthor = JOptionPane.showInputDialog("Please enter the author you are looking for"); message = Book.findAuthor(searchAuthor, bookInventory ); JOptionPane.showMessageDialog(null, message);break; case 4: int searchInventoryNumber = Integer.parseInt(JOptionPane.showInputDialog("Please enter the inventory that you are looking for:")); message = Book.findInventoryNumber(searchInventoryNumber, bookInventory ); JOptionPane.showMessageDialog(null, message);break; case 5: String searchCategory = JOptionPane.showInputDialog("Please enter the category you are looking for"); message = Book.findCategory(searchCategory, bookInventory ); JOptionPane.showMessageDialog(null, message);break; case 6: booksInfo(bookInventory);break; case 7: keepGoing = false;break; default: JOptionPane.showMessageDialog(null, "Your choice is invalid. Choose a valid choice.");break; } // end of switch } while (keepGoing); JOptionPane.showMessageDialog ( null, "bye \n"); } // end main public static void addBook(Book[] newBooks) { String title = JOptionPane.showInputDialog("enter title."); String author = JOptionPane.showInputDialog("enter title author."); double price = Double.parseDouble(JOptionPane.showInputDialog(null, "enter price: ")); int stockNumber = Integer.parseInt(JOptionPane.showInputDialog(null, "enter stock number: ")); String category =""; int choice; do { choice = categoryChoice(); switch (choice) { case 1: category = "fiction";break; case 2: category = "non-fiction";break; case 3: category = "children"; break; default: JOptionPane.showMessageDialog(null, "Your choice is invalid. Choose a valid choice.");break; } // end of switch }while (choice != 1 && choice != 2 && choice !=3 ); // end loop newBooks[Book.getNumberOfBooks()] = new Book(title, author, price, stockNumber, category); }// end addBook method public static void booksInfo(Book[] theBooks) { String message = ""; for(int i = 0; i < Book.getNumberOfBooks(); i++) { message = message + theBooks[i].toString(); } System.out.println (message + "\nTotal number of Books: " + Book.getNumberOfBooks() + "\n"); }// end booksInfomethod public static int menuOptions() { String message; int choice; message = "\n1. Add new book \n" + "2. Search title\n"; message += "3. Search author\n" + "4. Search inventory number\n" ; message += "5. Search category\n" + "6. Print inventory\n" + "7. Exit the program\n\n" + "enter your choice.\n"; choice = Integer.parseInt(JOptionPane.showInputDialog(null,message)); return choice; } // end menuOptions method public static int categoryChoice() { String message; int choice; message = "\n1. Fiction\n" + "2. Non-fiction\n" + "3. Children\n\n" + "Enter your choice.\n"; choice = Integer.parseInt(JOptionPane.showInputDialog(null,message)); return choice; } // end categoryChoice method } //end of class
Any help that I can get would really be helpful. Thank you