473,398 Members | 2,525 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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.
Expand|Select|Wrap|Line Numbers
  1. //Class that represents products for Sale
  2. /*Tiffany Walker
  3. IT215 Java Programming
  4. October 6, 2011*/
  5.  
  6. public class Product
  7. {
  8.             //DECLARE VARIABLES
  9.     private String number;     //Item Number of product( used String as item 
  10.                    // numbers sometimes contain alphabet characters)
  11.     private    String artist;      //Name of the band or artist
  12.     private    int units;         //How many of the product are in stock
  13.     private    double price;      //How much one item costs
  14.     private double value;      //How much the stock is worth
  15.  
  16.  
  17.             //CONSTRUCTOR
  18.     public Product( String itemNumber, String artistName, int instockUnits, double costPerUnit, double costOfStock )
  19.     {
  20.         number = itemNumber;
  21.         artist = artistName;
  22.         units = instockUnits;
  23.         price = costPerUnit;
  24.         value = 0.00;
  25.     }
  26.  
  27.  
  28.             //METHODS OF THE CLASS
  29.     //Set the item number
  30.     public void setNumber( String itemNumber )
  31.     {
  32.         number = itemNumber;
  33.     }
  34.     //End set item number
  35.  
  36.  
  37.     //Get item number
  38.     public String getNumber()
  39.     {
  40.         return number;
  41.     }
  42.     //End get item number
  43.  
  44.  
  45.     //Set artist
  46.     public void setArtist( String artistName )
  47.     {
  48.         artist = artistName;
  49.     }
  50.     //End set artist
  51.  
  52.     //Get artist
  53.     public String getArtist()
  54.     {
  55.         return artist;
  56.     }
  57.     //End get artist
  58.  
  59.     //set units
  60.     public void setUnits( int instockUnits )
  61.     {
  62.         units = instockUnits;
  63.     }
  64.     //end set units
  65.  
  66.  
  67.     //Get units
  68.     public int getUnits()
  69.     {
  70.         return units;
  71.     } 
  72.     //End get units
  73.  
  74.  
  75.     //Set price
  76.     public void setPrice( double costPerUnit )
  77.     {
  78.         price = costPerUnit;
  79.     }
  80.     //End set price
  81.  
  82.  
  83.     //Get price
  84.     public double getPrice()
  85.     {
  86.         return price;
  87.     }
  88.     //End get price
  89.  
  90.  
  91.  
  92.     //Calculate Value
  93.     public double value( double price, int units )
  94.     {
  95.         return value = price * units;
  96.     }
  97.     //End calculate value
  98.  
  99.     //String output of Product
  100.     public String toString()
  101.     {
  102.         return "Product " + number + "  " + artist + "'s CD.  Cost..." + price + ".   Units..." + units + ".   Value..." + (price * units) + ".";
  103.     }
  104.  
  105. }
And my loop
Expand|Select|Wrap|Line Numbers
  1. for ( int counter = 0; counter < inventory.length; counter++ )
  2.         {
  3.             System.out.print( "What is the product number of the CD? ");
  4.             String theNumber = input.next();
  5.             CD.setNumber( theNumber );
  6.             System.out.println();
  7.  
  8.             System.out.print( "What is the name of the Artist? ");
  9.             String theArtist = input.next();
  10.             CD.setArtist( theArtist );
  11.             System.out.println();
  12.  
  13.             System.out.print( "How many of this CD are in stock? ");
  14.             int theUnits = input.nextInt();
  15.             CD.setUnits( theUnits );
  16.             System.out.println();
  17.  
  18.             System.out.print( "How much does this CD cost? ");
  19.             double thePrice = input.nextDouble();
  20.             CD.setPrice( thePrice );
  21.             System.out.println();
  22.  
  23.             CD.value( CD.getPrice(), CD.getUnits() );
  24.  
  25.             inventory[ counter ] = new Product( theNumber, theArtist, theUnits, thePrice, CD.value( CD.getPrice(), CD.getUnits() ) );
  26.         }
  27.  
  28.         for ( int counter = 0; counter < inventory.length; counter++ )
  29.         {    
  30.             total += CD.value( CD.getPrice(), CD.getUnits() );
  31.             System.out.println( inventory[counter ] );
  32.         }    
  33.  
  34.         System.out.printf( "The total value of the inventory is %.2f\n\n", total );
  35.  
Oct 14 '11 #1

✓ answered by Totally Stumped

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

This properly adds the value through the array!

