hey guys

i'm hopeless with java can someone help me out ??

i am using three classes for my program
the following two and a ClientUI class

this is the menu in my client

  1. Add a new item to the collection
  2. Load some items into the collection
  3. Display list of items in the collection
  4. Find an item in the collection
  5. Display found items name
  6. Display 'value' of found item
  7. Sell an item
  8. Display item details
  9. Total value of stock items
  10. Display items to reorder
  11. Delete an item
  12. quit

i can't figure out how to do the stuff in bold.
everything else seems to work.

[SIZE=1]class Item
{
    private String name;
    private int quantity;
    private double price;
    double itemValue;
public Item(String n, int q, double p)
{
    name = new String(n);
    quantity = q;
    price = p;
}
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;
}       
[B]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 itemVal)
{
    itemVal = quantity * price;
    return itemVal;
}[/B]
public String toString(String n, int q, double p)
{
    String a = "\nItem Name: " + getName() + "\nQuantity: " + getQuantity()+"\nPrice: " + getPrice() + "\n";
    return a;
}
}[/SIZE]



[SIZE=1]class StockTake
{
    private static int DEFAULTSIZE = 50;
    private static int MAXITEMS;
    private  int length=0;
    private Item[] it;
    static String name;
    static int quantity;
    static double price;
    static String itemName;
private void init()
  {
    it = new Item[MAXITEMS];
    length = 0;
  }
public StockTake()
  {
    MAXITEMS = 12;
    init();
  }
public StockTake( int length )
  {
    if( length <= 0 )
      length = DEFAULTSIZE;
    MAXITEMS = length;
    init();
  }
public boolean addItem(String n, int q, double p)
{
    if (length == MAXITEMS)
        {
        return false;
        }
    else
        {   
        it[length] = new Item(n, q, p);
        length++;
        return true;
        }
}
public String findItem( String itemName )
  {
    int i = 0;
    while( i < length )
    {
      if( it[i].getName().equals(itemName) )
      itemName = it[i].getName();
      i++; 
      return (itemName);   
    }
    return null; 
  }
public boolean deleteItem(String itemName)
{   
    int i = 0;
    int index = -1;
    while( i < length )
     {
      if( it[i].getName().equals(itemName) )
            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 toString()
{
    String o = "";
    for (int i=0; i < length; i++)
        o += "\nItem Name: " + it[i].getName() + "\nQuantity: " +it[i].getQuantity()+"\nPrice: " + it[i].getPrice() + "\n";
    return o;   
}
public double foundItemVal(double itemVal)
{
  {
    int i = 0;
    while( i < length )
    {
      if( it[i].getName().equals(itemName) )
      return (itemVal);     
      i++;   
    }
    return '0'; 
  }
}
}[/SIZE]

Edited 3 Years Ago by happygeek: fixed formatting

Display the value of a found item:

  1. search for the item

    Item itemFound = null;
    for(int i =0; i < length;i++)
    {
    if( it.getName().equals(nameToFind) )
    {
    itemFound = it;
    }
    }

    if(itemFound == null)
    System.out.println("Not found!");
    else
    System.out.println( "Found it! Its price is: " + itemFound.getPrice() );

  2. Sell an item

Im not sure why you have a loop in your sell() method (see below):

public int sell(int sellQuant)
{
while (quantity>0)
{
quantity = quantity - sellQuant;
if (sellQuant > quantity)
System.out.print("Order exceeds stock limit.");
}

If im correct it should be:

public int sell(int sellQuant)
{
quantity = quantity - sellQuant;
}
  1. Total value of stock items

    public int getTotalValue()
    {
    int totalValue=0;
    for(int i =0; i < length;i++)
    {
    totalValue += it.getPrice();
    }

    return totalValue;
    }

  2. Display items to reorder

    public void displayItemsToReorder()
    {
    for(int i =0; i < length;i++)
    {
    if( it.getQuantity() <= 0 )
    {
    System.out.println(it.getName());
    }
    }

    return totalValue;
    }

Edited 3 Years Ago by happygeek: fixed formatting

thanks for the help.
Don't surpose you could tell me why this won't work....

public String findName()
{
String nameToFind;
String find = findItem(nameToFind);
if (nameToFind == find)
{
System.out.println("item found");
nameToFind = found;
}
else
{
System.out.println("item not found");
nameToFind = null;
}
}


public String foundItemVal()
{
String nameToFind = findName( String nameToFind);
Item itemFound = null;
int i;
for( i =0; i < length;i++)
{
if( it.getName().equals(nameToFind) )
{
itemFound = it;
}
}


if(itemFound == null)
return ("Not found!");
else
return ( "Found it! Its total price is: " + it.getPrice() * it.getQuantity() );


}

Edited 3 Years Ago by happygeek: fixed formatting

The problem is in this code:

public String findName()
{
String nameToFind;
String find = findItem(nameToFind);
if (nameToFind == find)
{
System.out.println("item found");
nameToFind = found;
}
else
{
System.out.println("item not found");
nameToFind = null;
}
}

You are writing (nameToFind == find). This does not compare to strings for equality. It compare two objects to see if they are one in the same object (same memory location). You must write nameToFind.equals(find).

For more help, visit my site www.NeedProgrammingHelp.com or email me at NeedProgrammingHelp@hotmail.com

This article has been dead for over six months. Start a new discussion instead.