• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Array, Iteration and item reading in JAVA

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

dicecca112

Member
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
 
ok, I am assuming that you have Drink array[x]; and that type is a string.

Can you please upload the source to actually see what it is? Why are you copying all that stuff, e.g, shouldn't this work? (with error checking of course)
Drink array[x];
for(;x>0;x--)
if(array[x].getType == "Coffee" && array[x].getName== "SomeName")
array[x].quanity--;

================================
Would it be possible to just do the following in your code?
if (myarray[x].getClass().getName().equalsIgnoreCase("Pepsi"))
{myarray[x].getClass().setQuantity(myarray[x].getClass().getQuantity-1); }
 
Last edited:
alright here is what I have, the function that I want to do this is called dispense and its in the dispensor class. It doesn't work though

my think was that when I iterate through the array looking for the name, I'd grab the element number from the array. using that i could do items[itemNumber].whatever I needed to do. Doesn't seem to work though. The only case that works, is when its not of type Drink, Snack or Coffee, its suppose to output Item not found, which it does.
 
Last edited:
I must admit I'm unfamilar with the exact type getClass() returns, but it seems like the following could be causing your problems:
Code:
private Item items[] = new Item[10];

// Lots of code!

String name = items.getClass().getName();
If getClass() is doing what I think it is, then name should be equal to "Item" (since that is the type that items is).

I can't quite see how the Dispensor is working to suggest a fix (I can't find where the items array is filled??), but I'd possibly look into the use of Java's instanceof keyword. You use it in an if statement to compare types (e.g., if (items[1] instanceof Drink) { foo(); }) and I believe it returns true if the item being compared could be assigned to the specific type (i.e., if items[1] is a Pepsi it will return true for comparison to the types Pepsi, Drink, and Object, but not Snack.)

JigPu
 
well from starring at this for a while, it looks like when I try to get the className using getClass.getName, its returning gibberish ( returns [lITEM for some reason). instanceof might be the way to go. I think its because I'm not in the array yet.
 
dicecca112 said:
well from starring at this for a while, it looks like when I try to get the className using getClass.getName, its returning gibberish ( returns [lITEM for some reason). instanceof might be the way to go. I think its because I'm not in the array yet.
instanceof is a good operator to use in this case. foo[x] instanceof Drink, etc.
 
well we figured it out last night. What happens was getClass.getName was returning classnameItem. So we had to change something so that it output Classname, then it worked.
 
I am glad to see you have something working, but have you considered using queues? Loading the machine could be simulated by pushing items onto the queues, and you would pop one of those items off the appropriate queue each time the machine dispenses one. If you had a queue for each item your simulated machine stores, then your code for dispensing items is to simply check that the queue is not empty, and, if it is not, pop an item from that queue.
 
we didn't learn queues in class, but from what I learned about them in other classes yeah it would have been easier. Even if we used an Iterator or ArrayList it would have been easier but we didn't have the time to mess around with it.
 
I cannot count the number of times I struggled in school, or even in my professional career, to solve a problem, only to discover a new technique at a later date that would have made it trivial.

One of the best things anybody learning programming can do after learning the fundamentals is to pick up a book on design patterns. Most problems encountered in programming have been encountered before. Unfortunately, the usual response is to reinvent the wheel. Of course, different rules apply when you are still learning programming basics.
 
yeah we got into that near the end of my java class this semester, it was really cool. I felt myself always digging for old code that I had solved a problem. What is the book, written by the gang of 3 that I have to order one of these days
 
dicecca112 said:
yeah we got into that near the end of my java class this semester, it was really cool. I felt myself always digging for old code that I had solved a problem. What is the book, written by the gang of 3 that I have to order one of these days

You are thinking of the "Gang of 4" book - so called because it was published by 4 authors. It's a design patterns book - I personally found it useless, but some people swear by design patterns. Teachers love that kind of stuff though when you can explain your program using words like "Factory" and "Strategy" Patterns. Try to get your prof into a discussion on why "Model-View-Controller" is actually NOT a pattern. . .

BTW: I believe the name of the book is simple "Design Patterns"
 
Back