Page 1 of 2 12 LastLast
Results 1 to 20 of 29
  1. #1
    SMHouston is offline Member
    Join Date
    Sep 2009
    Posts
    80
    Rep Power
    0

    Default Book finder , running into a few snags.

    ok, heres the class I created.

    Java Code:
    //class Book
    
    public class Book
    {
    	private int numberOfAuthors; //variable to store the number of authors
    	private int numberOfCopies; //variable to store the number of copies
    	private double bookCost; //variable to store the cost of the book
    	private int isbnNum; //variable to store the ISBN number of the book
    	private String bookTitle; //variable to store the book title
    	private String authorName; //variable to store the books author(s)
    	private String pubName; //variable to store the publishers name
    	
    		
    		//Default constructor to set the cost and number of book copies
    		//Postconditions: numberOfCopies = 0; bookCost = 0; isbnNum = 0; bookTitle = ""; authorName = "";
    		//						numberOfAuthors = 0; pubName = "";
    	public Book()
    	{
    		bookTitle = "";
    		numberOfAuthors = 0;
    		authorName = "";	
    		pubName = "";
    		isbnNum = 0;
    		numberOfCopies = 0;
    		bookCost = 0.0;
    	}
    	
    		//Constructor with parameters to set the cost, number of copies, isbn number, and Title
    		//in the store specified by the user
    		//Postconditions: numberOfCopies = numberOfCoipes;
    		//						bookCost = setCost;
    	public Book(String setTitle, int setNumAuthor, String setAuthor, String setPubName, 
    							int setIsbn, int setNoOfCopies, double setCost)
    	{
    		if(setTitle != "")
    			bookTitle = setTitle;
    		else
    			bookTitle = "";
    			
    		if(setNumAuthor > 0)
    			numberOfAuthors = setNumAuthor;
    		else
    			numberOfAuthors = 0;
    
    		if(setAuthor != "")
    			authorName = setAuthor;
    		else
    			authorName = "";
    			
    		if(setPubName != "")
    			pubName = setPubName;
    		else
    			pubName = "";
    		
    		if(setIsbn > 0)
    			isbnNum = setIsbn;
    		else
    			isbnNum = 0;
    
    		if(setNoOfCopies > 0)
    			numberOfCopies = setNoOfCopies;
    		else
    			numberOfCopies = 0;
    				
    		if(setCost > 0.0)
    			bookCost = setCost;
    		else
    			bookCost = 0.0;		
    	}
    	
    		//Method to show the number of copies in the store
    		//Postcondition: The value of the data member 
    		// 					numberOfCopies is returned
    	public int getCount()
    	{
    		return numberOfCopies;
    	}
    	
    		//Method to show the cost of the book
    		//Postconditions: The value of the data member 
    		//						bookCost is returned
    	public double getBookCost()
    	{
    		return bookCost;
    	}
    	
    		//Method to show the ISBN number of the book
    		//Postconditions: The value of the data member
    		//						isbnNum is returned
    	public int getIsbnNum()
    	{
    		return isbnNum;
    	}
    		
    		//Method to show the number of authors of the book
    		//Postconditions: The value of the data member numberOfAuthors is returned
    	public int getNumAuthor()
    	{
    		return numberOfAuthors;
    	}
    	
    		//Method to show the title of the book
    		//Postconditions: The value of the data member bookTitle is returned
    	public String getBookTitle()
    	{
    		return bookTitle;
    	}
    	
    		//Method to show the author of the book
    		//Postconditions: The value of the data member authorName is returned
    	public String getAuthorName()
    	{
    		return authorName;
    	}
    		//Method to show the publisher name
    		//Postconditions: The value of the data member pubName is returned
    	public String getPubName()
    	{
    		return pubName;
    	}
    	
    		//Method to reduce the number of books by 1
    		//Postcondition: numberOfCopies = numberOfCopies - 1;
    	public void saleMade()
    	{
    		numberOfCopies--;
    	}
    }
    Here is the program I've made to test it

    Java Code:
    //Program Book finder
    
    import java.io.*;
    
    public class BookFinder
    {
    	 static BufferedReader keyboard =
                    new BufferedReader(new InputStreamReader(System.in));
    
        public static void main(String[] args) throws IOException
        {
    		
    		Book book = new Book();
    		Book book1 = new Book("How to Sail", 1, "Shawn Houston", "Coastal Publishing", 
    								232908343, 5, 50.00); //step 1
    		Book book2 = new Book("How to Fish", 1, "Dick Charles", "Frontal Publishing",
    								342345345, 6, 39.99); //step 2
    		Book book3 = new Book("How to JAVA", 1, "Shane Smith", "Publishers Inc.",
    								435354656, 10, 29.99); //step 3
    		
    		
    		int choice; //variable to store choice
    		
    		showSelection();
    		choice = Integer.parseInt(keyboard.readLine());
    		
    		while(choice != 9)
    		{
    			switch(choice)
    			{
    			case 1: sellProduct(book1, book);
    						break;
    			case 2: sellProduct(book2, book);
    						break;
    			case 3: sellProduct(book3, book);
    						break;
    			default: System.out.println("Invalid Selection");
    			}//end switch
    			
    				showSelection();
    				choice = Integer.parseInt(keyboard.readLine());
    		}//end while
    	}//end main
    	
    		public static void showSelection()
    		{
    			System.out.println("WELCOME TO THE BOOK FINDER");
    			System.out.println("To select a book or search, enter ");
    			System.out.println("1 for ");
    			System.out.println("2 for ");
    			System.out.println("3 for ");
    			System.out.println("4 for Search by ISBN");
    			System.out.println("5 for Search by Author");
    			System.out.println("9 to exit");
    		}//end showSelection
    		public static void sellProduct(Book product)
                                                 throws IOException
        {
             int amount;  //variable to hold the amount entered
             int amount2; //variable to hold the extra amount needed
    
             if(product.getCount() > 0)  //if dispenser is not empty
             {
                System.out.println("We have " + product.getCount() + "on hand ");                              
                                   
                amount = Integer.parseInt(keyboard.readLine());
    
                if(amount <= product.getCount())
                {
                   System.out.println("Thank you for your purchase");                           
    				}
                else
                   System.out.println("We do not have that many in stock");
    			 }
             else
                System.out.println("Sorry this item is sold out.");
        }//end sellProduct
    
    											
    }//end sellBook
    These are the errors I'm getting:

    BookFinder.java:69: > expected
    if(amount =< product.getCount())
    ^
    BookFinder.java:69: illegal start of expression
    if(amount =< product.getCount())
    ^
    BookFinder.java:73: 'else' without 'if'
    else
    ^

    OK, what I'm trying to do is make a book store. Where options will come up and someone can pick a book from the list and how many copies they want. It will show how many copies are on hand and then after they pick it will update the number of copies.The program will the update the inventory on hand.

    Theres a few other things I want to do with this but that comes after I can get this part working, like searching for the book by author or ISBN number.

    Nothing seems wrong in the class, theres something def wrong in the main program.
    Last edited by SMHouston; 09-03-2009 at 11:27 PM.

  2. #2
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    5

    Default

    oops, reverse the position:

    Java Code:
    if( val1 <= val2 )
    My Hobby Project: LegacyClone

  3. #3
    SMHouston is offline Member
    Join Date
    Sep 2009
    Posts
    80
    Rep Power
    0

    Default

    Ok, changed the position up on that, good catch think my eyes are fried for the day.

    Now I'm getting:

    BookFinder.java:31: sellProduct(Book) in BookFinder cannot be applied to (Book,Book)
    case 1: sellProduct(book1, book);
    ^
    BookFinder.java:33: sellProduct(Book) in BookFinder cannot be applied to (Book,Book)
    case 2: sellProduct(book2, book);
    ^
    BookFinder.java:35: sellProduct(Book) in BookFinder cannot be applied to (Book,Book)
    case 3: sellProduct(book3, book);
    ^
    3 errors

    Which I think is coming back to this line:

    Java Code:
    public static void sellProduct(Book product)
                                                 throws IOException
    But not sure what to do here.

  4. #4
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    5

    Default

    You have a method that requires one variable (Book) but you are trying to pass two variables (Book, Book)
    My Hobby Project: LegacyClone

  5. #5
    SMHouston is offline Member
    Join Date
    Sep 2009
    Posts
    80
    Rep Power
    0

    Default

    ok, got it straightened out thanks very much.

    Just one last question. In these lines:

    Java Code:
    System.out.println("1 for ");
    System.out.println("2 for ");
    System.out.println("3 for ");
    I've tried to add it so its:

    Java Code:
    System.out.println("1 for " + book1)
    etc.
    from the code:

    Book book = new Book();
    Book book1 = new Book("How to Sail", 1, "Shawn Houston", "Coastal Publishing",
    232908343, 5, 50.00); //step 1
    Book book2 = new Book("How to Fish", 1, "Dick Charles", "Frontal Publishing",
    342345345, 6, 39.99); //step 2
    Book book3 = new Book("How to JAVA", 1, "Shane Smith", "Publishers Inc.",
    435354656, 10, 29.99); //step 3


    But its not working it won't print it out

  6. #6
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    5

    Default

    Well, the thing is it should print out something but probably not what you want; As is, it will print out the hashCode() of the object. What you can do is add a method in Book:

    Java Code:
    public String toString()
    {
       return "Book: " + bookTitle; //add anything else about this object that seem interesting
    }
    when you override this method, which is inherited from Object, it will return your custom description of the object.
    My Hobby Project: LegacyClone

  7. #7
    SMHouston is offline Member
    Join Date
    Sep 2009
    Posts
    80
    Rep Power
    0

    Default

    k, will give it a try. Thanks again for the help.

    Do you have any references that you know of that I could take a look at to see how I would let the user search for a book by its ISBN or Author that I set for the book?

  8. #8
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    5

    Default

    Well, if we are talking about real world usage... then the only way to go is with a database of one flavor or another.

    SQL

    Otherwise it is rather difficult to create your own search-able data-structures, but it is possible. :)

    edit: It's funny that the wikipedia article actually has a book searches as examples.
    Last edited by mrmatt1111; 09-04-2009 at 12:45 AM.
    My Hobby Project: LegacyClone

  9. #9
    SMHouston is offline Member
    Join Date
    Sep 2009
    Posts
    80
    Rep Power
    0

    Default

    Thats what I figured is by using a database. I just figured they could pull from the list I made up top somehow by entering the book name or isbn number and it would bring down something like:

    Is this the book your looking for?
    (description of book)

    Otherwise:
    We do not carry this book.

  10. #10
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    5

    Default

    Yea a direct lookup is pretty easy:

    An example is a HashMap, which may or may not be the best thing to use depending on the size of your dataset:

    Java Code:
    HashMap<String,Book> bookMap = new HashMap<String,Book>();
    
    bookMap.put("1234-122-3133", book1);
    
    System.out.println(bookMap.get("1234-122-3133"));
    My Hobby Project: LegacyClone

  11. #11
    SMHouston is offline Member
    Join Date
    Sep 2009
    Posts
    80
    Rep Power
    0

    Default

    Ok I'll give it a whirl

    I haven't tried it yet but I was thinking on the lines of making the choice in the case lines like:

    Case 5: "search for book by author"

    if (case 5)
    System.out.println"what is the authors name"
    keyboard reader
    if authorsname = authors name
    return book1..2...3 etc
    else
    return we do not have that book in stock...same with the ISBN

  12. #12
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    the problem you have with different searchable properties is that a hash map only indexes based on the one key. this is why it would be best to use a database already implemented. if you don't want to use a database, you can still try to learn how to use apache lucene for indexing. i've only used it once and that was over a year and a half ago, so i don't remember much, but i remember it being a bit complicated.

  13. #13
    SMHouston is offline Member
    Join Date
    Sep 2009
    Posts
    80
    Rep Power
    0

    Default

    So doing it the way I mentioned above would be a waste? If so I'll save my time and problems I may get myself into.

  14. #14
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Hmm... it appears someone should write a Map implementation that takes multiple keys...

    Maybe I'll do that sometime.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  15. #15
    SMHouston is offline Member
    Join Date
    Sep 2009
    Posts
    80
    Rep Power
    0

    Default

    Ok, I've updated a few things and put in a cash register.
    Now, this are the lines I'm having problems with:

    Java Code:
    	public static void showSelection()
    		{
    			System.out.println("WELCOME TO THE BOOK FINDER");
    			System.out.println("To select a book or search, enter ");
    			System.out.println("1 for ");
    			System.out.println("2 for ");
    			System.out.println("3 for ");
    			System.out.println("4 for Search by ISBN");
    			System.out.println("5 for Search by Author");
    			System.out.println("9 to exit");
    		}//end showSelection
    In these lines here for 1-2-3 I want to print the book information out. Now I've tried the method in the class book but something isn't right.

    This is what I have there:

    Java Code:
    		//Method to print out book information on option screen
    	public String toString()
    	{
    		return "Book1: " + bookTitle +"\nAuthor Name: " + authorName + "\nPublisher Name: " + pubName 
    					+ "\nISBN : " + isbnNum + "\nNumber of Copies : " + numberOfCopies + "\nBook Price: " + bookCost;
    	}

    I'm also having problems with the cash register. I can get it to output the number of copies, ask the customer how many copies they want, then when they enter that it prints out how much they owe. However, when they enter an integer it works, but when you enter it as a double like 129.99 it throws an error.
    Heres the method:

    Java Code:
    	public static void sellProduct(Book product, Book book, CashRegister cRegister)
                                                 throws IOException
        {
             int amount;  //variable to hold the amount entered
             double amount2; //variable to hold the amount of cash from consumer
    			double amount3; //variable to hold the extra amount of cash
    
             if(product.getCount() > 0)  //if inventory is not empty
             {
                System.out.println("We have " + product.getCount() + " copies on hand, how many would you like?");                              
                                   
                amount = Integer.parseInt(keyboard.readLine());
    				
    			 if(amount <= product.getCount())
                {
              	System.out.println("You owe " + product.getBookCost() * amount);
    				
    				amount2 = Integer.parseInt(keyboard.readLine());
    				
    				 if(amount2 < product.getBookCost() * amount)
                {
                   System.out.println("You owe another "
                               + (product.getBookCost() - amount2)
                               + " cents");
                   amount3 = Integer.parseInt(keyboard.readLine());
                   amount2 = amount2 + amount3;
                }
    
                if(amount >= product.getBookCost())
                {
                   cRegister.acceptAmount(amount);
                   product.saleMade();
                   System.out.println("Thank you for your purchase");                               
    					            }
                else
                   System.out.println("The amount is not enough. "
                                    + "Here is your money back");
                System.out.println("*-*-*-*-*-*-*-*-*-*-"
                                 + "*-*-*-*-*-*-*-*-*-*");
    				}
                else
                   System.out.println("We do not have that many in stock");
    			 }
             else
                System.out.println("Sorry this item is sold out.");
        }//end sellProduct
    and here is my cash register class incase anyone needs it to look at:

    Java Code:
    //class cashRegister
    
    public class CashRegister
    
    {
        private double cashOnHand;   //variable to store the cash
                                  //in the register
    
            //Constructor with parameters
            //To set the cash in the register to a specific amount
            //Postcondition: cashOnHand = cashIn;
        public CashRegister(double cashIn)
        {
            if(cashIn >= 0)
             cashOnHand = cashIn;
            else
                 cashOnHand = 500.00;
        }
    
            //Default constructor with parameters
            //To set the cash in the register 500 dollars
            //Postcondition: cashOnHand = 500;
        public CashRegister()
        {
             cashOnHand = 500.00;
        }
    
            //Method to show the current amount in the cash register
          //Postcondition: The value of the instance variable
            //               cashOnHand is returned
        public double currentBalance()
        {
          return cashOnHand;
        }
    
            //Method to receive the amount deposited by
            //the customer and update the amount in the register.
            //Postcondition: cashOnHand = cashOnHand + amountIn
        public void acceptAmount(double amountIn)
        {
            cashOnHand = cashOnHand + amountIn;
        }
    }
    Been looking at this all morning, and its probably something simple that I just can't see. Also its printing some of the book prices out like 209.9898983943 How can i get that to round, gonna start hunting through my book now.
    Last edited by SMHouston; 09-04-2009 at 04:49 PM.

  16. #16
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    You are using the wrong type to store amount. You are using int (integer) when you want to be able to accept decimal point numbers as well. Better use double and use Double.parseDouble for the conversion.
    Have a look at System.out.printf for the formatting part.

  17. #17
    SMHouston is offline Member
    Join Date
    Sep 2009
    Posts
    80
    Rep Power
    0

    Default

    Ok, thanks, I knew it was something simple.

    Anyone know where I'm going wrong in these lines here? I want to get the book to print it out, and my toString is where I'm having a problem in this program and my other book program, something isn't clicking as I'm reading it over and over.

    Java Code:
    Book book = new Book();
    		Book book1 = new Book("How to Sail", 1, "Shawn Houston", "Coastal Publishing", 
    								232908343, 5, 50.00); 
    		Book book2 = new Book("How to Fish", 1, "Dick Charles", "Frontal Publishing",
    								342345345, 6, 39.99); 
    		Book book3 = new Book("How to JAVA", 1, "Shane Smith", "Publishers Inc.",
    								435354656, 10, 29.99);
    this is what I'm trying to get printed out in:

    Java Code:
    public static void showSelection()
    		{
    			System.out.println("WELCOME TO THE BOOK FINDER");
    			System.out.println("To select a book or search, enter ");
    			System.out.println("1 for ");
    			System.out.println("2 for ");
    			System.out.println("3 for ");
    			System.out.println("4 for Search by ISBN");
    			System.out.println("5 for Search by Author");
    			System.out.println("9 to exit");
    		}//end showSelection
    Which is what I need to toString for but like I said I'm missing something here.

    Java Code:
    public String toString()
    	{
    		return "Book1: " + bookTitle +"\nAuthor Name: " + authorName + "\nPublisher Name: " + pubName 
    					+ "\nISBN : " + isbnNum + "\nNumber of Copies : " + numberOfCopies + "\nBook Price: " + bookCost;
    	}
    Last edited by SMHouston; 09-04-2009 at 07:21 PM.

  18. #18
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    You are failing to explain yourself very well.
    What exactly is wrong with your toString method?
    What is it printing out and what do you want it to print out instead?

  19. #19
    SMHouston is offline Member
    Join Date
    Sep 2009
    Posts
    80
    Rep Power
    0

    Default

    Its not printing out anything.

    For example if I make the line:

    Java Code:
    System.out.println("1 for " + book1);
    System.out.println("2 for " + book2);
    System.out.println("3 for " + book3);
    I get this error:

    BookFinder.java:49: cannot find symbol
    symbol : variable book1
    location: class BookFinder
    System.out.println("1 for " + book1);
    ^
    1 error

    I'm guessing I'm not calling the toString correctly, but I have no idea how. This is where I'm stuck on two programs using a toString

    Java Code:
    public String toString()
    	{
    		return "Book1: " + bookTitle +"\nAuthor Name: " + authorName + "\nPublisher Name: " + pubName 
    					+ "\nISBN : " + isbnNum + "\nNumber of Copies : " + numberOfCopies + "\nBook Price: " + bookCost;
    	}
    What I want it to print out is the for example

    Choice 1: (Book Information, I.E title, name, author, isbn number)
    Choice 2: (Book Information, I.E title, name, author, isbn number)

    That I have set in the beginning of the program like

    Java Code:
    	Book book1 = new Book("How to Sail", 1, "Shawn Houston", "Coastal Publishing", 
    								232908343, 5, 50.00); 
    		Book book2 = new Book("How to Fish", 1, "Dick Charles", "Frontal Publishing",
    								342345345, 6, 39.99); 
    		Book book3 = new Book("How to JAVA", 1, "Shane Smith", "Publishers Inc.",
    								435354656, 10, 29.99);
    Last edited by SMHouston; 09-04-2009 at 07:43 PM.

  20. #20
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Your toString is still innocent (yet anyway). It is not even being called yet.
    You are simply trying to use a variable called book1 at a point where it is not known.
    Better post the code for the BookFinder class

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 0
    Last Post: 08-26-2009, 01:03 PM
  2. best book for jsf
    By raj17 in forum JavaServer Faces (JSF)
    Replies: 8
    Last Post: 08-04-2009, 08:08 AM
  3. sign finder
    By iPetey in forum New To Java
    Replies: 6
    Last Post: 05-01-2009, 05:24 AM
  4. Root Finder for polynomials
    By gibsonrocker800 in forum Advanced Java
    Replies: 11
    Last Post: 01-03-2008, 11:26 PM
  5. Library Finder 1.2
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 07-15-2007, 04:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •