| | |
arrays
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Apr 2005
Posts: 8
Reputation:
Solved Threads: 0
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
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
for an array of any object, just do:
I'm pretty sure that's how u do that.
Java Syntax (Toggle Plain Text)
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
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
![]() |
Similar Threads
- (reformatted) How to return Multi-Dimensional Arrays (C++)
- What relation does **indirection operator have with Multidimensional Arrays (C++)
- Arrays (C++)
- How to Return Multidimensional Arrays (C++)
- C file input/output 2D arrays. (C)
- passing arrays in visual basic (Visual Basic 4 / 5 / 6)
Other Threads in the Java Forum
- Previous Thread: How to "seperate" numbers from String?
- Next Thread: basic java GUI problem
Views: 1728 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for Java
add android api apple applet application arguments array arrays automation bank binary bluetooth chat chooser class classes client code component converter database digit draw eclipse equation error event exception file fractal functiontesting game givemetehcodez graphics gui health helpwithhomework html hyper ide idea image infinite input int integer j2me java javame javaprojects jmf jni jpanel julia linux list loop main map method methods mobile myregfun netbeans newbie nonstatic number object oracle pattern pearl print problem program programming project recursion scanner screen scrollbar server set size sms socket sort spamblocker sql sqlserver string superclass swing test thread threads time transfer tree windows





