File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Extending the VendingMachine Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Extending the VendingMachine" Watch "Extending the VendingMachine" New topic

Extending the VendingMachine

Mick Jones

Joined: May 21, 2009
Posts: 7
Hi everyone

I have fixed the error and tidied up the code, so here is the final, working version of the Vending Machine.

I want to carry on developing this as I feel I am learning a lot doing this...

Does anyone have any cool ideas of how to extend it? My ideas are:

- Convert program to Swing so it has a GUI
- Create some kind of Engineer interface, i.e. the vending machine engineer opens the machine up and does clever stuff like gets debug info, reports, reprograms locations and costs. (no idea how to add this interface)
- Crazy idea... Remote Vending Machine administration, i.e. same as above but engineer can access the info remotely via the web to diagnose / re-program the vending machine (like this idea but not sure how to do it)
- Any other ideas?

p.s. i am also interested in the Cattle Drive course, how good is it? Please dont just send me the link as I have already read the info, i would like to hear from others if you feel i would benefit?

Please also comment on how good code is etc as I am trying to learn!

Currently learning Java
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 684
You could experiment with different program designs.

For example here you have a main method in the vending machine class, and a call to the vending machine constructor within that main method of that class. You may wish to consider a vending machine class without a main method, the main and some of the logic would be in a "manager class" to me that seems more logical when you are managing multiple vending machines.

You may wish to create an application with a graphical user interface. In which case the focus might be on designing your vending machine and snack classes to be re-usable, i.e. so they could work without modification in both a GUI based application and a non-GUI based application. That is the essense of modular design, in my opinion.

You could even have it so it works in both an applet (browser based application that is accessible over the internet), or in a desktop GUI that uses JFrames.

In all these cases, you should be able to design your product and vending machine classes such that you don't need to make many changes to have them work in a GUI or a non-GUI environment.

p.s. there are always more than one or two ways to get the job done. I could have written a program that did exactly the same thing, but it might not have used a go() method, maybe I might have had more stuff in my main(). Not saying that is betetr or worse, each as its advantages I guess.
Tanweer Noor

Joined: Sep 22, 2009
Posts: 1
Your code is giving a lot of compilation error. let me know the fix

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46437
Welcome to JavaRanch , Tanweer Noor

Try copying the code with the "view plain" link above, otherwise tell us exactly what errors you are experiencing.
Wim Vanni
Ranch Hand

Joined: Apr 06, 2011
Posts: 96

Some quick ideas and remarks. Sadly I don't have the time to go into this extensively. (This does not mean there's a lot 'wrong' with your code ;-)

- Use a seperate class for the productMap
- Get (and store) the data from a 'datastore' (possibly a database or xml, but this can as well be a simple txt file)
- Implement a maximum stock level
- Careful with the Exception handling you've set up; catching an exception doesn't necessarily mean it's 'only' an invalid selection; Create specific/custom exceptions that you throw and catch and handle appropriately
- the attribute moneyIn can become negative (although you capture this in line 91); seems inappropriate
- try implementing toString() to the Product class; could be useful to replace some of the product.getName() calls; this method could already take the stock level into account

I'm not familiar with the vending machine exercise but as far as I can see the code you show here deserves a good grade.

Good luck with your java learning track, and remember: the sky is the limit! :-)


Quick edit: had a little lightbulb moment : Implement a way to have the vending machine react as it would in real life: sometimes not dispensing the chosen item, sometimes even dropping two!
I agree. Here's the link:
subject: Extending the VendingMachine
It's not a secret anymore!