I'm making an inventory program for class. It has a loop that should cycle through the array and print the information. Part of the loop is supposed to add the value from each inventory item together to get the total value of the inventory. However, instead the program simply adds the last value entered to itself repeatedly. There is no compiler error, but the program isn't doing what it should.
Here is my code for the Product class.
And my loop
Here is my code for the Product class.
Code:
//Class that represents products for Sale
/*Tiffany Walker
IT215 Java Programming
October 6, 2011*/
public class Product
{
//DECLARE VARIABLES
private String number; //Item Number of product( used String as item
// numbers sometimes contain alphabet characters)
private String artist; //Name of the band or artist
private int units; //How many of the product are in stock
private double price; //How much one item costs
private double value; //How much the stock is worth
//CONSTRUCTOR
public Product( String itemNumber, String artistName, int instockUnits, double costPerUnit, double costOfStock )
{
number = itemNumber;
artist = artistName;
units = instockUnits;
price = costPerUnit;
value = 0.00;
}
//METHODS OF THE CLASS
//Set the item number
public void setNumber( String itemNumber )
{
number = itemNumber;
}
//End set item number
//Get item number
public String getNumber()
{
return number;
}
//End get item number
//Set artist
public void setArtist( String artistName )
{
artist = artistName;
}
//End set artist
//Get artist
public String getArtist()
{
return artist;
}
//End get artist
//set units
public void setUnits( int instockUnits )
{
units = instockUnits;
}
//end set units
//Get units
public int getUnits()
{
return units;
}
//End get units
//Set price
public void setPrice( double costPerUnit )
{
price = costPerUnit;
}
//End set price
//Get price
public double getPrice()
{
return price;
}
//End get price
//Calculate Value
public double value( double price, int units )
{
return value = price * units;
}
//End calculate value
//String output of Product
public String toString()
{
return "Product " + number + " " + artist + "'s CD. Cost..." + price + ". Units..." + units + ". Value..." + (price * units) + ".";
}
}
Code:
for ( int counter = 0; counter < inventory.length; counter++ )
{
System.out.print( "What is the product number of the CD? ");
String theNumber = input.next();
CD.setNumber( theNumber );
System.out.println();
System.out.print( "What is the name of the Artist? ");
String theArtist = input.next();
CD.setArtist( theArtist );
System.out.println();
System.out.print( "How many of this CD are in stock? ");
int theUnits = input.nextInt();
CD.setUnits( theUnits );
System.out.println();
System.out.print( "How much does this CD cost? ");
double thePrice = input.nextDouble();
CD.setPrice( thePrice );
System.out.println();
CD.value( CD.getPrice(), CD.getUnits() );
inventory[ counter ] = new Product( theNumber, theArtist, theUnits, thePrice, CD.value( CD.getPrice(), CD.getUnits() ) );
}
for ( int counter = 0; counter < inventory.length; counter++ )
{
total += CD.value( CD.getPrice(), CD.getUnits() );
System.out.println( inventory[counter ] );
}
System.out.printf( "The total value of the inventory is %.2f\n\n", total );
Comment