Deleted since my college will scan using turnitin.com
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Deleted since my college will scan using turnitin.com
Last edited by frankycool; November 16th, 2009 at 11:57 AM. Reason: Plagarism
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:
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:
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:
int index = car.indexOf("Ford_Mustang");
frankycool (November 15th, 2009)
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
Last edited by frankycool; November 16th, 2009 at 11:45 AM. Reason: Plagarism
You forgot to over-write the the equals method in the CarMethods class
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
Last edited by frankycool; November 16th, 2009 at 11:46 AM. Reason: Plagarism
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:
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.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; } }
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; }