Interview Programs

Coffee Vendor Machine Program

23 Jul , 2015  

Hi, this is the example of Coffee Vendor Machine Program in Java.Coffee Vendor Machine program is also asked for fresher in a technical interview.First understand what is the Coffee Vendor Machine program?

Fizz Buzz progrm in java click here

Write a program For Coffee Vendor Machine or Coffee Machine, which accepts money from the user.
1. If the money is sufficient, then Coffee Vendor Machine serves the coffee to the user

2. If the money is not sufficient then, Coffee Vendor Machine notify the user that money is not enough.

3. If a user submits more money than the actual price of coffee, then Coffee Vendor Machine will give the coffee as well as remaining money.
Note: the Coffee Vendor Machine dispenses only one type of coffee, which is about 50 Rs.

Explanation of question in detail:
1. If the user gives 40 Rs to Coffee Vendor Machine then Coffee Vendor Machine displays the message money is not enough to serve the coffee.
2. If the user gives 50 Rs to Coffee Vendor Machine then Coffee Vendor Machine serve the coffee to the user.
3. If the user gives 100 Rs to Coffee Vendor Machine then Coffee Vendor Machine serve the coffee for the user as well as return the change 100-50=50 Rs to the user.


import java.util.Scanner;

public class CoffeeVendor {

	private static final double coffeePrice = 50;

	public static void main(String[] args) {
		double userMoney = 0.0;
		userMoney = acceptMoney();
		if (isMoneyEnough(userMoney)) {
		} else {
			notifyMessage("Money is not enough to serve the coffee");
		if (userMoney > coffeePrice) {
			System.out.println("your change is :" + issueChange(userMoney)
					+ "Rs");

	public static double acceptMoney() {
		double payAmount = 0.0;
		Scanner input = new Scanner(;
		System.out.println("Give Money to the machine:");
		payAmount = input.nextDouble();
		return payAmount;

	public static boolean isMoneyEnough(Double myMoney) {
		if (myMoney >= coffeePrice) 
			return true;
			return false;

	public static void dispenceCoffee() {
		System.out.println("Coffee is serve to the user");

	public static void notifyMessage(String message) {

	public static double issueChange(Double money) {
		Double change = 0.0;
		change = money - coffeePrice;
		return change;


Following are the sample output for above program

Output I: for money 40 Rs
Give Money to the machine:
Money is not enough to serve the coffee

Output II: for money 50 Rs
Give Money to the machine:
Coffee is serve to the user

Output III: for money 80 Rs
Give Money to the machine:
Coffee is serve to the user
your change is :30.0Rs

Code Explanation
1. acceptMoney() method is used for accepting the money from the user.
2. isMoneyEnough() method is used to check if the money is sufficient or not for the coffee. If the money is sufficient then dispenceCoffee() method is executed. Else notifyMessage() method will be executed.
3. dispenceCoffee() method is used to serves the coffee
4. notifyMessage() method is used to notify the user money is not enough to serve the coffee.
5. issueChange() is excuted if money is more than coffee price and return the change to the user

If you have any doubts , any question regarding this problem comments here. You can also share this problem on social sites by clicking on below links

Prime Number Program In Java Click Here


4 Responses

  1. prasad says:

    nice …..and we are much more interested to learn core java concept please upload core java content asap.thanks

  2. yogesh says:

    its a such a interesting problem i can understand it………thank u…..

  3. Devidas says:

    please upload the core java concept in depth as quick as possible………..!
    thank you…………!

Leave a Reply

Your email address will not be published. Required fields are marked *