-
Need help adding an object to an ArrayList
My problem is this; when i add a first StockItems to and arraylist it works, but when i add an additional one the StockItems already on the arraylist shelf change to the new parameters. i want it to print out (testOne testTwo) but test one gets updated and it becomes (testTwo testTwo). I tested it and highly doubt its a problem with printing it out. the objects already on the shelf get changed.
public class StoreCompiler {
public static void main(String[] args)
{
GroceryStore store = new GroceryStore();
store.runStore();
}
}
public class GroceryStore
{
Inventory inv = new Inventory();
private String command = ":";
private String searchWhat;
//Starts the store
public GroceryStore()
{
}
public void runStore()
{
System.out.println("'stock' allows you to enter a new item");
System.out.println("'displayAll' shows everything " +
"on the shelf");
enterCommand();
}
public void enterCommand()
{
Scanner sc = new Scanner(System.in);
command = sc.next();
if(command.equals("stock"))
stock();
if(command.equals("displayAll"))
displayAll();
else
runStore();
}
public void stock()
{
inv.newItem();
enterCommand();
}
public void displayAll()
{
inv.listAll();
enterCommand();
}
}
import java.util.*;
public class Inventory
{
private ArrayList <StockItems> shelf;
private String search = null;
private String restockWhat = null;
//Constructs a shelf on which the items are held and the
//first item.
public Inventory()
{
shelf = new ArrayList<StockItems>();
System.out.println("Welcome");
}
//helper method for all new items, creates a new item.
private StockItems getItem()
{
Scanner sc = new Scanner(System.in);
System.out.println("name: ");
String des = sc.next();
System.out.println("id: ");
int id = sc.nextInt();
System.out.println("price: ");
double price = sc.nextDouble();
System.out.println("how many? ");
int howMany = sc.nextInt();
return new StockItems(des, id, price, howMany);
}
//this is used to add new items.
public void newItem()
{
StockItems item = getItem();
for(int x = 0; x < item.returnAmountWanted(); x++)
shelf.add(item);
}
//This shows all of the items on the shelf.
public void listAll()
{
for(StockItems item: shelf)
{
System.out.print(item.returnDes() + " " +
item.returnId() + " " + item.returnPrice()
+ " " + item.returnAmountWanted() + ". ");
}
}
import java.util.*;
//This program creates and item with a name, id, price, and
//connects to the Inventory class telling it how many of
//one item to add.
public class StockItems
{
private static String description;
private static int id;
private static int howMany;
private static double price;
//Main constructor to assemble new StockItems.
public StockItems(String des, int idNum, double prc, int hwMny)
{
initialize(des, idNum, prc, hwMny);
}
private void initialize(String des, int idNum, double prc, int hwMny)
{
description = des;
id = idNum;
price = prc;
howMany = hwMny;
}
//this returns the description to the inventory.
public String returnDes()
{
return description;
}
//this returns the price to the inventory.
public double returnPrice()
{
return price;
}
//this tells the inventory how much to initially add
public int returnAmountWanted()
{
return howMany;
}
//returns the id number.
public int returnId()
{
return id;
}
//to string method
public String toString()
{
return "The item " + description + " has the id " +
id + " and the price is $" + price + ". you " +
"put " + howMany + " on the shelf.";
}
}
Similar Threads
-
Replies: 7
Last Post: 10-24-2002, 06:33 AM
-
Replies: 1
Last Post: 09-17-2002, 05:44 PM
-
By scottish mike in forum .NET
Replies: 0
Last Post: 08-08-2002, 05:56 AM
-
By Derek Mooney in forum .NET
Replies: 94
Last Post: 10-29-2001, 08:44 PM
-
Replies: 2
Last Post: 12-12-2000, 10:41 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks