Design a Vending Machine in Java - Interview Question

Posted by Infocampus HR on February 1st, 2018

              One of the key advantages of such Java interviews is that you can test many essential skills or a candidate in one go. In order to complete  the design, coding, and unit testing of  a Vending machine, a candidate needs to be really good in all three departments. By the way, this kind of real world problem is also a good exercise to improve your object-oriented analysis and design skills, which is very important if you want to become a good application developer.

By designing a vending machine in Java or any other object-oriented language, you not only learn basics e.g. Encapsulation, Polymorphism or Inheritance but also learns subtle details of how to use an abstract class and interface  while solving a problem or designing an application.

Usually, this kind of problem also gives you an opportunity to utilize Java design patterns, as in this problem we will be using Factory method pattern for creating different types of Vending Machine. I have talked about this question when I shared 20 software design questions in Java, and after that, I receive a lot of feedback to provide a solution for that question.

This two-part series of posts will provide a solution of Vending machine problem in Java. By the way, this problem can be solved in a different way, and you should try to do that before looking into the solution given here. This is also an opportunity to revisit SOLID and OOPS design principles and get ready to use them in your code. You'll find many of them are applicable when you design vending machine in Java.

Many Java developers ask me How to become a better programmer, how do I improve my programming skills, or I am good at Java but not so good on problem-solving skill etc. This is a reality, today's era is full of language expert than real programmers. It's easy to understand keywords, methods, and API of Java programming language, but same time it's difficult to solve real problems, design reusable and robust software and get the maximum of data structure and algorithm. I have often seen Java programmers are not doing so well when asked to design and code a solution in limited time, but same time they are really good with Java concepts and all theory. Even senior programmers of 4 to 6 years experience in Java programming, sometimes fail to solve questions like designing

Even senior programmers of 4 to 6 years experience in Java programming, sometimes fail to solve questions like designing coffee-maker, coding Vending Machine or sometimes even recursively reversing a linked list. I may be just moving off from becoming a better programmer, but it's important to know your limitation and then work out to resolve those.

If you feel resistance in coding, then you should code as much as possible, if you feel stressed and lost in designing using Object-oriented analysis and design, then you should do designing using pen and paper as much as possible. This resistance is what actually preventing you from being a better programmer.

I have personally found that coding and designing presents a lot of resistance to an average programmer, because in most of them in their professional job don't do enough coding and development.

Problem Statement

You need to design a Vending Machine which

  1.  Accepts coins of 1,5,10,25 Cents i.e. penny, nickel, dime, and quarter.
  2.  Allow user to select products Coke(25), Pepsi(35), Soda(45)
  3.  Allow user to take refund by canceling the request.
  4.  Return selected product and remaining change if any
  5.  Allow reset operation for vending machine supplier.


The requirement statement is the most important part of the problem. You need to read problem statement multiple times to get a high-level understanding of the problem and what are you trying to solve. Usually, requirements are not very clear and you need to make a list of your own by reading through problem statement.

I like point based requirement because it's easy to track. Some of the requirement are also implicit but it's better to make it explicit in your list e.g. In this problem, vending machine should not accept a request if it doesn't have sufficient change to return.

Unfortunately, there is not many book or course which teach you these skills, you need to develop them by yourself by doing some real world work. Though, two of the book which helped me to improve by object-oriented analysis and design skills are  1st edition by Brett D. McLaughlin. One of the best book if you don't have much experience in object oriented programming.

Like it? Share it!


Infocampus HR

About the Author

Infocampus HR
Joined: December 10th, 2016
Articles Posted: 792

More by this author