arrays

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2005
Posts: 8
Reputation: sydneyrustle is an unknown quantity at this point 
Solved Threads: 0
sydneyrustle sydneyrustle is offline Offline
Newbie Poster

arrays

 
0
  #1
May 19th, 2005
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[i].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[i] == null)
{
it[i] = it[i+1];
it[i+1] = null;
}
}
}

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


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

cheers

syd
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 350
Reputation: Ghost is an unknown quantity at this point 
Solved Threads: 2
Ghost's Avatar
Ghost Ghost is offline Offline
Posting Whiz

Re: arrays

 
0
  #2
May 19th, 2005
for an array of any object, just do:

  1. Item arrayOfItems [] = new Item();

I'm pretty sure that's how u do that.
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 30
Reputation: yassar is an unknown quantity at this point 
Solved Threads: 0
yassar's Avatar
yassar yassar is offline Offline
Light Poster

Re: arrays

 
0
  #3
May 20th, 2005
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1728 | Replies: 2
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC