By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,034 Members | 819 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,034 IT Pros & Developers. It's quick & easy.

Inventory Woes

MonolithTMA
P: 5
Greetings all,

I am new here, and aside from my introductory post, this will be my first.

I am working on an Inventory program for a class I am taking. I've searched the forums here and have seen other people working on the same or similar projects, but the problem I run into is that in Java, there seems to be more than one way to skin a cat. (No cats were harmed in the writing of this post.)

I am able to enter multiple items and their values, and I can get the total for each item. The part I am at now is trying to add the multiple items in my array together. I've read through several chapters on arrays in several books. I've scoured the web. yet it seems I can't find it. I know I'm probably missing something simple. Any help would be appreciated. I will include my code below.

This is the class that defines the data:

Expand|Select|Wrap|Line Numbers
  1. class CDData {
  2.     String[] cdName;
  3.     int[] cdQuantity;
  4.     int[] cdNumber;
  5.     double[] invTotalValue;
  6.     double T;
  7. }
  8.  
This is the Main class:

Expand|Select|Wrap|Line Numbers
  1. import java.util.*; // Initialize scanner for user input
  2.  
  3. public class CDInventory {
  4.  
  5.     /** Creates a new instance of CDInventory*/
  6.     public CDInventory() {
  7.     }
  8.  
  9.  
  10.     public static void main(String[] args) {
  11.  
  12.         CDData onlyCDData = new CDData();
  13.  
  14.         System.out.println(""); // blank line
  15.         System.out.println("\t************************");
  16.         System.out.println("\t** Audio CD Inventory **"); // Display Title
  17.         System.out.println("\t************************");
  18.         System.out.println(); // blank line
  19.  
  20.         // Begin Input Section
  21.  
  22.         /*Ask how many CDs to input. This will give us the itteration for our Array loop. */
  23.  
  24.         boolean stop = false;
  25.         while (!stop) {
  26.             Scanner input = new Scanner( System.in ); // Load Scanner for input
  27.             System.out.println("How many audio CDs would you like to enter today? Enter 0 to quit."); // Request amount of CDs to enter.
  28.  
  29.  
  30.             int count; // enteredAmount
  31.             count = input.nextInt(); // Input amount of CDs to enter.
  32.  
  33.             /* Check for 0 */
  34.  
  35.             if ( count == 0 ) {
  36.                 System.out.println("Thank you. The program has now ended");
  37.                 System.out.println(); // blank line
  38.                 stop = true;
  39.             } else {
  40.  
  41.                 /*The amount of CDs has been decided, now we begin to gather data from the user, specific to each CD. */
  42.  
  43.  
  44.                 for (int i=0; i < count; i++) {
  45.                     /*CD Name input. */
  46.  
  47.                     onlyCDData.cdName = new String[count];
  48.                     System.out.println(); // blank line
  49.                     System.out.println("Please input new CD Name:");
  50.                     System.out.println(); // blank line
  51.                     onlyCDData.cdName[i] = input.next(); // Input CD Name
  52.                     System.out.println(); // blank line
  53.  
  54.                     /*CD number input. */
  55.  
  56.                     onlyCDData.cdNumber = new int[count];
  57.                     System.out.println("Please input the CD number:");
  58.                     System.out.println(); // blank line
  59.                     onlyCDData.cdNumber[i] = input.nextInt(); // Input CD Number.
  60.  
  61.                     /*CD quantity input. */
  62.  
  63.                     onlyCDData.cdQuantity = new int[count];
  64.                     System.out.println(); // blank line
  65.                     System.out.println("Please input the CD quantity:");
  66.                     System.out.println(); // blank line
  67.                     onlyCDData.cdQuantity[i] = input.nextInt(); // Input CD quantity
  68.  
  69.                     /*CD quantity can't be a negative number. */
  70.  
  71.                     while (onlyCDData.cdQuantity[i] < 0) {
  72.                         System.out.println("Sorry, the quantity must be a positive number.");
  73.                         System.out.println("Please input the CD quantity:");
  74.                         System.out.println(); // blank line
  75.                         onlyCDData.cdQuantity[i] = input.nextInt(); // Input CD quantity
  76.                     }
  77.  
  78.                     /*CD price input. */
  79.  
  80.                     onlyCDData.cdPrice = new float[count];
  81.                     System.out.println(); // blank line
  82.                     System.out.println("Please enter the CD price:");
  83.                     System.out.println(); // blank line
  84.                     onlyCDData.cdPrice[i] = input.nextFloat(); // Input CD price
  85.                     System.out.println(); // blank line
  86.  
  87.                     /*CD price can't be a negative number or zero. */
  88.  
  89.                     while (onlyCDData.cdPrice[i] <= 0) {
  90.                         System.out.println("Sorry, the CD price must be a positive number.");
  91.                         System.out.println("Please enter the CD price:");
  92.                         System.out.println(); // blank line
  93.                         onlyCDData.cdPrice[i] = input.nextFloat(); // Input CD price
  94.                     }
  95.  
  96.                     // End Input Section
  97.  
  98.                     // Calculate and Dis-play
  99.                     onlyCDData.invTotalValue = new double[count];
  100.                     onlyCDData.invTotalValue[i] = (double) onlyCDData.cdQuantity[i] * onlyCDData.cdPrice[i];
  101.  
  102.                     System.out.println("- - - - - - - - - -");
  103.                     System.out.println(); // blank line
  104.                     System.out.printf( "CD Number " );
  105.                     System.out.print( onlyCDData.cdNumber[i] );
  106.                     System.out.printf( " is titled:" );
  107.                     System.out.printf( " " );
  108.                     System.out.printf( onlyCDData.cdName[i] );
  109.                     System.out.printf( ". " );
  110.                     System.out.print( "There are " );
  111.                     System.out.print( onlyCDData.cdQuantity[i] );
  112.                     System.out.printf( " " );
  113.                     System.out.printf( "In stock." );
  114.                     System.out.printf( " " );
  115.                     System.out.printf( onlyCDData.cdName[i] );
  116.                     System.out.printf( "'s total inventory value is: $%,.2f\n", onlyCDData.invTotalValue[i]);
  117.                     System.out.println(); // blank line
  118.  
  119.                     if(i == count - 1) {
  120.                         //This is where I want to add all the intTotalValue's together to make up T., but I have no idea what to do.
  121.                         //onlyCDData.T = All of the onlyCDData.invTotalValue added together.
  122.                         System.out.printf( "The total inventory value of all CDs combined is: $%,.2f\n", onlyCDData.T);
  123.                         System.out.println("Thank you. The program has now ended");
  124.                         System.exit(0);
  125.                     }
  126.                 }
  127.             }
  128.         }
  129.     }
  130. }
  131.  
