My loop isn't working

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Totally Stumped
    New Member
    • Oct 2011
    • 13

    My loop isn't working

    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.
    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) + ".";
    	}
    	
    }
    And my loop
    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 );
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    What is this CD variable? You only have one CD variable so how do you expect to get a sum of all the records when you only store one at any time?

    Move the total into the prior loop and it should total fine. But you will still only be storing one CD at a time.

    Comment

    • Totally Stumped
      New Member
      • Oct 2011
      • 13

      #3
      CD is an object made from Product class. Inventory[] is filled with CD's. I tried moving the total calculation to the other loop, but it didn't work.

      If you were trying to add data in array together, how would you go about it. Perhaps I'm tackling the problem the wrong way.

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        The method is fine. It's just that you're using the single CD to get a total of an array. You haven't linked it to the inventory array in any way.

        You should either total each item in the inventory array or set the CD to a single item in the inventory array before getting the value.

        Post the code that you used when you moved the calculation to the other loop, that should have worked.

        Comment

        • Totally Stumped
          New Member
          • Oct 2011
          • 13

          #5
          This is the changed code.
          Code:
          //Using inventory.length as a counter, loop input and output
          		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() ) );
          			
          			total =+ CD.value( CD.getPrice(), CD.getUnits() );
                   	}
            
                   	for ( int counter = 0; counter < inventory.length; counter++ )
                   	{    
                       		
                       		System.out.println( inventory[counter ] );
                  	}    
          				
          		System.out.printf( "The total value of the inventory is %.2f\n\n", 	
          
          	   total );

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            Shouldn't that be += and not =+?

            Comment

            • Totally Stumped
              New Member
              • Oct 2011
              • 13

              #7
              It wasn't working either way, but I figured try each. I've exhausted anything I thought was the problem. I'm just trying anything at this point. Desperation...

              Comment

              • Rabbit
                Recognized Expert MVP
                • Jan 2007
                • 12517

                #8
                Have you tried outputting each of those inputs to make sure they're getting in there correctly?

                Comment

                • Totally Stumped
                  New Member
                  • Oct 2011
                  • 13

                  #9
                  When I run the code, I get an output that shows the product#, name, price, amount, and value for each product in the array. The value show up and are correct. But when I try to add the values together (using the various versions of code I tried) either I get 0.00, or the only the last value calculated, or the last value times the array.length.

                  Comment

                  • Rabbit
                    Recognized Expert MVP
                    • Jan 2007
                    • 12517

                    #10
                    I don't see anything in your code that actually outputs the values. Are you sure you're not just seeing the inputs?

                    Comment

                    • Totally Stumped
                      New Member
                      • Oct 2011
                      • 13

                      #11
                      Code:
                      for ( int counter = 0; counter < inventory.length; counter++ )
                                   {    
                        
                                            System.out.println( inventory[counter ] );
                                   }
                      After the user inputs the data which is added to the array, this code shows on the screen a summation of the data. The method that controls how that data is displayed in found in the Product class.

                      Comment

                      • Totally Stumped
                        New Member
                        • Oct 2011
                        • 13

                        #12
                        Got the answer.
                        total += inventory[counter].value( CD.getPrice(), CD.getUnits());

                        This properly adds the value through the array!

                        Comment

                        • Rabbit
                          Recognized Expert MVP
                          • Jan 2007
                          • 12517

                          #13
                          Glad you got it working. You should mark your post as the answer.

                          Comment

                          Working...