Problem statement: Design a system for the following scenario:
1.An item list contains item code, name, rate, and quantity for several items.
Whenever a new item is added in the list uniqueness of item code is to be checked. Register a new product with its price.
Time to time rate of the items may change.
Whenever an item is issued or received existence of the item is checked and quantity is updated.
In case of issue, availability of quantity is also to be checked.
User may also like to know price/quantity available for an item.
Find how many items cost more than a given amount. The amount will be a parameter.
Remember that the methods have to return an error code if for example an invalid item code is given
For the above problem i have created a Item class with following members:
private String name;
private double rate;
private long code;
private int quantity;
public Item()
public Item(String name, double rate, long code, int quantity)
public Item(Item item)
public String toString()
public String getName()
public void setName(String name)
public double getRate()
public void setRate(double rate)
public long getCode()
public void setCode(long code)
public int getQuantity()
public void setQuantity(int quantity)
Now I have created a Shop class to access the Item class, to do all operations on a Item... Here is a part of the shop class.
private ArrayList<Item> ItemList;
private Iterator<Item> itr;
public Shop() {
System.out.println("New Shop for Items created.");
ItemList = new ArrayList<Item>();
itr= ItemList.iterator();
}
public Item search(long code) {
Item item;
while(itr.hasNext()) {
item = new Item(itr.next());
if (item.getCode() == code) {
return item;
}
}
return null;
}
public void addItem() throws InputMisMatchEXception{
long aCode;
String aName;
double aRate;
int aQuantity;
Item foundItem;
System.out.println("Enter Item code:");
aCode = sc.nextLong();
foundItem = search(aCode);
if (foundItem == null) {
System.out.println("Item name : ");
aName = sc.next();
System.out.println("Rate : ");
aRate = sc.nextDouble();
System.out.println("Quantity : ");
aQuantity = sc.nextInt();
Item aItem = new Item(aName, aRate, aCode, aQuantity);
ItemList.add(aItem);
} else if (foundItem != null) {
System.out.println("Item exists");
}
}
Now when i am adding a new item, it works.. But When I add a second element it throws a
ConcurrentModificationException
Here is a sample output:
New Shop for Items created.
*
-----ITEM------
1. Add items to list
2. Update item list
3. Issue item
4. Display item details
5. Exit
Choice: 1
Enter Item code: 123
Item name : qwerty
Rate : 12345
Quantity : 100
1. Add items to list
2. Update item list
3. Issue item
4. Display item details
5. Exit
Choice: 1
Enter Item code: 1212
Exception in thread "main"
java.util.ConcurrentModificationException at
java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901) at
java.util.ArrayList$Itr.next(ArrayList.java:851) at
items.Shop.search(Shop.java:28) at items.Shop.addItem(Shop.java:55)
at items.ItemDemo.main(ItemDemo.java:26)
What am i doing wrong? (Tried to give all relevant classes)
ItemDemo.mainin the question