473,700 Members | 2,528 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inventory Woes

5 New Member
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. }
This is the Main class:

Expand|Select|Wrap|Line Numbers
  1. import java.util.*; // Initialize scanner for user input
  3. public class CDInventory {
  5.     /** Creates a new instance of CDInventory*/
  6.     public CDInventory() {
  7.     }
  10.     public static void main(String[] args) {
  12.         CDData onlyCDData = new CDData();
  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
  20.         // Begin Input Section
  22.         /*Ask how many CDs to input. This will give us the itteration for our Array loop. */
  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.
  30.             int count; // enteredAmount
  31.             count = input.nextInt(); // Input amount of CDs to enter.
  33.             /* Check for 0 */
  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 {
  41.                 /*The amount of CDs has been decided, now we begin to gather data from the user, specific to each CD. */
  44.                 for (int i=0; i < count; i++) {
  45.                     /*CD Name input. */
  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
  54.                     /*CD number input. */
  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.
  61.                     /*CD quantity input. */
  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
  69.                     /*CD quantity can't be a negative number. */
  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.                     }
  78.                     /*CD price input. */
  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
  87.                     /*CD price can't be a negative number or zero. */
  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.                     }
  96.                     // End Input Section
  98.                     // Calculate and Dis-play
  99.                     onlyCDData.invTotalValue = new double[count];
  100.                     onlyCDData.invTotalValue[i] = (double) onlyCDData.cdQuantity[i] * onlyCDData.cdPrice[i];
  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
  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. }
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,
Jan 28 '07 #1
3 1679
5 New Member
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. }
Jan 28 '07 #2
3,652 Recognized Expert Specialist
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
5 New Member
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,
Jan 28 '07 #4

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

Similar topics

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 materials and their specifications. I have many lookup formulas on sheet1 that lookup the specs on the inventory. The problem is sheet 1 works really well but sheet 2 is not really performing as an inventory database like I want it to. The...
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 button "Unexpected Error":3251 operation is not supported for this type of object.The demo cd has two databases, one is called inventory and the other just has the tables for the design called inventory data. When you run inventory the database works...
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 assignments... 4. CheckPoint: Inventory Program Part 1 • Resource: Java: How to Program • Due Date: Day 5 forum • Choose a product that lends itself to an inventory (for example, products at your workplace, office supplies, music CDs, DVD...
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 the due date 10% of the grade is taken off. I think I'm coming down with the flu and my brain is just not processing this assignment. Here is the assignment: Modify the Inventory program by adding a button to the GUI that allows the user to move...
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 the item number, the name of the product, the number of units in stock, the price of each unit, and the value of the inventory of that product. In addition, the output should display the value of the entire inventory. • Create a method to calculate...
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 program that I have built so far with no problem. But I cannot figure out how to turn it into a GUI application. This is what I'm trying to get it to do. Modify the Inventory Program to use a GUI. The GUI should display the information one product...
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 the corresponding actions on the item name, the number of units in stock, and the price of each unit. An item added to the inventory should have an item number one more than the previous last item. Add a Save button to the GUI that saves the...
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 that uses one additional unique feature of the product you chose (for the DVDs subclass, you could use movie title, for example). In the subclass, create a method to calculate the value of the inventory of a product with the same name as the method...
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 of each unit, and the value of the inventory of that product. In addition, the GUI should display the value of the entire inventory, the additional attribute, and the restocking fee. Here is my Inventory program from 1 to 3: package...
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.