我做了几个小时该Java分配和坚持这一测试类非常近5个小时。
在这个作业,我创建了一个产品类别,Money类,一个LineItem类和库存类。 现在我需要创建一个测试类通过将新进入了LineItem库存阵列测试程序。
在测试类,我想创建一个静态方法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;
}
}
我还没有在方法上还没有发表评论,但会做这样一旦我理解这一点。