Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Book Store Assignment.No errors but it doesn't print the name of the author.

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Book Store Assignment.No errors but it doesn't print the name of the author.

    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
    Last edited by howardderamus1111; May 2nd, 2014 at 07:25 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Book Store Assignment.No errors but it doesn't print the name of the author.

    What does the user enter to test the program?

    What are the program's responses that are wrong?
    For example:
    user enters: 1
    program responds: House << wrong, response should be horse

    Add println statements that prints out what the JOptionMessage methods show so the output can be copied and pasted here.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Book Store Assignment.No errors but it doesn't print the name of the author.

    Like I mentioned above, if I add a book in the inventory, it gets added but when I search for the exact book it doesn't show in the dialopg box. The dialog box is blank.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Book Store Assignment.No errors but it doesn't print the name of the author.

    What does the user enter to see the problem? Please post EXACTLY what the user would enter, line by line.

    What is the content of the computer's message that is wrong.
    Add println statements that prints out what the JOptionMessage methods show so the output can be copied and pasted here.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Book Store Assignment.No errors but it doesn't print the name of the author.

    The user enters option 1 to add book. He enters the name of the book as testing. The name of the author as Tester. Price as 2, Number in stocks 5 Category of book as fiction. This add the new book. But when I go back to the main menu and search for inventroy number (option 4) or anything else the dialog box doesn't show anything. Its just blank.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Book Store Assignment.No errors but it doesn't print the name of the author.

    Please post the responses one to a line without any comments so I can copy the full text into my testing program.
    For example the first two lines:
    1
    Tester

    Also copy and paste here the response from the program that shows the error.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Re: Simple inventory System. Sometimes it doesn't update my added book.
    By beautygboelo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 4th, 2013, 06:34 AM
  2. Code doesn't work despite being from book
    By Math2 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: June 16th, 2013, 08:24 PM
  3. I need help starting my pseudocode. My book doesn't explain it well at all.
    By Bubbabingo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 4th, 2012, 08:37 PM
  4. Simple inventory System. Sometimes it doesn't update my added book.
    By JustinK in forum What's Wrong With My Code?
    Replies: 9
    Last Post: August 1st, 2011, 10:50 AM
  5. Can someone please tell me why my code doesn't print out anything?
    By deeerek in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 6th, 2010, 08:35 AM

Tags for this Thread