2012-04-19 68 views
0

我正在做這個Java任務幾個小時,並堅持這個測試人員類爲非常近5個小時。Java:我如何在靜態方法中創建對象並調用另一個類的方法?

在此作業中,我創建了Product類,Money類,LineItem類和Inventory類。現在我需要創建一個測試類,通過將新的lineitems放入庫存數組來測試該程序。

在測試儀類中,我試圖創建一個靜態方法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中的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

它*未初始化;你聲明一個對數組的引用,但是不要創建一個數組。至於實際的類型,我們不知道你的類型層次結構,所以我們只是猜測。 – 2012-04-19 21:10:40

+0

或實際上我在庫存類中聲明瞭該數組。但不確定是否可以參考。如何在這裏顯示層次結構以便更好地理解? – Panda 2012-04-19 21:12:16

回答

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變量。

+0

嗨...如果你不介意你能告訴我,我正在做什麼'_items [1] = new LineItem(product1,5,unitPrice1);'?我手動添加LineItem到數組?因爲我被告知我需要使用庫存類中的方法。如果我說的是對的......我需要重新思考邏輯。 – Panda 2012-04-19 21:16:10

+0

@Panda:您正在創建一個新的'LineItem'實例,並將該數組的元素1設置爲對新實例的引用。很難知道'庫存'類如何在沒有看到它的情況下涉及到... – 2012-04-19 21:19:46

+0

嗨...很抱歉,我剛剛添加了庫存類。但主要是我只是指'public void addItem(LineItem item) {_items [_numItems] = item; _numItems ++; }'很抱歉,如果它太長了 – Panda 2012-04-19 21:24:13

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

那麼指數從0不是從1開始,

_items[4] 

將返回indexoutofbounds錯誤

0

有幾件事情:

incompatible types- found LineItem but expected Inventory 

是由事實造成的數組應該包含Inventory對象,但您將LineItem分配給它,而不是

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中,數組元素開始索引爲0而不是1

_items 

是讓你的隊友在你的咖啡打噴嚏一個靠不住的變量名

+0

噢,我忘記了從0開始的索引...感謝提醒。 – Panda 2012-04-19 21:25:36

+0

感謝您解釋錯誤。 – Panda 2012-04-19 21:25:49

相關問題