Jhoel Esmejarda wrote:I hope someone could be of help. thanks
Well, my advice is going to be general, rather than specific to your problem:
1. Don't use constants like 3, because they make your code very brittle. What if you decide to have 4 items later on? Or ten? You'll have to go through your entire program and replace every piece of code that has 3 in it. There is a much better way to do tests like that, and it's based on the fact that all arrays have a
length. And if, later on, you decide to use collections (eg, a List), you can use the
size() method. In fact, it might not be a bad thing to write a little method called
numberOfItems().
2. Your classes have names that sound like functions. In general, classes are
things (not always, but certainly in your case), so they should usually have names that are
nouns not verbs. And to get a good class, you need to understand what it's modelling.
Take your program: You're trying to model a vending machine, yet you don't have a class called
VendingMachine. You have items of food, yet you don't have a class called
FoodItem. Vending machines usually have slots that contain the items to be sold, but I don't see a class called
Slot. A vending machine also has to interact with a person in order to do anything at all, yet there's no
User or
Customer class.
I have a feeling you're worrying too much about
how you're going to do things, rather than concentrating on
what needs to be done.
It's a common mistake for beginners because you're just dying to bash out some code; but it's almost always the wrong thing to do.
STOP CODING and get out a pencil and paper, and write down
what it is that a vending machine does. What's the sequence of events needed for a user to get an item of food?
Exactly. Does it coincide with the classes that you've written?
I hope it helps.
Winston