Re: Inventory Program
Newsgroups trimmed.
Warning: longish posting.
dh7taurus@yahoo.com wrote:
Hello All,
I have written the app to the best of my ability and would like you to
look it over and let me know if I am at least on the right track and
give any help you deem necessary.
Here is the program:
Now that your deadline has passed I'll offer you some thoughts which you
can take into account for part 2.
//InventoryProgram1.java
A better use of comments is to briefly describe the purpose of the
program. What will people be able to achieve by using it?
Why are you writing this program? These are the sorts of questions that
could usefully be answered by comments.
//import java.util.Scanner; // allow for input
import java.util.Arrays;
import java.util.Scanner;
import java.lang.String;
class Product {
String Name;
int itemNumber;
int numberofunits;
It is best to use standard Java naming conventions for variables,
capitalise the first letter of every word in a compound word, apart from
the first.
int numberOfUnits;
It helps us in reading your code and it helps you avoid accidentally
creating two variables like itemNumber and itemnumber.
int priceperunit;
public Product (String name, int itemNumber, int numberofunits,
int priceperunit); {
String Name = name;
this.itemNumber = itemNumber;
this.numberofunits = units;
You don't have a "units" variable. The compiler should tell you this. If
you can't work out what the compiler error messages mean, post them
here. But try to keep postings short.
this.priceperunit = price;
computeValueInventory ();
}
private void computeValueInventory () {
Value = Units * price;
names beginning with a capital letter should be classes, here you are
using them as if they were variable names. You don't have a class or
variable named Value or value.
It is most likely to be better to have computeValueInventory return a
value rather than have it alter the value of a field (an instance
variable). The reason is that the reader of the code doesn't have to
read the method definition when looking at a call to it, the assignment
of the return value should make it obvious what variables are affected.
Since you are calculating monetary values you should discuss with your
teacher what data types are appropriate for holding those values and
performing calculations. In particular, float and double are not a good
choice as they usually don't hold exact values for decimal numbers.
}
}
public class InventoryProgram1 //declares public class inventory
It is a bad idea to write comments that merely paraphrase the code. In
this case the comment says class "inventory" but this contradicts
"InventoryProgram1" in the code. This sort of comment can cause
confusion rather than help avoid it.
{
public static void main(String args []) //starts the program
{
PRODUCT[] = new Product[3];
Names in all capitals are conventionally used for constants, not for
ordinary classes. To declare a variable you need to specify a variable
name after the type and before the equals sign.
Product[] products = new Product[3];
product[0] = new PRODUCT(1, "StApler",45, 2,
17.99 );//
declares a new item
// number, name
// quantity, and price
Those comments would be best placed before the declaration of the
constructor for Product.
product[1] = new PRODUCT(2, "Calanders", 47, 4,
12.99 );
product[2] = new PRODUCT(3, "Toner", 49, 6, 19.99 );
//added changes
I don't know if this comment helps you. It mystifies me.
What was changed, why was it changed? Did the change achieve the objective?
Inventory1 x = new Inventory1();
Maybe I missed it but I don't see a class Inventory1 defined anywhere.
If you intend to create it later, don't try to write code that uses it.
It will just make it harder to get your program working. Follow
Patricia's advice, write a tiny program, get it to compile, get it to
produce the output you expect and only then add another small feature.
Probably you meant InventoryProgram1.
x.sortPRODUCT(product);
x isn't a great variable name.
A better method name would be sortProduct.
Consistent and systematic capitalisation would avoid an error you made
here: sortPRODUCT isn't SortPRODUCT.
}// end main
public void SortPRODUCT(PRODUCT[] the PRODUCT) {
Java has built in methods for sorting arrays. Unless the purpose of this
exercise is to learn about simple sorting algorithms, you'd be better
off learning how to use Java's sorting methods. I'd use Google to search
for "Java sort tutorial" and hope to find a Sun tutorial in the search
results.
for (int index = 0; index < the PRODUCT.size - 1;
"the"?
index++) {
String s1 = thePRODUCT[index].getProductName
();
String s2 = thePRODUCT[index] +
1].getProductName
();
The compiler should tell you about the extra ] there. The trouble with
writing such a large mass of code without compiling smaller versions of
it is that you must end up with a huge and daunting list of error
messages. Plus the compiler may be struggling to make sense of later
code due to earlier syntax errors. This makes it much harder to get this
code working than the approach recommended by Patricia. Get something
smaller to compile cleanly, then add a tiny amount of code, get that to
compile cleanly, repeat. In your shoes I'd recompile every time I added
a line of code, I'd then concentrate on getting that line to compile
cleanly.
if (comparewords(s1, s2)) {
PRODUCT temp = thePRODUCT[index];
thePRODUCT[index] = thePRODUCT
[index + 1];
thePRODUCT[index + 1] = temp;
index = -1;
}
}
}
private boolean comparewords(String s1, String s2) {
Java has built in methods for comparing and sorting Strings. You don't
need to examine individual characters yourself.
boolean islarger = false;
for (int index = 0; index < s1.length(); index++) {
if (index < s2.length()) {
if (s1.toLowerCase().charAT
(index) > s2.toLowerCase().charAT(index)) {
islarger = true;
break;
{
if (s1.toLowerCase().charAT
(index) < s2.toLowerCase().charAT(index) {
break;
{
}else {
return true;
}
}
return islarger;
}
//end of changes
} // end class Inventory1
class PRODUCT // declares class inventory
Another case where the comments just add confusion. The code would be
clearer without those comments.
{
private int itemName;// declares item number as in
private String productTitle;// declares product title as
string
private int onHand; // declares on hand as int
private double productCost;// declares cost as double
public PRODUCT(int stockNumber, String title, int
inStock,
double price) // constructor
{
itemNumber = stockNumber;// intitiates stock
number, title, instock, and
// price
productTitle = title;
onHand = inStock;
I'd choose either onHand or inStock as a variable name and use that one
name consistently.
productCost = price;
That's a misleading use of variable names.
} //
// set PRODUCT StockNumber //sets stock number
public void seItemNumber(int stockNumber)
{
itemNumber = stockNumber;
}
public int getItemNumber() // class item number
{
return itemNumber; // returns item number
} // end method get PRODUCT Item
public void setProductTitle(String title) // set PRODUCT
Title
{
productTitle = title;
}
public String getProductTitle() // gets and returns PRODUCT
Title
{
return productTitle;
}
public void setOnHand(int inStock) // Set on hand
{
onHand = inStock;
}
public int getOnHand() // gets and returns on hand
{
return onHand;
} //
public void setProductCost(double price) // sets product cost
{
productCost = price;
}
public double getProductCost() // gets and returns PRODUCT
cost
{
return productCost;
}
public double value() // calculate the value of stock
{
return productCost * onHand;
}
} // end class Product
It is supposed to handle multiple items, use an array to store the
items, display the information one product at a time, including the
item number, the name of product, the number of units in stock, the
price of each, and the value of the inventory of that product. It
should also display the value of the entire inventory.
If it were a 16 oz steak you wouldn't try to swallow it whole, you'd cut
off small pieces. Writing programs is best attempted in small pieces.
--
RGB