hi all,

just looking for some help with setting up an array of items for a stocktake program.

i have an item class that works, and now i need to create an array to hold an array of the items. the item class code is

class Item
{


private String name;
private int quantity;
private double price;


public Item()
{
String name = " ";
int quantity = 0;
double price = 0.0;
}


public Item(String name)
{
name = name;
}


public String getName()
{
return name;
}


public int getQuantity()
{
return quantity;
}


public double getPrice()
{
return price;
}


public void setName(String name)
{this.name = name;
}


public void setPrice(double price)
{
this.price = price;
}


public int sell(int sellQuant)
{
while (quantity>0)
{
quantity = quantity - sellQuant;


if (sellQuant > quantity)
System.out.print("Order exceeds stock limit.");
}
return quantity;
}


public int order(int newQuant)
{
quantity += newQuant;
return quantity;
}


public double value(double itemValue)
{
itemValue = quantity * price;
return itemValue;
}


public String toString()
{
return name + "\n" + quantity + "\n" + price;
}
}


i've started the stocktake class but i havn't gotten very far. so far i've got this



class StockTake
{
private static final int MAXSIZE = 10;
private  int length=0;
private Item[] it;
static String item;


public StockTake()
{
it = new Item[MAXSIZE];
}


public boolean addItem(String Item)
{
if (length == MAXSIZE)
{
return false;
}
else
{
it[length] = new Item(item);
length++;
return true;
}
}


public boolean deleteItem(String item)
{
int i = 0;
int index = -1;
while( i < length )
{
if( it.getName().equals(item) )
index = i;
i++;
}
if (index >=0)
{
it[index] = null;
length--;
rePack();
return true;
}
else
return false;
}


private void rePack()
{
for (int i=0; i <length; i++)
{
if (it == null)
{
it = it[i+1];
it[i+1] = null;
}
}
}


public String findItem( String item )
{
int i = 0;
while( i < length )
{
if( it.getName().equals(item) )
return (it.getName() + " found\n");
i++;
}
return null;
}
}

can anyone tell me if any of this makes any sense????

cheers

syd

Recommended Answers

All 2 Replies

for an array of any object, just do:

Item arrayOfItems [] = new Item();

I'm pretty sure that's how u do that.

Maybe you should use ArrayList class rather than using arrays.

You do not need to specify a maximum element number. Also ArrayList class does the shifting things itself when you remove an element or add an element to a specific index.

Also if you use j2se 1.5.0 you can define your ArrayList specified to Item objects

Like: List<Item> itemList = new ArrayList<Item>();

İf you use an older version Just define:

ArrayList itemList = new ArrayList(); // But in this, you should consider typecasting

ArrayList is in "java.util" package

Also the methods of this class might be useful too for your further operations

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.