0

私はこのJava割り当てを何時間も行っており、このテスタークラスをほぼ5時間続けています。

この割り当てでは、Productクラス、Moneyクラス、LineItemクラス、およびInventoryクラスを作成しました。次に、新しい広告申込情報を在庫配列に配置して、プログラムをテストするためのテストクラスを作成する必要があります。

テスタークラスでは、public static void addTestItems(Inventory theInventory)4つのアイテムを追加することを想定した静的メソッドを作成しようとしています。アイテムごとに、製品オブジェクトを作成してから、新しく作成された製品を含むLineItemオブジェクトを作成する必要があります。次に、在庫クラスのメソッドを使用して、在庫クラスの配列にアイテムを追加する必要があります。

私がこれまでに試したこと:

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); 
}

現在のエラーはincompatible types- found LineItem but expected Inventoryなので、に変更Inventory[] _items;してみましたLineItem[] _items;。しかし、エラーは変数でした_itemsは初期化されていない可能性があります。

申し訳ありませんが、私はJavaの真の初心者です。オンラインで年齢を検索してみましたが、ほとんどの結果がよくわかりません。私が理解しているのはhttp://forums.devshed.com/java-help-9/bluej-compiler-error-cannot-find-symbol-variable-object-688573.htmlだけでしたが、コンテキストに入れるのに疲れましたが失敗しました。また、多くの結果が見つかりましたが、コンストラクターとインスタンス変数が含まれており、先生はそれらは必要ないと具体的に述べています。

専門家が私の間違いを教えてくれるように私を導いてくれるのだろうか。どうもどうも。

在庫クラス:

/**
* 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;
   }
}

方法についてはまだコメントしていませんが、理解できたらコメントします。

4

3 に答える 3

2

配列は型Inventory[]ですが、型の参照を割り当てようとしていますLineItemまた、初期化していない。これを変える:

Inventory[] _items;

これに:

LineItem[] _items = new LineItem[5];

そして、すべてがうまくいくはずです-インデックス0を使用しておらず(そのため、サイズ5にする必要があります)、その後も配列に対して何もしていません...

配列を使用する別の方法は、リストを使用することです。

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));

...次に、変数で実際にをしたいのかを考えitemsます。

于 2012-04-19T21:11:13.480 に答える
0
LineItem[] _items = new LineItem[4];

その場合、インデックスは1からではなく0から始まります。

_items[4] 

indexoutofboundsエラーを返します

于 2012-04-19T21:13:44.167 に答える
0

いくつかのこと:

incompatible types- found LineItem but expected Inventory

これは、配列にInventoryオブジェクトが含まれているはずなのに、代わりにLineItemsを割り当てていることが原因です。

variable _items may not be initialise

_itemsオブジェクトはあるが、何にも初期化していないことを意味します。あなたがしたい

LineItem[] _items = new LineItem[4];

PS:動的なサイズの配列が必要な場合は、ロードする可能性のあるラインアイテムの数がわからないなど、それらのラインに沿ってベクトルやコレクションなどを使用します。

また、

_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); 

Javaでは、配列要素は1ではなく0で始まります。

_items

あなたのチームメイトがあなたのコーヒーにくしゃみをするようにする奇妙な変数名です

于 2012-04-19T21:14:17.500 に答える