0

I am doing this Java assignment for hours and stuck with this tester class for very almost 5 hours.

In this assignment, I have created a Product class, a Money class, a LineItem class and an Inventory class. Now i need to create a test class to test the program by putting new lineitems into the inventory array.

In the tester class, I am trying to create a static method public static void addTestItems(Inventory theInventory) which suppose to add 4 items. For each item I will need to create a product object followed by a LineItem object to contain the newly created product. next i need to use a method from the inventory class to add the items into the array in the inventory class.

What i have tried too so far:

private static void addTestItems(Inventory theInventory)
{
    Inventory[] _items;
    Product product1 = new Product("Book","Objects first with Java"," An excellent introductory Java textbook");
    Product product2 = new Product("CD","The dark side of the moon","The all-time classic Pink Floyd album");
    Product product3 = new Product("DVD", "Transformers","Robots in disguise");
    Product product4 = new Product("Laptop","Lenovo T42","A good yet affordabble laptop");
    Money unitPrice1 = new Money(29,99);
    Money unitPrice2 = new Money(4,99);
    Money unitPrice3 = new Money(9,99);
    Money unitPrice4 = new Money(450,0);
    _items[0] = new LineItem(product1,5,unitPrice1);
    _items[1] = new LineItem(product2,8,unitPrice2);
    _items[2] = new LineItem(product3,200,unitPrice3);
    _items[3] = new LineItem(product4,9,unitPrice4); 
}

The current error is incompatible types- found LineItem but expected Inventory so i tried changing Inventory[] _items; to LineItem[] _items;. But the error was variable _items may not be initialise.

Sorry guys I am a real noob in Java, I tried searching on-line for ages but I do not quite understand most results. The only one i understand was http://forums.devshed.com/java-help-9/bluej-compiler-error-cannot-find-symbol-variable-object-688573.html but i tired putting into my context but failed. I also found lot of results but they had constructors and instance variables in them which my teacher specifically mentioned that I will not need them.

Wonder if expert could guide me along like let me know my mistakes. Thanks thanks.

The inventory class:

/**
* In the Inventory class, it is merely to create a list / array of product which allows    the information from the linitem to be put with an index.
* For example, for the first product, we can use the inventory class to input it into the index 1. and he next product into index 2 and so on.
 * It is suse to create an array and inputing the lineitem information into it.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
 public class Inventory
{
// instance variables - replace the example below with your own
private LineItem[] _items;
private int _numItems;


/**
 * Constructor for objects of class Inventory
 */
public Inventory()
{
    // initialise instance variables
    _items = new LineItem[1000];
    _numItems = 0;
}

/**
 * An example of a method - replace this comment with your own
 * 
 * @param  y   a sample parameter for a method
 * @return     the sum of x and y 
 */
public void addItem(LineItem item)
{
   _items[_numItems]= item;
   _numItems++;
}

public String toString()
{
    String result="";
    int i=0;
    while (i < _numItems)
    {
        result = result + _items[i] + "/n";
        i++;
    }
    return result;
}

public void print()
{
    String myResult=this.toString();
    System.out.println(myResult);
}

public Money getTotalValue()
{
    int i=0;
    Money total= new Money(0);
    while (i<_items.length)
    {
        total = total.add(Money.NO_MONEY);
        i++;
    }
    return total;
}

public LineItem getItem(String productName)
{
    int i = 0;
    LineItem itemDetails = null;
    while (i<_items.length)
    {
        if (_items[i].equals(productName))
        {
            itemDetails= _items[i];
        }
        else
        {
            //do nothing
        }
        i++;
    }
    return itemDetails;
   }
}

I have yet to comment on the methods yet but will do so once i understand it.

2
  • It isn't initialized; you declare a reference to an array, but never create an array. As to the actual types, we don't know your type hierarchy, so we'd just be guessing. Commented Apr 19, 2012 at 21:10
  • or actually i declared the array in the inventory class. but not sure if it can be refereed over. How do i show hierarchy here for better understanding? Commented Apr 19, 2012 at 21:12

3 Answers 3

2

Your array is of type Inventory[] - but you're trying to assign references of type LineItem. You're also not initializing it. Change this:

Inventory[] _items;

to this:

LineItem[] _items = new LineItem[5];

And all should be well - although you're not using index 0 (which is why you need it to be size 5) and you're not doing anything with the array afterwards either...

Another alternative to using an array is to use a List:

List<LineItem> items = new ArrayList<LineItem>();
items.add(new LineItem(product1, 5, unitPrice1));
items.add(new LineItem(product2, 8, unitPrice2));
items.add(new LineItem(product3, 200, unitPrice3));
items.add(new LineItem(product4, 9, unitPrice4));

... next think about what you actually want to do with the items variable.

Sign up to request clarification or add additional context in comments.

5 Comments

hi...if you don't mind could you tell me what exactly am i doing by _items[1] = new LineItem(product1,5,unitPrice1);?? am i manually adding the LineItem to the array? because i was told that i need to use a method from the Inventory class. If what i say was right...I will need to rethink of the logic.
@Panda: You're creating a new instance of LineItem, and setting element 1 of the array to be a reference to the new instance. It's hard to know how the Inventory class gets involved here without seeing it...
hi...so sorry i just added the inventory class. but mainly i ill be only referring to public void addItem(LineItem item) { _items[_numItems]= item; _numItems++; } so sorry if its too long
ooh. can i use the items in the addItem() method from the inventory class??
sorry...but may i know why when i add Inventory addItem(items); , it is not allowed. kind to point out my mistake?
0
LineItem[] _items = new LineItem[4];

then the index starts from 0 not from 1,

_items[4] 

will return indexoutofbounds error

Comments

0

A few things:

incompatible types- found LineItem but expected Inventory

is caused by the fact that your array is supposed to contain Inventory objects but you're assigning LineItems to it instead

variable _items may not be initialise

means that you have your _items object but you haven't initialized it to anything. You want to do

LineItem[] _items = new LineItem[4];

PS: If you want dynamically sized arrays, don't know how many line items you'll potentially load, etc etc use a vector or a collection or something along those lines.

Also,

_items[1] = new LineItem(product1,5,unitPrice1);
_items[2] = new LineItem(product2,8,unitPrice2);
_items[3] = new LineItem(product3,200,unitPrice3);
_items[4] = new LineItem(product4,9,unitPrice4); 

In Java, array elements start with index 0 and not 1

_items

is a wonky variable name that makes your team mates sneeze in your coffee

1 Comment

ooh yah i forgot about the index starting from 0...thanks for reminder.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.