12 2240
Rabbit
12,516 Expert Mod 8TB
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.
Oct 14 '11 #2
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.
Oct 14 '11 #3
Rabbit
12,516 Expert Mod 8TB
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.
Oct 14 '11 #4
This is the changed code.
Expand|Select|Wrap|Line Numbers
  1. //Using inventory.length as a counter, loop input and output
  2.         for ( int counter = 0; counter < inventory.length; counter++ )
  3.         {
  4.             System.out.print( "What is the product number of the CD? ");
  5.             String theNumber = input.next();
  6.             CD.setNumber( theNumber );
  7.             System.out.println();
  8.  
  9.             System.out.print( "What is the name of the Artist? ");
  10.             String theArtist = input.next();
  11.             CD.setArtist( theArtist );
  12.             System.out.println();
  13.  
  14.             System.out.print( "How many of this CD are in stock? ");
  15.             int theUnits = input.nextInt();
  16.             CD.setUnits( theUnits );
  17.             System.out.println();
  18.  
  19.             System.out.print( "How much does this CD cost? ");
  20.             double thePrice = input.nextDouble();
  21.             CD.setPrice( thePrice );
  22.             System.out.println();
  23.  
  24.             CD.value( CD.getPrice(), CD.getUnits() );
  25.  
  26.                      inventory[ counter ] = new Product( theNumber, theArtist,     
  27.  
  28.            theUnits, thePrice, CD.value( 
  29.                CD.getPrice(), CD.getUnits() ) );
  30.  
  31.             total =+ CD.value( CD.getPrice(), CD.getUnits() );
  32.              }
  33.  
  34.              for ( int counter = 0; counter < inventory.length; counter++ )
  35.              {    
  36.  
  37.                      System.out.println( inventory[counter ] );
  38.             }    
  39.  
  40.         System.out.printf( "The total value of the inventory is %.2f\n\n",     
  41.  
  42.        total );
  43.  
Oct 14 '11 #5
Rabbit
12,516 Expert Mod 8TB
Shouldn't that be += and not =+?
Oct 14 '11 #6
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...
Oct 14 '11 #7
Rabbit
12,516 Expert Mod 8TB
Have you tried outputting each of those inputs to make sure they're getting in there correctly?
Oct 14 '11 #8
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.
Oct 14 '11 #9
Rabbit
12,516 Expert Mod 8TB
I don't see anything in your code that actually outputs the values. Are you sure you're not just seeing the inputs?
Oct 14 '11 #10
Expand|Select|Wrap|Line Numbers
  1. for ( int counter = 0; counter < inventory.length; counter++ )
  2.              {    
  3.  
  4.                       System.out.println( inventory[counter ] );
  5.              }    
  6.  
  7.  
  8.  
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.
Oct 14 '11 #11
Got the answer.
total += inventory[counter].value( CD.getPrice(), CD.getUnits());

This properly adds the value through the array!
Oct 15 '11 #12
Rabbit
12,516 Expert Mod 8TB
Glad you got it working. You should mark your post as the answer.
Oct 15 '11 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: ercwebdev | last post by:
Can anyone tell me why this For loop isn't working (see below) The error message is says: Error: 'document.emailForm.FNAME_CHILD' is null or not an object Thanks in advance, Alan R
8
by: Drew | last post by:
I am building an application for keeping track of user permissions here at work. I have built the interfaces, and am now working on the processing page for inserting to the database. I am having...
3
by: Matt | last post by:
I am in the process of creating a custom .NET object (this is my first one). I have created a few classes and some of them are collections. When I try to iterate through a collection I receive the...
12
by: upernikaw | last post by:
Hello, I am attempting to create a nested loop (in Access 2003/VB) that will print a report for a set of user defined months inputed on a form and that will print out for every Client. So the first...
8
by: jimmy | last post by:
I am working on a project which tracks 'bad' words in IE and im using a For loop to check for an array of words in he address bar. I have included the broken code below. Any pointers on why it isnt...
3
by: tamarindm | last post by:
I need to play a .wav file over and over again. I am using the following code. private static extern bool PlaySound( string lpszName, int hModule, int dwFlags ); public int SND_ASYNC =...
2
by: Dipti Singh | last post by:
Hi, I have two asp pages. when both contain lots of coding, loops etc. if i run the first page that contain for loop for creating array of textboxes, then this page run successfully. but if...
18
by: Prosdo | last post by:
Hey Everyone. I have a program for school that is suppose to project how many seconds it takes a projectile to hit the ground. I have the formula, but the loop is not posting to my output file. Can...
4
pdring
by: pdring | last post by:
Hi everyone, I have had a look through the site but can't find anything to help with a little problem i am experiencing with a college assignment. I wonder if anyone would like to help out? My...
1
by: dwcolt | last post by:
Help! I'm hoping that someone can point me to a VB script that will do a fairly simple task. I have a Microsoft Access table that lists the full directory path for a number of files. I am...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.