- Joined
- Feb 25, 2004
- Location
- MA, USA
Alright I have a little problem.
I have an array that stores variables of type Class Drink, Item and Coffee.
what I have to do is, iterate the array, looking to see if a item is in the array.
For example I could have created a Drink Item, with attributes type, name, size and quantity
I want to be able to do is iterate the array, looking for the type and name.
When I find that, I want to be able to look at the items quantity and if its greater than zero decriment it.
I know that getClass returns and object of type class, and getClass().getName() will return me the Name of that class.
I was thinking if I find a Drink element, I could use its getName() method to get the name and if its not the name I want go to the next element.
I was worried about how Java would know that I want to edit the Pepsi named object of Drink and not the Coke one. Would this be an issue or can I just go
if (myarray.getClass().getName() == DrinK)
name = Drink.getName()
if (name.equalsIgnoreCase("Pepsi")
quantity = Drink.getQuantity;
if (quantity <= 0)
output error message
else if (quantity is > 0)
output item can be sold
Drink.setQuantity(quantity--);
if you would like to look at the code itself, let me know and I'll upload it.
its basically a gui, that is imitating a vending machine
I have an array that stores variables of type Class Drink, Item and Coffee.
what I have to do is, iterate the array, looking to see if a item is in the array.
For example I could have created a Drink Item, with attributes type, name, size and quantity
I want to be able to do is iterate the array, looking for the type and name.
When I find that, I want to be able to look at the items quantity and if its greater than zero decriment it.
I know that getClass returns and object of type class, and getClass().getName() will return me the Name of that class.
I was thinking if I find a Drink element, I could use its getName() method to get the name and if its not the name I want go to the next element.
I was worried about how Java would know that I want to edit the Pepsi named object of Drink and not the Coke one. Would this be an issue or can I just go
if (myarray.getClass().getName() == DrinK)
name = Drink.getName()
if (name.equalsIgnoreCase("Pepsi")
quantity = Drink.getQuantity;
if (quantity <= 0)
output error message
else if (quantity is > 0)
output item can be sold
Drink.setQuantity(quantity--);
if you would like to look at the code itself, let me know and I'll upload it.
its basically a gui, that is imitating a vending machine