2012-04-19 6 views
0

私はこのJava割り当てを何時間もやっていて、このテスタークラスで非常にほぼ5時間立ち往生しています。Java:静的メソッドでオブジェクトを作成し、別のクラスからメソッドを呼び出す方法を教えてください。

この課題では、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ので、私はLineItem[] _items;Inventory[] _items;を変えてみました。しかし、エラーは可変であり、項目は初期化されていない可能性があります。

申し訳ありませんが、私はJavaで本当のnoobです、私は年齢のオンライン検索を試みたが、私はほとんどの結果を理解していません。私が理解している唯一の人は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; 
    } 
} 

私は、メソッドにコメントをまだ持っていますが、私はそれを理解すればそうなります。

+0

でそれをくしゃみなりグラグラ変数名です*は初期化されていません。配列への参照を宣言しますが、決して配列を作成しないでください。実際の型については、型の階層はわからないので、推測するだけです。 –

+0

または実際に私はインベントリクラスの配列を宣言しました。それが審理されるかどうかはわかりません。理解を深めるためにここに階層をどのように表示しますか? – Panda

答えて

2

のタイプはInventory[]ですが、LineItemの参照を割り当てようとしています。 また、は初期化していません。これに

Inventory[] _items; 

: - あなたは(あなたはそれがサイズ5である必要が理由です)、インデックス0を使用していない、あなたはしているものの

LineItem[] _items = new LineItem[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)); 

...次にあなたが実際に欲しいものについて考えてみましょうdoitemsという変数があります。

incompatible types- found LineItem but expected Inventory 

そのあなたに起因する:インデックスは0からではない1から始まり

+0

こんにちは... '_items [1] = new LineItem(product1,5、unitPrice1);で何をしているのか正確に教えてもらえますか?私は手動で配列にLineItemを追加していますか?なぜなら私はInventoryクラスのメソッドを使う必要があると言われたからです。もし私が言うことが正しければ...私は論理を再考する必要があります。 – Panda

+0

@Panda: 'LineItem'の新しいインスタンスを作成し、配列の要素1を新しいインスタンスへの参照に設定します。 'Inventory'クラスがそれを見ずにここに関与する方法を知るのは難しいです... –

+0

こんにちは...すみません、私はインベントリクラスを追加しました。主に私は 'public void addItem(LineItem item)を参照するだけです。 { _items [_numItems] = item; _numItems ++; } 'あまりにも長い場合は申し訳ありません – Panda

0
LineItem[] _items = new LineItem[4]; 

その後、

_items[4] 

0

いくつかのことがindexoutofboundsエラーが返されます配列にはInventoryオブジェクトが含まれているはずですが、代わりにLineItemを割り当てています

variable _items may not be initialise 

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

LineItem[] _items = new LineItem[4]; 

PSをしたい:あなたは、動的にサイズの配列をしたい場合、あなたが潜在的に、ベクターまたはコレクションまたはそれらの線に沿って何かを使用するなどなど、ロードますどのように多くの行の項目を知りません。 Javaでは

また、

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

、配列の要素は、インデックス0で始まり、いない1

_items 

あなたのチームメイトは、あなたのコーヒー

+0

私は0から始まるインデックスについて忘れています...リマインダーのおかげで。 – Panda

+0

このエラーを説明していただきありがとうございます。 – Panda

関連する問題