Results 1 to 5 of 5
  1. #1
    arrech326 is offline Member
    Join Date
    Nov 2009
    Posts
    13
    Rep Power
    0

    Post Three classes help!!

    I cant seem to make this three classes talk with each other Any Suggestions??

    Java Code:
    import java.util.Scanner;// 
    
    public class BookStore {
    	
    	public static void main(String args[]){
    		
    		Scanner userIn = new Scanner(System.in);
    		BookInventory bI = new BookInventory();
    		InventoryResult invRes = new InventoryResult();
    		
    		Boolean exit = false;
    		
    			System.out.println("Welcome to the Book Store Inventory!");
    					while (!bI.getBookName().equals("exit"))
    				{
    			System.out.println();
    			System.out.println("Please enter the book name here: ");
    			System.out.println();
    			System.out.println("To exit the Inventory please enter out as book name.");
    				String bookName = userIn.nextLine();
    					if (bookName.equals("exit")){
    						Exit();
    				}
    					else{
    			System.out.println();
    			System.out.println("Please enter the ISBN number for " + bookName);
    				int ISBN = userIn.nextInt();
    			System.out.println();
    			System.out.println("Please enter the quantity of "+ bookName);
    				int unitQ = userIn.nextInt();
    			System.out.println();
    			System.out.println("Please enter the price of "+ bookName);
    				double bookPrice = userIn.nextDouble();
    			System.out.println();
    			
    			invRes.displayInventory();
    			
    			userIn.nextLine();// Clear the buffer
    			
    				}
    				}
    				}
    	private static void Exit(){ // Creates exit module
    			System.out.println("Thank you for using the Inventory Application. Come back soon, Good bye!");// Prompts user Good bye message
    				System.exit (0);// exits program
    		
    	}//End main module
    	
    				
    }//End BookStore Class
    
    
    import java.text.NumberFormat;
    
    public class BookInventory {// Creates BookInventory Class
    	
    	private String bookName = "";// Declares bookName as string variable
    	private int ISBN, unitQ = 0;// Declares ISBN and unitQ as integer variables
    	private double bookPrice, inventoryValue = 0;// Declares bookPrice and inventoryValue as double variables
    	
    		NumberFormat nf = NumberFormat.getCurrencyInstance();
    	
    	public BookInventory(){ }//Creates and terminates BookInventory Empty Constructor
    	
    	public BookInventory(String bookNameIn, int ISBNIn, int unitQIn, double bookPriceIn){//Creates BookInventory Constructor with parameters
    		setBookName (bookNameIn);
    		setISBNnum (ISBNIn);
    		setUnitQ (unitQIn);
    		setBookPrice (bookPriceIn);
    	}//Terminates BookInventory Constructor with parameters
    	
    	public void setBookName(String bookNameIn){
    		this.bookName = bookNameIn;
    	}
    	public String getBookName(){
    			return this.bookName;
    	}
    	public void setISBNnum(int ISBNIn){
    		ISBN = ISBNIn;
    	}
    	public int getISBNnum(){
    			return ISBN;
    	}
    	public void setUnitQ(int unitQIn){
    		unitQ = unitQIn;
    	}
    	public int getUnitQ(){
    			return unitQ;
    	}
    	public void setBookPrice(double bookPriceIn){
    		bookPrice = bookPriceIn;
    	}
    	public double getBookPrice(){
    			return bookPrice;
    	}
    	public double getInventoryValue(){
    		
    		double inventoryValue = bookPrice * (int)unitQ;
    			return inventoryValue;
    	}
    }//Ends BookInventory Class
    
    
    class InventoryResult {
    
    	public void displayInventory(){
    		
    		BookInventory InvObject = new BookInventory();
    		BookStore bsObject = new BookStore();
    		
    		InvObject.setBookName(bookName);
    		InvObject.setBookPrice(bookPrice);
    		InvObject.setISBNnum(ISBN);
    		InvObject.setUnitQ(unitQ);
    		
    		inventoryValue = bookPrice * unitQ;
    		
    			System.out.println("Title: "+ bookName);
    			System.out.println();
    			System.out.println("ISBN # "+ ISBN);
    			System.out.println();
    			System.out.println("Unit Price: "+ nf.format(bookPrice));
    			System.out.println();
    			System.out.println("Quantity: "+ unitQ);
    			System.out.println();
    			System.out.println("The inventory total amount for "+ bookName + "is "+ nf.format(inventoryValue) +".");
    			System.out.println();
    	}//Ends displayInventory method
    }//End InventoryResult class
    I'm trying to make all classes see the variables but they have to be private. I created objects but is giving me errors on the variables on the other classes.

    :confused:
    -Charlene

  2. #2
    silverspoon34 is offline Member
    Join Date
    Nov 2009
    Posts
    18
    Rep Power
    0

    Default

    look at your first few lines spotted this

    public static void main(String args[]){

    should be

    public static void main(String[] args){

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by silverspoon34 View Post
    look at your first few lines spotted this

    public static void main(String args[]){

    should be

    public static void main(String[] args){
    silver, you might want to test this on a program yourself to see if it really makes a difference. In fact, I'll give you the program and let you compile and run it:
    Java Code:
    public class Fu1 {
       public static void main(String args[]) {
          System.out.println("Does this work?");
       }
    }

  4. #4
    silverspoon34 is offline Member
    Join Date
    Nov 2009
    Posts
    18
    Rep Power
    0

    Default

    not a pro just looked out of place to what i knew ( i stand corrected)

    however i did run it on eclipse which came with 18 errors on debug mostly to do with things that would be requested as user input, but scanner has been initialised so cant see why it would throw that.

  5. #5
    rdtindsm is offline Member
    Join Date
    Feb 2009
    Posts
    92
    Rep Power
    0

    Default

    As an example of the problem, lets look at several lines of code.

    You have this line
    private String bookName = "";// Declares bookName as string variable
    in file define class BookInventory.displayResult();

    and this line
    InvObject.setBookName(bookName);
    in InventoryResult.

    I have to guess that the two book names refer to the same variable because your statement of the problem doesn't indicate which variables you can't see, and where you expect to see them.

    When I put your code in eclipse, I got all sort of error messagess saying that the name can't be resolved, bookName in the last quoted line being a case in point.

    There is no variable called bookName in this file. You need to create it and assign values, perhaps through interaction with a keyboard with code something like
    String bookName = userIn.nextLine();
    You know how to do this because the above line is quoted from the Bookstore main method.

    .Your main method sets variables called bookName, ISBN, etc but they are strictly local. They have no relation to any similarly named variables in other files, except by any coincidence of names. Review the concepts of scope in relation to local, instance, and class variables.

    You wrote setters and getters. I'm assuming you didn't do it just to practice typing. Use them to set the values in your BookInventory object that you created.

    Now, I'm also guessing that the signature for
    displayInventory()
    needs to be changed to displayInventory(BookInventory foo)
    where foo is the object created in main.

    You don't have to set anything in displayInventory. If you want to display the values in a println, use a statement like foo.getBookName(). You could perhaps declare these values to be public and access them as foo.bookName, but that is considered poor form because it violates the concepts of encapsulation and data hiding.
    Last edited by rdtindsm; 11-24-2009 at 06:36 AM. Reason: spelling

Similar Threads

  1. Help with classes
    By gnarly hogie in forum New To Java
    Replies: 14
    Last Post: 10-10-2008, 02:29 PM
  2. Get name of available classes
    By escuja in forum CLDC and MIDP
    Replies: 0
    Last Post: 07-26-2008, 12:03 PM
  3. Cant run my classes
    By Assaf A in forum Eclipse
    Replies: 1
    Last Post: 04-22-2008, 02:31 PM
  4. Using a JAR from other classes
    By Joe2003 in forum Advanced Java
    Replies: 1
    Last Post: 01-02-2008, 07:08 PM
  5. When do we use inner classes?
    By cruxblack in forum New To Java
    Replies: 5
    Last Post: 08-10-2007, 05:00 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
  •