Results 1 to 5 of 5
Thread: Three classes help!!
- 11-22-2009, 02:48 AM #1
Member
- Join Date
- Nov 2009
- Posts
- 13
- Rep Power
- 0
Three classes help!!
I cant seem to make this three classes talk with each other Any Suggestions??
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.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
:confused:-Charlene
- 11-22-2009, 07:27 PM #2
Member
- Join Date
- Nov 2009
- Posts
- 18
- Rep Power
- 0
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?"); } }
- 11-22-2009, 08:11 PM #4
Member
- Join Date
- Nov 2009
- Posts
- 18
- Rep Power
- 0
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.
- 11-24-2009, 06:34 AM #5
Member
- Join Date
- Feb 2009
- Posts
- 92
- Rep Power
- 0
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
-
Help with classes
By gnarly hogie in forum New To JavaReplies: 14Last Post: 10-10-2008, 02:29 PM -
Get name of available classes
By escuja in forum CLDC and MIDPReplies: 0Last Post: 07-26-2008, 12:03 PM -
Cant run my classes
By Assaf A in forum EclipseReplies: 1Last Post: 04-22-2008, 02:31 PM -
Using a JAR from other classes
By Joe2003 in forum Advanced JavaReplies: 1Last Post: 01-02-2008, 07:08 PM -
When do we use inner classes?
By cruxblack in forum New To JavaReplies: 5Last Post: 08-10-2007, 05:00 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks