我的任务是创建一个控制库存、删除、删除和更新项目的程序。我已经编写了 StockItem 类和 Stocklist 类,但是我被困在 StockListTester 上。当我删除和添加学生时,我没有收到任何错误,但是每当我尝试打印我的“listA”时,我都会得到输出:StockList@6345e044。
有谁知道这是为什么,如果是,如何解决?
我还没有在 StockList 中设置我的格式方法,因为我不确定在方法体中放什么,这可能与此有关吗?
任何帮助将不胜感激,我的代码可以在下面找到。谢谢
库存清单代码:
import java.util.*;
public class StockList
{
public static LinkedList<StockItem> stock
= new LinkedList<StockItem>();
public StockList() {};
// Adds item to end of stock list
public void addItem(StockItem item)
{
StockList.stock.addLast(item);
}
// Removes item identified by productID from stock list
public void deleteItem(String itemID)
{
ListIterator itr = stock.listIterator();
while(itr.hasNext())
{
StockItem item = (StockItem)itr.next();
if(item.getitemID().equals(itemID))
{
itr.remove();
break;
}
}
}
// Updates price of existing item
public void updateItemPrice(String itemID, double price)
{
ListIterator itr = stock.listIterator();
while(itr.hasNext())
{
StockItem item = (StockItem)itr.next();
if(item.getitemID().equals(itemID))
{
item.setprice(price);
break;
}
}
}
// Updates quantity of existing item
public void updateItemQuantity(String itemID, int quantity)
{
ListIterator itr = stock.listIterator();
while(itr.hasNext())
{
StockItem item = (StockItem)itr.next();
if(item.getitemID().equals(itemID))
{
item.setquantity(quantity);
break;
}
}
}
// Updates re-order level of existing item
public void updateReOrderLevel(String itemID,
int reOrderLevel)
{
ListIterator itr = stock.listIterator();
while(itr.hasNext())
{
StockItem item = (StockItem)itr.next();
if(item.getitemID().equals(itemID))
{
item.setreOrderLevel(reOrderLevel);
break;
}
}
}
// Returns formatted representation of the stock list
// public String formatStockList()
// {…}
// Returns formatted representation of re-order list
// Items are on this list if quantity < reOrderLevel
// public String formatReOrderList()
// {…}
}
StockListTester 代码:
import java.util.*;
public class StockListTester {
public static void main (String[] args)
{
StockItem item1 = new StockItem ("MBP", "Macbook Pro", 1300.0, 200, 400);
StockItem item2 = new StockItem ("TBHD", "TB Hardrive", 40.0, 400, 100);
StockItem item3 = new StockItem ("W8P", "Windows 8 Premium", 75.99, 5000, 0 );
//creating 3 new items
System.out.println("STOCKLIST TESTER");
System.out.println("Test 1");
System.out.println("Methods tested: addItem, constructor");
System.out.println("***************************************\n");
StockList listA = new StockList();
listA.addItem(item1);
listA.addItem(item2);
listA.addItem(item3);
//adding my 3 items to a newly created StockList
System.out.println(listA + "\n");
System.out.println("Test 2");
System.out.println("Methods tested: removeItem");
System.out.println("***************************************\n");
listA.deleteItem(item1.getitemID());
System.out.println("Expected:");
System.out.println("item 2 TBHD TB Hardrive 40.0 400 100 ");
System.out.println("item 3 W8P Windows 8 Premium 75.99 5000 0 \n");
System.out.println("Actual:");
System.out.println(listA);