私はこの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;
}
}
私は、メソッドにコメントをまだ持っていますが、私はそれを理解すればそうなります。
でそれをくしゃみなりグラグラ変数名です*は初期化されていません。配列への参照を宣言しますが、決して配列を作成しないでください。実際の型については、型の階層はわからないので、推測するだけです。 –
または実際に私はインベントリクラスの配列を宣言しました。それが審理されるかどうかはわかりません。理解を深めるためにここに階層をどのように表示しますか? – Panda