473,395 Members | 1,870 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,395 software developers and data experts.

Inventory Woes

MonolithTMA
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
3 1662
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
3,652 Expert 2GB
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
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

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

Similar topics

5
by: gregork | last post by:
I have painstakingly created an excel 2000 workbook for the very complex recipes I have to formulate. I have 2 sheets- 1 for configuring the recipe and 1 that is like an inventory of all the raw...
13
by: royaltiger | last post by:
I am trying to copy the inventory database in Building Access Applications by John L Viescas but when i try to run the database i get an error in the orders form when i click on the allocate...
109
by: zaidalin79 | last post by:
I have a java class that goes for another week or so, and I am going to fail if I can't figure out this simple program. I can't get anything to compile to at least get a few points... Here are the...
0
by: south622 | last post by:
I'm taking a beginning Java course and I'm stuck in week eight of a nine week course. If anyone could help me I would greatly appreciate it. This assignment was due yesterday and each day I go past...
9
by: xxplod | last post by:
I am suppose to modify the Inventory Program so the application can handle multiple items. Use an array to store the items. The output should display the information one product at a time, including...
3
by: cblank | last post by:
I need some help if someone could help me. I know everyone is asking for help in java. But for some reason I'm the same as everyone else when it comes to programming in java. I have an inventory...
2
by: pinkf24 | last post by:
I cannot figure out how to add the following: Modify the Inventory Program to include an Add button, a Delete button, and a Modify button on the GUI. These buttons should allow the user to perform...
1
by: jcato77 | last post by:
I need help with a class project I'm working on, Below is my assignment and the code I have currently created. Assignment: Modify the Inventory Program by creating a subclass of the product class...
3
by: 100grand | last post by:
Modify the Inventory Program to use a GUI. The GUI should display the information one product at a time, including the item number, the name of the product, the number of units in stock, the price...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.