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

    Question Inventory part 2 help displaying result

    :confused:I'm trying to figure out why the results are not showing on the bottom. Can anyone tell me what I'm doing wrong? Any Suggestions with my code?

    I'm working on the Inventory Program part 2
    I'm supposed to create an array and display the information on each book.

    Thanks
    Java Code:
    import java.text.NumberFormat;
    import java.util.Arrays;
    
    public class Inventory {	//Class Inventory
    
    	public static void main(String[]args){	//Main method
    		
    		NumberFormat nf = NumberFormat.getCurrencyInstance();
    		Product bookInfo = new Product();	//Creates object for Product class
    		
    		bookInfo.getTitle();	//Calls getTitle method
    		bookInfo.getISBN();		//Calls getISBN method
    		bookInfo.getUnits();	//Calls getUnits method
    		bookInfo.getPrice();	//Calls getPrice method
    		bookInfo.getValue();	//Calls getValue method
    		bookInfo.getTotalValue();	//Calls getTotalValue method		
    		
    			System.out.println("\t\tWelcome to the Book Store Inventory!");	//Displays welcome message
    			System.out.println();	//Creates space for readability
    			System.out.println("Title\t\t\tISBN\t\tUnit Quantity\tPrice\t\tValue");	//Creates header for table
    			System.out.println();	//Creates space for readability
    			
    		final int ARRAY_LENGTH = 4;
    		Product book[] = new Product[ARRAY_LENGTH];
    		
    		book [0] = new Product("Programming 101",123456789,15,14.99);
    		book [1] = new Product("Java for beginners",987345524,2,15.95);
    		book [2] = new Product("Learning to Program",134553256,12,6.99);
    		book [3] = new Product("How to Java",146542334,4,12.49);
    		
    			for(int i = 0; i<book.length; i++){
    				
    		System.out.println(book[0]);	//Displays 1st element of each array.
    		System.out.println();	//Creates space for readability
    		System.out.println(book[1]);	//Displays 2nd element of each array.
    		System.out.println();	//Creates space for readability
    		System.out.println(book[2]);	//Displays 3rd element of each array
    		System.out.println();	//Creates space for readability
    		System.out.println(book[3]);	//Displays 4th element of each array
    		System.out.println();	//Creates space for readability
    		System.out.println();	//Creates space for readability
    		
    		}	//End for loop
    	}	//Ends method
    }	//Ends class
    Java Code:
    import java.util.Scanner;
    
    public class Product {	//Creates class Product
    	private String title;	// Declares bookName as string variable
    	private int ISBN, units;	// Declares ISBN and unitQ as integer variables
    	private double price, value;	// Declares Price and Value as double variables
    	private static double totalValue;	// Declares totalValue as double static variable.
    				Scanner userIn = new Scanner(System.in);	//Names Scanner userIn
    	
    	public Product(){ }//Creates and terminates BookInventory Empty Constructor
    	
    	public Product(String titleIn, int ISBNIn, int unitsIn, double priceIn){//Creates BookInventory Constructor with parameters
    		this.setTitle (titleIn);
    		this.setISBN (ISBNIn);
    		this.setUnits (unitsIn);
    		this.setPrice (priceIn);
    	}//Terminates BookInventory Constructor with parameters
    	
    	public void setTitle(String titleIn){
    		this.title = titleIn;	//Set instance variable title
    	}
    	public String getTitle(){
    			return title;	//Get title value
    	}
    	public void setISBN(int ISBNIn){
    		this.ISBN = ISBNIn;		//Set instance variable ISBN
    	}
    	public int getISBN(){
    			return ISBN;	//Get ISBN value
    	}	
    	public void setUnits(int unitsIn){
    		this.units = unitsIn;	//Set instance variable units
    	}
    	public int getUnits(){
    			return units;	//Get units value
    	}
    	public void setPrice(double PriceIn){
    		this.price = PriceIn;	//Set instance variable price
    	}
    	public double getPrice(){
    			return price;	//Get price value
    	}
    	public void setValue(){
    		value=  price * (int)units;	//Set and calculates value of price * units
    	}
    	public double getValue(){
    			return value;	//Get value price * units
    	}
    	public double getTotalValue(){
    		return totalValue;	//Gets totalValue value
    	}
    	
    	public void displayInventory(){
    		
    		
    	}
    	
    }
    -Charlene

  2. #2
    dinosoep is offline Senior Member
    Join Date
    Nov 2009
    Posts
    150
    Rep Power
    5

    Default

    System.out.println(book[0]);
    book[0] is a book.
    can your printer print a 3d book? no, it can't but it can print text
    System.out.println(here has to be text and not an book object)

  3. #3
    arrech326 is offline Member
    Join Date
    Nov 2009
    Posts
    13
    Rep Power
    0

    Default

    how can I display the items on the array? I understand that the book object can not be displayed but the information on it? I'm so lost :( thanks for the reply
    -Charlene

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

    Default

    I respectfully disagree with Dinosoep. Any object can be printed, and System.out.println(book[i]) is perfectly valid, but it only will print something that makes sense if you tell it to. I suggest that you give your Product class a decent toString method. For e.g.,

    Java Code:
      public void toString() {
      
        return title + ", " + ISBN; // feel free to extend this to include other useful bits of info
                                          // from the Product class
    
      }
    Last edited by Fubarable; 12-02-2009 at 12:01 AM.

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

    Default

    Well I remember last time I tried I created an array for each object and it came out ok but I have to do one array with the elements.

    I rememberone of my lines was
    System.out.println("Title: " + book[0]);

    I want to display the items in a table but they have to be in one array so when I sort the array by title alphabetically the rest of the elements for that book stay with it and it wont just sort the title and leave the rest where they are because is not the same info.
    -Charlene

Similar Threads

  1. Inventory Program Part 2 of 6
    By tlouvierre in forum New To Java
    Replies: 2
    Last Post: 05-28-2009, 02:30 AM
  2. Inventory Program Part 3 ~ please help!
    By marMcD in forum New To Java
    Replies: 13
    Last Post: 02-25-2009, 06:57 AM
  3. Java Inventory Program Part 3
    By ljk8950 in forum New To Java
    Replies: 18
    Last Post: 07-28-2008, 06:47 AM
  4. Inventory part 3 program problems
    By badness in forum New To Java
    Replies: 1
    Last Post: 12-17-2007, 08:00 AM
  5. Inventory part 2 help please
    By badness in forum New To Java
    Replies: 1
    Last Post: 12-12-2007, 08:51 AM

Posting Permissions

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