Total Inventory coming up null

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 468 Views - Last Post: 09 May 2012 - 06:43 PM Rate Topic: -----

#1 acetown  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 58
  • Joined: 06-April 12

Total Inventory coming up null

Posted 09 May 2012 - 02:01 PM

I cannot figure out why total inventory is coming up null. It runs like it is supposed to, but it puts total inventory is null. I need it to produce the output of NumberofUnits * UnitPrice
Here is my code
import java.util.Scanner;

  class Camera
	{

	    private String ProductName;
	    private int ItemNumber;
	    private int NumberofUnits;
	    private double UnitPrice;
	 
	    public Camera(int i, String Name, int Number, int Units, double Price)
	    {
	 
                ProductName = Name;
	        ItemNumber = Number;
	        NumberofUnits = Units;
	        UnitPrice = Price;
	    }
	 
	    public void setProductName(String title, String Name)
	    {
	        ProductName = Name;
	    }
	 
	    public String getProductName()
	    {
	        return ProductName;
	    }
	 
	    public void setItemNumber(int Number)
	    {
	        ItemNumber = Number;
	    }
	 
	    public double getItemNumber()
	    {
	        return ItemNumber;
	    }
	 
	    public void setNumberofUnits(int Units)
	    {
	        NumberofUnits = Units;
	    }
	 
	    public double getNumberofUnits()
	    {
	        return NumberofUnits;
	    }
	 
	    public void setUnitPrice(double Price)
	    {
	        UnitPrice = Price;
	    }
	 
	    public double getUnitPrice()
	    {
	        return UnitPrice;
	    }

            public void setTotalInventory(double Inventory)
            {
            }
	 
	    public double getTotalInventory()
	    {
	        return UnitPrice * NumberofUnits;
	    }
          
	}


	 
	public class Inventory
	{
	 
	    private static Scanner input = new Scanner(System.in);
		private static String TotalInventory;
	 
	    public static void main(String args[] )
	    {
	        Camera[] camera = new Camera[5];
	 
	        camera[0] = new Camera(0, "Nikon", 0001, 5, 1499);
	        camera[1] = new Camera(0, "Olympus", 0002, 7, 799);
	        camera[2] = new Camera(0, "Canon", 0003, 3, 1549);
	        camera[3] = new Camera(0, "Casio", 0004, 10, 89);
	        camera[4] = new Camera(0, "Kodak", 0005, 3, 299);
	 
	        System.out.println("Camera Inventory Program");
	 
	        for (int i = 0; i < camera.length; i++)
	        {
	            System.out.println("Product Name is " + camera[i].getProductName());
	            System.out.println("Item Number is " + camera[i].getItemNumber());
	            System.out.println("Number of Units is "+ camera[i].getNumberofUnits());
	            System.out.println("Unit Price is " + camera[i].getUnitPrice());
	           
				
				System.out.println("TotalInventory is " + TotalInventory);
	        }
	 
	    }
	 
	    public static Scanner getInput()
	    {
	        return input;
	    }
	 
	    public static void setInput(Scanner input)
	    {
	        Inventory.input = input;
	    }
	}
   


Is This A Good Question/Topic? 0
  • +

Replies To: Total Inventory coming up null

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • Joined: 10-September 10

Re: Total Inventory coming up null

Posted 09 May 2012 - 02:05 PM

