I have an arraylist As such :-

products.add( new CarsSubProduct( "FordMustang",1,12,10000 ) );
products.add( new CarsSubProduct( "JEEP",2,10,10000 ) );

I am trying to Edit just the value 12 without altering the value of the other objects but no success.When i use the set method it automatically modifies all the four objects in the arraylist.

I want to implement this technique in an Inventory software where ,when a user buys e.g Ford Mustang the units sold shall be decremented from 1000 which is the total available units in stock.

Recommended Answers

All 5 Replies

Thanks for your reply peter_budo,will do some reading about it and see if it solves my problem.

I want to implement this technique in an Inventory software where ,when a user buys e.g Ford Mustang the units sold shall be decremented from 1000 which is the total available units in stock.

You'd typically look-up the item of interest and then perform the necessary operations on it rather than setting a particular item of the ArrayList. A better implementation here would be have your own Inventory class which provides another layer of abstraction over the List implementation of your inventory and then have methods like lookup(itemId) or find(itemId) to find a given item. After it, all that needs to be done is modify the quantity property of that item.

HI Thanks for the code and advice you provided.
Need advice on an error,i have two methods which goes to the array and get a value from it, had a feeling its going to work but when i ran the program it gave me an error saying
ArrayIndexOutOfBoundsException: -1
Why is it that even when i specify the index to go to it still returns -1 ?
If i can get through this problem then i think am done with the hard part of my assignment.

public double validateprice()
{
        int check=CarInfo.Cars.indexOf(Carname);
        double prs=CarInfo.Cars.get(check).getPrice();

        return prs;
}
public double validatestock()
{
        int check=CarInfo.Cars.indexOf(Carname);
        double unt=CarInfo.Cars.get(check).getUnit();
      
        return unt;
}

> Why is it that even when i specify the index to go to it still returns -1 ?

It's not returning -1; the exception says that you can specifying an array index [-1 in your case] which is out of bounds for the given array [either less than 0 or more than equal to the array length]. Try debugging your application to determine the exact cause of this exception.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.