Now, as it is written, it works, but the last two comments indicate the part I can't figure out. Again, any help would be greatly appreciated.

Thanks in advance!

Best regards,
Mike
Jan 28 '07 #1
Share this Question
Share on Google+
3 Replies


MonolithTMA
P: 5
In my zealousness to present clean, nice looking code, I managed to remove.

Expand|Select|Wrap|Line Numbers
  1. float[] cdPrice;
class CDData is actually:

Expand|Select|Wrap|Line Numbers
  1. class CDData {
  2.     String[] cdName;
  3.     int[] cdQuantity;
  4.     int[] cdNumber;
  5.     float[] cdPrice;
  6.     double[] invTotalValue;
  7.     double T;
  8. }
  9.  
Jan 28 '07 #2

Ganon11
Expert 2.5K+
P: 3,652
You may want to consider using an array of CDData objects. Instead of using arrays for each value of CDs, you could have each CDData object hold the value on one CD - its name, amount, price, etc. Then, when you needed to total up the values, you would just traverse your array of CDData objects and add the corresponding element.

If you have to use the CDData class as it is now, then simply start another loop (from 0 < count) and add that invTotalValue to your overall total.
Jan 28 '07 #3

MonolithTMA
P: 5
Thanks! I really apreciate your help.

I just added the line:

Expand|Select|Wrap|Line Numbers
  1. onlyCDData.T = (onlyCDData.T + onlyCDData.invTotalValue[i]);
and that did it!

I knew it was something simple.

So, I could make the entire class an array? That makes sense. Time to do some more reading now.

Thanks again!

Best regards,
Mike
Jan 28 '07 #4

Post your reply

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