Total inventory is printed using this line (#099):

System.out.println("TotalInventory is " + TotalInventory);

Where is TotalInventory modified from its default value of null as an Inventory instance variable?
Was This Post Helpful? 1
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Total Inventory coming up null

Posted 09 May 2012 - 02:09 PM

	public class Inventory
	{
	 
	    private static Scanner input = new Scanner(System.in);
            // TotalInventory declared here it is null
		private static String TotalInventory;
	 
	    public static void main(String args[] )
	    {

	        Camera[] camera = new Camera[5];
	 
	        for (int i = 0; i < camera.length; i++)
	        {
	            System.out.println("Product Name is " + camera[i].getProductName());
	            System.out.println("Item Number is " + camera[i].getItemNumber());
	            System.out.println("Number of Units is "+ camera[i].getNumberofUnits());
	            System.out.println("Unit Price is " + camera[i].getUnitPrice());
	           
			// TotalInventory print here. Nowhere you assigned a value to it
                        // so it is still null	
			System.out.println("TotalInventory is " + TotalInventory);
	        }


Was This Post Helpful? 1
  • +
  • -

#4 guntario  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 24
  • Joined: 28-March 12

Re: Total Inventory coming up null

Posted 09 May 2012 - 02:14 PM

Your getters and setters are in the wrong class. They should be in the same class as TotalInventory.
Was This Post Helpful? 1
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Total Inventory coming up null

Posted 09 May 2012 - 02:18 PM

View Postguntario, on 09 May 2012 - 05:14 PM, said:

Your getters and setters are in the wrong class. They should be in the same class as TotalInventory.

???
Was This Post Helpful? 1
  • +
  • -

#6 acetown  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 58
  • Joined: 06-April 12

Re: Total Inventory coming up null

Posted 09 May 2012 - 02:21 PM

I am trying to get it to print a total but cannot seem to get it. I hate being new to java.
Was This Post Helpful? 0
  • +
  • -

#7 guntario  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 24
  • Joined: 28-March 12

Re: Total Inventory coming up null

Posted 09 May 2012 - 02:22 PM

View Postpbl, on 09 May 2012 - 02:18 PM, said:

View Postguntario, on 09 May 2012 - 05:14 PM, said:

Your getters and setters are in the wrong class. They should be in the same class as TotalInventory.

???

He's updating private String TotalInventory from the Camera class, but he declared TotalIventory in the inventory class.
Was This Post Helpful? 2
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Total Inventory coming up null

Posted 09 May 2012 - 02:25 PM

no he probably wants to println()

camera[i].getTotalInventory()

:)
Was This Post Helpful? 1
  • +
  • -

#9 acetown  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 58
  • Joined: 06-April 12

Re: Total Inventory coming up null

Posted 09 May 2012 - 02:28 PM

it was actually something that simple. I changed it to that and it worked just like it should have. Thanks for all the help.
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Total Inventory coming up null

Posted 09 May 2012 - 02:33 PM

View Postguntario, on 09 May 2012 - 05:22 PM, said:

He's updating private String TotalInventory from the Camera class,

where do you see that ?
Was This Post Helpful? 0
  • +
  • -

#11 DimitriV  Icon User is online

  • >not activating your almonds
  • member icon

Reputation: 560
  • View blog
  • Posts: 2,669
  • Joined: 24-July 11

Re: Total Inventory coming up null

Posted 09 May 2012 - 02:36 PM

Doesn't look like there is one.
Was This Post Helpful? 1
  • +
  • -

#12 acetown  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 58
  • Joined: 06-April 12

Re: Total Inventory coming up null

Posted 09 May 2012 - 02:54 PM

Now I have to Create a method to calculate the value of the entire inventory.
Create another method to sort the array items by the name of the product.
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Total Inventory coming up null

Posted 09 May 2012 - 03:01 PM

for the Toatl

  double total = 0;
  for(int i = 0; i < camera.length; ++i) {
     ...
     System.out.println("Total inv: " + camera[i].getTotalInventory());
     total += camera[i].getTotalInventory();
  }
  System.out.pritnl(total);


for the sort have your class Camera:

class Camera implements Comparable<Camera> {
  
   public int compareTo(Camera other) {
     return String.compareTo(productName, other.productName);
   }



in your main simply do: Arrays.sort(camera);
Was This Post Helpful? 1
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Total Inventory coming up null

Posted 09 May 2012 - 04:52 PM

View PostDimitriV, on 09 May 2012 - 05:36 PM, said:

Doesn't look like there is one.

Yes I know, my +1 was a missclick :)

butseems that acetown gives +1 to anybody who reply to his post
Whatever they reply :)
Was This Post Helpful? 0
  • +
  • -

#15 acetown  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 58
  • Joined: 06-April 12

Re: Total Inventory coming up null

Posted 09 May 2012 - 06:35 PM

When I implement the class Camera like you show, it gives this message The method compareTo(String) in the type String is not applicable for the arguments (String, String)
Here is the code
import java.util.Arrays;
import java.util.Scanner;

  class Camera implements Comparable<Camera>{
	  
	  public int compareTo(Camera other){
		  return String.compareTo(ProductName, other.ProductName);
	  }
  
	
       
	   
	    private String ProductName;
	    private int ItemNumber;
	    private int NumberofUnits;
	    private double UnitPrice;
	 
	    public Camera(int i, String Name, int Number, int Units, double Price)
	    {
	    	
	        
                ProductName = Name;
	        ItemNumber = Number;
	        NumberofUnits = Units;
	        UnitPrice = Price;
	    }
	 
	    public void setProductName(String title, String Name)
	    {
	        ProductName = Name;
	    }
	 
	    public String getProductName()
	    {
	        return ProductName;
	    }
	 
	    public void setItemNumber(int Number)
	    {
	        ItemNumber = Number;
	    }
	 
	    public double getItemNumber()
	    {
	        return ItemNumber;
	    }
	 
	    public void setNumberofUnits(int Units)
	    {
	        NumberofUnits = Units;
	    }
	 
	    public double getNumberofUnits()
	    {
	        return NumberofUnits;
	    }
	 
	    public void setUnitPrice(double Price)
	    {
	        UnitPrice = Price;
	    }
	 
	    public double getUnitPrice()
	    {
	        return UnitPrice;
	    }

            public void setTotalInventory(double Inventory)
            {
            	Inventory = getTotalInventory();
            }
	 
	    public double getTotalInventory()
	    {
	        return UnitPrice * NumberofUnits;
	    }

            
	}
  

	 
	public class Inventory
	{
	 
	    
		private static Scanner input = new Scanner(System.in);
		
		
		
	 
	    public static void main(String args[])
	    {
	    	
	        Camera[] camera = new Camera[5];
	        Arrays.sort(camera);
	        
	        camera[0] = new Camera(0, "Nikon", 0001, 5, 1499);
	        camera[1] = new Camera(0, "Olympus", 0002, 7, 799);
	        camera[2] = new Camera(0, "Canon", 0003, 3, 1549);
	        camera[3] = new Camera(0, "Casio", 0004, 10, 89);
	        camera[4] = new Camera(0, "Kodak", 0005, 3, 299);
	 
	        System.out.println("Camera Inventory Program");
	 
	        for (int i = 0; i < camera.length; i++)
	        {
	            System.out.println("Product Name is: " + camera[i].getProductName());
	            System.out.println("Item Number is: " + camera[i].getItemNumber());
	            System.out.println("Number of Units is: "+ camera[i].getNumberofUnits());
	            System.out.println("Unit Price is: " + camera[i].getUnitPrice());
		    System.out.println("TotalInventory is: " + camera[i].getTotalInventory());
		    System.out.println();
	        }
	        
	        double total = 0;
	        	for(int i = 0; i < camera.length; ++i) {
	        	   
	        	   System.out.println("Total inv: " + camera[i].getTotalInventory());
	        	   total += camera[i].getTotalInventory();
	        	}
	        	System.out.println(total);
	 
	    }
	 
	    public static Scanner getInput()
	    {
	        return input;
	    }
	 
	    public static void setInput(Scanner input)
	    {
	        Inventory.input = input;
	    }
	}

	    

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2