I could really use some help with a Java programming class that I am taking online. If you know Java then you are probably aware of the (infamous) inventory program that many classes have you do. For those not familiar, this is the first part of the assignment:
Choose a product that lends itself to an inventory (for example, products at your
workplace, office supplies, music CDs, DVD movies, or software).
Create a product class that holds the item number, the name of the product, the number
of units in stock, and the price of each unit.
Create a Java application that displays the product number, the name of the product, the
number of units in stock, the price of each unit, and the value of the inventory (the
number of units in stock multiplied by the price of each unit). Pay attention to the good
programming practices in the text to ensure your source code is readable and well
So, I wrote the following code and turned it in for a passing grade. It compiles and performs the functions required above.
This is where my trouble starts. I now need to modify the above code to meet these assignment requirements:
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 the value of the entire inventory.
Create another method to sort the array items by the name of the product.
I am at a total loss at this point and don’t even know where to start. The book is useless and doesn’t cover using arrays with multiple variables (String, int, double, etc,). The instructor is useless and has provided no guidance and even admitted that the text doesn’t cover what we are working on. I have searched the intertubes and found multiple instances of others working on this as well. But, I just need some one-on-one guidance on how to get started with an array class and how to get the user input from my SFInventoryPt1 class stored in the array.
I could really use some help on this. I’m not looking for someone to my homework for me. I’m looking for someone that could explain to me how I get to the assignment requirements with what I currently have. Thanks for looking!!!
I'll start you out by providing a few links.
This is the only book you need:
The Java Tutorial
How to use Java Arrays
Congratulations. Java was my first love.
Work through the tutorial. If you have specific questions, feel free to contact me directly.
Hint: You can use an array of Object, and store an Object of type Integer (rather than a primitive int), as well as Strings, etc. If you're using 5.0, autoboxing will smooth the process. Also remember to solve the problem on paper using psuedo code before you bother to touch the keyboard. Know excatly how the app will work and flow before you code anything. Good luck, and don't stress.
Thanks Bama! I'll look through the tutorials and see if they shed any light. I appreciate the guidance.
I figured that I could use the user input as an object and insert it in to the array. I'll just work through it from that angle. I also agree that mapping it out in pseudocode is the way to start. I'm stubborn, though, and sometimes just jump in then realize I need to back up to the pseudocode. You know?
Tell me, how does the code that I have written look? Is it a good base to get to the requirements of the assignment or is there a better approach?
Short Answer: Create an Array of type ProductInfo. After your code prints out the little summary, add "product" to the Array.
ProductInfo inventory = new ProductInfo; // Creates an empty Array to hold up to 10 ProductInfo items.
int index = 0;
// Do your whole input item create thing
inventory[index] = product; // adds item of type ProductInfo to the array
++index; //increments your counter
Now, not clear on exactly what your prof told you to use. Arrays are generally set to a max size when created. They are not flexible. The Java class Vector is wonderful in that instead of specifying a max size or adding to it with a specific index, you can just call and "add" method.
Vector inventory = new Vector();
//Do your existing code
inventory.add(product); //Stores your object of type ProductInfo.
To retrieve data from a Vector, you must typecast it (Vectors store Objects, you can store many different types in teh same Vector).
System.out.println( (ProductInfo)(inventory.get(1)).getName() ); //Pulls object at index 1 from the Vector, tells Java that is is of type ProductInfo, then prints out the Name.
Another class to consider is ArrayList. At work we prefer ArrayList, I'll leave that to you to lookup
Thanks MWP80918, the short answer really just simplifies it for me so I can see where to go. I think that I was thinking too much in to this and stressing out.
So, I'm just going to create an empty array to store the ProductInfo object that I create in my ProductInfo method, correct? Then, in my main I'm going to send that object (containing the num, name, stock and price) to the next open field of the array.
I'm thinking that from there, as you suggest, have the summary print and loop through having the user continue to input until they enter the sentinel (STOP). Then access the array and print out all elements of the array and exit.
Am I getting this or heading in the wrong direction from what you've suggested?