Warning

 

Close

Confirm Action

Are you sure you wish to do this?

Confirm Cancel
Member Login

Posted: 1/23/2008 4:47:44 AM EST
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
documented.

So, I wrote the following code and turned it in for a passing grade. It compiles and performs the functions required above.

// SFInventoryPt1.java
// Inventory keeper for torque tools


import java.util.Scanner; // program uses class Scanner

class ProductInfo // class to store product information
{
ProductInfo(int itemNumber, String itemName, int stockQty, double priceEach) // constructor for product info
{
num = itemNumber;
name = itemName;
stock = stockQty;
price = priceEach;
}

public int getNum()
{
return num;
}
public String getName()
{
return name;
}
public int getStock()
{
return stock;
}
public double getPrice()
{
return price;
}
public double getInventoryTotal()
{
return price * stock;
}

private int num; // product's item number
private String name; // product's item name
private int stock; // number of item in stock
private double price; // price each of item

} // end class ProductInfo

class SFInventoryPt1
{
private String ProductInfo; // call class product info

// main method begins execution of Java application
public static void main ( String args[] )
{
//create Scanner to obtain input from command window
Scanner input = new Scanner( System.in );

int num; // product's item number
int stock; // number of items in stock
double price; // price each of item
ProductInfo product; // product information instance

System.out.println(); // blank line

System.out.print( "Enter the item name of the product, or STOP to quit: "); // prompt
String name = input.nextLine(); // read item name from user or quit

// loop until sentinel value read from user
while ( !name.equalsIgnoreCase ("stop") )
{

System.out.print( "Enter the item number of the product: "); // prompt
num = input.nextInt(); // read item number from user

while ( num <=0 ) //loop until item number is positive
{
System.out.println ("Item number must be positive. Please re-enter item number: ");//prompt user to re-enter item number
num = input.nextInt(); // read item number

} //end while

System.out.print( "Enter the quantity of product in stock: "); // prompt
stock = input.nextInt(); // read stock quantity from user

while ( stock <0 ) //loop until stock is positive
{
System.out.println ("Quantity in stock can not be less than zero. Please re-enter the quantity in stock: ");//prompt user to re-enter quantity in stock
stock = input.nextInt(); // read stock quantity from user

} //end while

System.out.print( "Enter the price of the product: "); // prompt
price = input.nextDouble(); // read item price from user

while ( price <=0 ) //loop until price is positive
{
System.out.println ("Product price must be positive. Please re-enter the price of the product: ");//prompt user to re-enter product price
price = input.nextDouble(); // read item price from user

} //end while

product = new ProductInfo( num, name, stock, price); // initialize ProductInfo variables

System.out.println(); // blank line

System.out.printf( "Item Name: %S\n", product.getName());
System.out.printf( "Item Number: %s\n", product.getNum());
System.out.printf( "Qty. in Stock: %s\n", product.getStock());
System.out.printf( "Price Each: $%.2f\n", product.getPrice());
System.out.printf( "Total Value in Stock: $%.2f\n", product.getInventoryTotal());

System.out.println(); // blank line

System.out.print( "Enter the item name of the product, or STOP to quit: "); // prompt
name = "";
while ( name.equals("") )
{
name = input.nextLine(); // read new product name from user or quit
}

} //end while

System.out.println(); // blank line

System.out.println("Good Bye!"); // exit message


} // end method main

} // end class SFInventoryPt1


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!!!
Link Posted: 1/23/2008 6:32:59 AM EST
[Last Edit: 1/23/2008 6:40:27 AM EST by BamaTrapper]
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.
Link Posted: 1/23/2008 6:54:56 AM EST
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?

Thanks again!
Link Posted: 1/23/2008 7:01:19 AM EST
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[10]; // 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
Link Posted: 1/23/2008 9:20:54 AM EST
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?
Top Top