Reputation: 271
I have been tasked with creating a programe that controls stock, removing, deleting, and updating items. I've written the StockItem class, and the Stocklist class, however I am getting stuck on the StockListTester. When I remove and add students, I am getting no errors, however whenever i try and print my "listA", I get the output: StockList@6345e044.
Does anybody know why this is, and if so, how to resolve it?
I have not yet set my format methods in StockList, as i am not sure what to put inside the method body, could it be something to do with that?
Any help would be much appreciated, my code can be found below. Thanks
StockList code:
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 code:
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);
Upvotes: 1
Views: 128
Reputation: 49362
Override the toString() method in your class StockList
and StockItem
. In StockList
, you can do like :
public String toString() {
return Arrays.toString(stock.toArray());
}
Upvotes: 2
Reputation: 2631
At the moment you get default .toString() method from object wich doesn't display any useful information for you. You can override it so it represents your object better.
You should consider using a test-framework to test your classes as well.
Upvotes: 0
Reputation: 3794
You need to override default toString()
method. In toString()
specify how you want to display your object state.
Upvotes: 0
Reputation: 19284
You should override toString()
method to print whatever you want.
If you dont do it, you use the default Object.toString()
method which print it that way.
Upvotes: 2