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
- class CDData {
- String[] cdName;
- int[] cdQuantity;
- int[] cdNumber;
- double[] invTotalValue;
- double T;
- }
Expand|Select|Wrap|Line Numbers
- import java.util.*; // Initialize scanner for user input
- public class CDInventory {
- /** Creates a new instance of CDInventory*/
- public CDInventory() {
- }
- public static void main(String[] args) {
- CDData onlyCDData = new CDData();
- System.out.println(""); // blank line
- System.out.println("\t************************");
- System.out.println("\t** Audio CD Inventory **"); // Display Title
- System.out.println("\t************************");
- System.out.println(); // blank line
- // Begin Input Section
- /*Ask how many CDs to input. This will give us the itteration for our Array loop. */
- boolean stop = false;
- while (!stop) {
- Scanner input = new Scanner( System.in ); // Load Scanner for input
- System.out.println("How many audio CDs would you like to enter today? Enter 0 to quit."); // Request amount of CDs to enter.
- int count; // enteredAmount
- count = input.nextInt(); // Input amount of CDs to enter.
- /* Check for 0 */
- if ( count == 0 ) {
- System.out.println("Thank you. The program has now ended");
- System.out.println(); // blank line
- stop = true;
- } else {
- /*The amount of CDs has been decided, now we begin to gather data from the user, specific to each CD. */
- for (int i=0; i < count; i++) {
- /*CD Name input. */
- onlyCDData.cdName = new String[count];
- System.out.println(); // blank line
- System.out.println("Please input new CD Name:");
- System.out.println(); // blank line
- onlyCDData.cdName[i] = input.next(); // Input CD Name
- System.out.println(); // blank line
- /*CD number input. */
- onlyCDData.cdNumber = new int[count];
- System.out.println("Please input the CD number:");
- System.out.println(); // blank line
- onlyCDData.cdNumber[i] = input.nextInt(); // Input CD Number.
- /*CD quantity input. */
- onlyCDData.cdQuantity = new int[count];
- System.out.println(); // blank line
- System.out.println("Please input the CD quantity:");
- System.out.println(); // blank line
- onlyCDData.cdQuantity[i] = input.nextInt(); // Input CD quantity
- /*CD quantity can't be a negative number. */
- while (onlyCDData.cdQuantity[i] < 0) {
- System.out.println("Sorry, the quantity must be a positive number.");
- System.out.println("Please input the CD quantity:");
- System.out.println(); // blank line
- onlyCDData.cdQuantity[i] = input.nextInt(); // Input CD quantity
- }
- /*CD price input. */
- onlyCDData.cdPrice = new float[count];
- System.out.println(); // blank line
- System.out.println("Please enter the CD price:");
- System.out.println(); // blank line
- onlyCDData.cdPrice[i] = input.nextFloat(); // Input CD price
- System.out.println(); // blank line
- /*CD price can't be a negative number or zero. */
- while (onlyCDData.cdPrice[i] <= 0) {
- System.out.println("Sorry, the CD price must be a positive number.");
- System.out.println("Please enter the CD price:");
- System.out.println(); // blank line
- onlyCDData.cdPrice[i] = input.nextFloat(); // Input CD price
- }
- // End Input Section
- // Calculate and Dis-play
- onlyCDData.invTotalValue = new double[count];
- onlyCDData.invTotalValue[i] = (double) onlyCDData.cdQuantity[i] * onlyCDData.cdPrice[i];
- System.out.println("- - - - - - - - - -");
- System.out.println(); // blank line
- System.out.printf( "CD Number " );
- System.out.print( onlyCDData.cdNumber[i] );
- System.out.printf( " is titled:" );
- System.out.printf( " " );
- System.out.printf( onlyCDData.cdName[i] );
- System.out.printf( ". " );
- System.out.print( "There are " );
- System.out.print( onlyCDData.cdQuantity[i] );
- System.out.printf( " " );
- System.out.printf( "In stock." );
- System.out.printf( " " );
- System.out.printf( onlyCDData.cdName[i] );
- System.out.printf( "'s total inventory value is: $%,.2f\n", onlyCDData.invTotalValue[i]);
- System.out.println(); // blank line
- if(i == count - 1) {
- //This is where I want to add all the intTotalValue's together to make up T., but I have no idea what to do.
- //onlyCDData.T = All of the onlyCDData.invTotalValue added together.
- System.out.printf( "The total inventory value of all CDs combined is: $%,.2f\n", onlyCDData.T);
- System.out.println("Thank you. The program has now ended");
- System.exit(0);
- }
- }
- }
- }
- }
- }
Thanks in advance!
Best regards,
Mike