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

    Default

    Here it is:

    BookFinder:

    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
        {
    		CashRegister cashRegister = new CashRegister();
    		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); 
    		
    		
    		int choice; //variable to store choice
    		
    		showSelection();
    		choice = Integer.parseInt(keyboard.readLine());
    		
    		while(choice != 9)
    		{
    			switch(choice)
    			{
    			case 1: sellProduct(book1, book, cashRegister);
    						break;
    			case 2: sellProduct(book2, book, cashRegister);
    						break;
    			case 3: sellProduct(book3, book, cashRegister);
    						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 to Search by ISBN");
    			System.out.println("5 to Search by Author");
    			System.out.println("9 to exit");
    		}//end showSelection
    		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 = Double.parseDouble(keyboard.readLine());
    				
    				 if(amount2 < product.getBookCost() * amount)
                {
                   System.out.println("You owe another $"
                               + (product.getBookCost()* amount - amount2));
                   amount3 = Double.parseDouble(keyboard.readLine());
                   amount2 = amount2 + amount3;					
                }
    
                if(amount2 >= product.getBookCost())
                {
                   cRegister.acceptAmount(amount);
                   product.saleMade();
    					System.out.println("Thank you for your purchase");
    					System.out.println("We now have " + product.getCount() + " left in stock");                               
    				}
                else
                   System.out.println("The amount is not enough. "
                                    + "Here is your money back");
               		System.out.println("**Have a nice day**");
    				}
                else
                   System.out.println("Sorry, We do not have that many in stock");
    			 }
             else
                System.out.println("Sorry this item is sold out.");
        }//end sellProduct
    
    											
    }//end sellBook
    Heres the code for my book class:

    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--;
    	}
    
    		//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;
    	}
    
    }

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

    Default

    I'm sorry to say but your code looks quite a mess. Don't panic though it still has hope.
    So did you remove the lines that had

    System.out.println("1 for " + book1);
    then? Because I don't see them anymore.
    The trick is that a variable can only be used when it is available in scope.
    If you declare a variable say, book1 in the main method, then it can only be used inside the main method only.

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

    Default

    Yeah I took them out to test a few other things because the program is running fine until they get put in there.

    I thought I did declare them in the main method with

    book1 = new book(......)
    book2 = new book(......)
    etc

    However, when i put them in there i get nothing. My choice pull everything from the specific book that I need, I just can't get it to print out in the choice selection for whatever reason. I can get the number of copies i set, the price i set, the author if i put the code back in, everything works except trying to get the book information into that choice list.

  4. #24
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Just let your show selection method take the book that it wants to display. That way you'd have a book in scope to do the printing on.

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

    Default

    Not sure what you mean.

    I've tried the : " " + product.getBookTitle();

    But that's not working either. Do you mean by setting it manually?

    Like:
    System.ount.println("1 for "How to Sail");

    I got it so when they press the button it shows all the information about the book then asks them how many they would want, think i'll just go that route for this little project.

  6. #26
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Better to start cleaning up your code now.

    Why do you have options for searching when you don't have any search methods?
    When the user enters a choice, are they entering the number of a book or an option saying what they want to search by? You can't use that choice for both.

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

    Default

    I have the search choices because I need to set up a search by author or search by isbn method which I'm gonna work on, wanted to get the main part working before I even attempted it.

    When you say my code is a mess, what do you mean, the way its implemented or?

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

    Default

    Now that I've finished watching the cricket I can now say a few things about your program.
    It's lacking a lot of object oriented concepts. BookFinder must help to find books not sell them. It has no business knowing about money as well. Have a look at this skeleton

    Java Code:
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    
    class CashRegister {
    	public void sellBook(Book selectedBook) {
    
    	}
    
    	public void showSalesReport() {
    
    	}
    }
    
    enum SearchOption {
    	ISBN, AUTHOR, TITLE, EXIT;
    }
    
    @SuppressWarnings("serial")
    class BookFinder {
    	static Scanner keyboard = new Scanner(System.in);
    
    	List<Book> libray = new ArrayList<Book>() {
    		{
    			add(new Book("How to Sail", 1, "Shawn Houston",
    					"Coastal Publishing", 232908343, 5, 50.00));
    			add(new Book("How to Fish", 1, "Dick Charles",
    					"Frontal Publishing", 342345345, 6, 39.99));
    			add(new Book("How to JAVA", 1, "Shane Smith", "Publishers Inc.",
    					435354656, 10, 29.99));
    
    		}
    	};
    
    	public Book searchBy(SearchOption option) {
    		System.out.println(option);
    		switch (option) {
    			case ISBN: {
    				System.out.println("Enter the ISBN:");
    				// ...
    			}
    		}
    		return null;
    	}
    
    	public static void main(String[] args) throws IOException {
    		CashRegister cashRegister = new CashRegister();
    		BookFinder bookFinder = new BookFinder();
    
    		bookFinder.showSelection();
    
    		SearchOption choice = SearchOption.values()[keyboard.nextInt()];
    		while (choice != SearchOption.EXIT) {
    			Book selectedBook = bookFinder.searchBy(SearchOption.ISBN);
    			cashRegister.sellBook(selectedBook);
    			bookFinder.showSelection();
    			choice = SearchOption.values()[keyboard.nextInt()];
    		}
    		cashRegister.showSalesReport();
    		System.exit(0);
    
    	}
    
    	public void showSelection() {
    		System.out.println("WELCOME TO THE BOOK FINDER");
    		System.out.println("To select a book or search, enter ");
    		// What are 1,2,3 for?
    		System.out.println("4 to Search by ISBN");
    		System.out.println("5 to Search by Author");
    		System.out.println("9 to exit");
    	}
    }

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

    Default

    I see what your saying.

    Ok so that will basically search for a book by ISBN and author. The way I had it set was basically like a book store so I see where I went wrong.

    I've spent literally probably 30 hours in the last few days playing with this and I think I'm getting blind to alot of things :)

    Problem is this is a beginner book and I haven't even touched enums or anything like that in the book yet hence why i'm trying not to use them.

Page 2 of 2 FirstFirst 12

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
  •