Hello, if anyone can help I'd greatly appreciate any input! I am of course another newbie and attempting to compile the Inventory program - it contains arrays and I am trying to create a method to calculate the total inventory which is contained within the array named sumInventory[]

Here's the inventorypart2 class this contains the arrays.

public class InventoryPart2 {
    // main
    public static void main(String[] args) {
    	
    	// welcome screen
    	System.out.println("Welcome to Inventory Program Part 2");
    	System.out.println();
    	System.out.println();//blank line
    	System.out.println("***Inventory Items***");
 		System.out.println();
    		// create and intialize ProductsPart2 object
	ProductsPart2 p1 = new ProductsPart2(1, "Pens", 50, 1.50);
	ProductsPart2 p2 = new ProductsPart2(2, "Pencils", 25, 0.25);
	ProductsPart2 p3 = new ProductsPart2(3, "Hammer", 15, 12.00);
	ProductsPart2 p4 = new ProductsPart2(4, "Tape", 15, 10.45);
	ProductsPart2 p5 = new ProductsPart2(5, "Paper", 25, 7.95);
	ProductsPart2 p6 = new ProductsPart2(6, "Scissors", 6, 4.25);
  	
  
   //declare and intitialize the arrays & specifies elements
 	
 		
 	int itemNumber[] = { 1, 2, 3, 4, 5, 6};
 	int numUnits[] = { 50, 25, 15, 15, 25, 8};
 	double priceUnits [] = {1.50, .25, 12.00, 10.45, 7.95, 4.25};		
 	String prodName[] = {"Pens", "Pencils", "Hammer", "Tape", "Paper", "Scissors"}; 

 //	double calculateInventory[] = {p1.calculateInventory(),p2.calculateInventory(), p3.calculateInventory(), 
 //	p4.calculateInventory(), p5.calculateInventory(), p6.calculateInventory()};
 	double sumInventory[] = {75, 6.25, 180, 156.75, 198.75, 25.50};
 	
 
 	//displays the array unsorted
 	for(int i = 0; i < 6; i++){
 	
 	System.out.printf("Item Name: %s\n" , prodName[i]);//product number display
 	System.out.printf("Item Number: %s\n", itemNumber[i]);//item number display
 	System.out.printf("Number of units: %s\n", numUnits[i]); // number of units dipslay
 	System.out.printf("Price per unit: $%6.2f\n", priceUnits[i]);//price per unit display
 	
 	System.out.printf("Inventory value: $ %.2f\n", sumInventory[i]);//inventory value for that product
 	System.out.println();
	
	
 }// end display loop method 
 	
 	//display total inventory value
 	System.out.printf("Value of Total inventory: $%%.2f\n", ProductsPart2.getTotalValue());	
 	 
  
  	System.out.println();//blank line
 
	System.out.println("****End Of Program****");//end message

	}	//end class InventoryPart2			
	
}// end main

Here is the ProductPart2 class that contains the calculation method that is erroring out.

public class ProductsPart2 {

	private int itemNumber;//item number
	private String prodName;//product name
	private int numUnits;//number of units
	private double priceUnit;//price per unit
	public double inventory;//value of inventory
	
	
	// constructor
	public ProductsPart2(int itemNumber, String prodName, int numUnits, double priceUnit) 
		{
		this.itemNumber = itemNumber;
		this.prodName = prodName;
		this.numUnits = numUnits;
		this.priceUnit = priceUnit;
	}//end of constructor
	
	// calculate the value of inventory
 public	double calculateInventory() {
		 inventory = priceUnit * numUnits;
	 	return inventory;
	}//end calculate method

 	// get and return itemNumber
	public int getItemNumber() {
		return itemNumber;
	}

	//method to set itemNumber
	public void setItemNumber(int itemNumber) {
		this.itemNumber = itemNumber;
	}//end method to setitem number

	//get and retun prodName
	public String getProdName() {
		return prodName;
	}//end method getProdName

	//method to set prodName
	public void setProdName(String prodName) {
		this.prodName = prodName;
	}//end method set itemName

	//get and return numUnits
	public int getNumUnits() {
		return numUnits;
	}//end method getNumUnits

	//method to setnumunits
	public void setNumUnits(int numUnits) {
		this.numUnits = numUnits;
	}//end method setnumUnits

	//	 get return the price of units
	public double getPriceUnit() {
		return priceUnit;
	}//end method getPriceUnit

	//method to setPriceUnit
	public void setPriceUnit(double priceUnit) {
		this.priceUnit = priceUnit;
	}//end method setPriceUnit


	  // returns it as a string 
     public String toString() { 
        return String.format(" %03d %10s %3d $%6.2f $%7.2f", 
                    itemNumber, prodName, numUnits, priceUnit, calculateInventory()); 	
     }//ends tostring
   
   		
	//an attempt to get total out of the array don't work
	public double getTotalValue(){//method to get total value of all inventory
	
	double value = 0.00;//initialize total
		//loop through array
	for (int i = 0; i < 6; i++)
	{
	value += sumInventory[i];
	}//end for
		return value;
	}//end method to get total value of all 
 	
   //method to sort by name
   
  

}//end class Product

I'm getting several errors right now but I'm just worried about getting the totalValue working for now. I believe the culprit is in red and can't figure out why? It is supposed to run through the sumInventory array and add all the vales. then in the InventoryPart2 class it supposed to grab the totalValue variable to display it.

Any input good or bad is appreciated - Thanks

Recommended Answers

All 5 Replies

I believe the culprit is in red and can't figure out why?

don't just assume this. since you say the error says it can not find a certain variable. from the title I would say its a variable called msge, but I can't find a variable by that name, declared or used, in the code you posted here.
Could you copy paste the error message you receive here?

==> well, I took another quick look on your code.
you're right, that's where the problem is. you're trying to get data out of the array sumInventory, but you have no such array. you haven't declared one in the method, nor have you declared one in the class. also, you never use it. why do you want to get data out of an empty array, that you didn't even create yet? :)

That array is already created in InventoryPart2 and he just did not pass it as argument to ProdusctsPart2 getTotalValue() method...

since it wasn't passed on as an argument, I only checked the class the method was in. in the scope it could be read, it didn't exist :)

Somewhere in the process of coding OP made a mistake, the array should be either pass to getTotalValue() method, or it should be initialized inside ProductsPart2 class. It is up to OP to decide on final solution

Thanks for all your input I was able to correct the problem - it wa with passing the argument.

Thanks much! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.