Deleted since my college will scan using turnitin.com
Printable View
Deleted since my college will scan using turnitin.com
I suspect it may be because you have the variable that holds the inventory declared static inside of CarsSubProduct. Change it to be non-static, and then you can get the object reference using the ArrayList.get() method:
Code :CarsSubProduct car = products.get(0); // get the first car in the list car.soldOne(); // decrement the inventory of car by one
Thanks,i tried your method and now i can specify which object i want to get in the array like
int price=Cars.get(0).getprice();
But now i want to search all the indexes and get the index which contains for example Ford_Mustang then i use another syntax as such above to get the price
Cars.add( new CarMethods( "Ford_Mustang",1,1000,10000 ) );
Override the equals method of your CarsSubProduct class (or whatever you are putting into the ArrayList) so that it will test equality by the name:
Code :public boolean equals(Object o) { if (o instanceof String) { return (this.name.equals(o.toString()); } else { return false; } }
Then assuming each ArrayList you have will only have 1 Ford_Mustang, you can use the indexOf method to find it:
Code :int index = car.indexOf("Ford_Mustang");
Thanks will do as explained
indexOf returns -1 if it doesn't find the object you want.
Even when i initialize Carname and run on netbeans debugger,Carname still appears as null
Post your CarName class... I have a feeling the error is somewhere in there.
I need the CarMethods class. Could you post that as well?
Deleted since my college will scan using turnitin.com
You forgot to over-write the the equals method in the CarMethods class :P
Code :public boolean equals(Object o) { if (o instanceof String) { return (this.name.equals(o.toString()); } else { return false; } }
Deleted since my college will scan using turnitin.com
hmm.. it seems that the indexOf method compares the types first (kind of annoying :( )
An easy way around this is to create a "dummy" CarMethods object that has the name, but nothing else.
So:
Code :Cars.indexOf(new CarMethods("Ford_Mustang",0,0,0));
You'll also need to change the .equals() method to this:
will give you the indexOf the first Ford_Mustang in the ArrayList. The alternative is to iterate through the entire ArrayList of Cars, which isn't too hard.Code :public boolean equals(Object o) { if (o instanceof String) { return (this.carsnames.equals(o)); } else if (o instanceof CarMethods) { return (this.carsnames.equals(((CarMethods) o).carsnames)); } else { return false; } }
Code :public int customIndexOf(ArrayList<CarMethods> cars, String toFind) { for (int i = 0; i < cars.size(); i++) { if (cars.get(i).equals(toFind)) { return i; } } return -1; }