Share & learn

Through Innovative Digital Library

How to write a program for a vending machine in Java?

I need a small project of vending machine in Java. (Input and Output should be provided from Console).
 



Answers

Posted by Paul on April 23rd 2018 13:11 PM

Requirements : First of all user should be asked for money. Once user enters money, a categorized menu should be displayed. (e.g Cold Drinks, Crisps etc) and user will be asked for a choice. Once user makes a choice a message of successful process and the remaining change should be displayed on console. Here is the code for Vending Machine in Java. File Details : Main.java : This is the start of the program. Here is the code :

// main class for vending machine 
public class Main {

    public static 
void main(String[] args) {

        
VendingMachine.main(args);

    }

}

Posted by Paul on April 23rd 2018 13:12 PM

VendingMachine.java : - Adding items to stock. - Menu display - Check for enough credit

import java.util.Scanner

public class 
VendingMachine {

    public static 
void main(String[] args) {

        
// initialize class for transactions
        
TransactionProcess transaction = new TransactionProcess();

        
// create class for each type of item

        // crisp items
        
Item crispsOne = new Item();
        
crispsOne.setStock("Cream & Onion"10"A1"5);

        
Item crispsTwo = new Item();
        
crispsTwo.setStock("Salted"10"A2"5);

        
Item crispsThree = new Item();
        
crispsThree.setStock("Tomato"10"A3"5);

        
// drink items
        
Item bottleOne = new Item();
        
bottleOne.setStock("Coke"10"B1"20);

        
Item bottleTwo = new Item();
        
bottleTwo.setStock("Pepsi"10"B2"25);

        
Item bottleThree = new Item();
        
bottleThree.setStock("Sprite"10"B3"28);

        
// chocolate itms 
        
Item chocOne = new Item();
        
chocOne.setStock("Five Star"10"C1"10);

        
Item chocTwo = new Item();
        
chocTwo.setStock("Dairy Milk"10"C2"10);

        
Item chocThree = new Item();
        
chocThree.setStock("Munch"10"C3"5);

        
// interface class for user interaction
        
UserInterface mainInterface = new UserInterface();
        
mainInterface.getMoney();
        
System.out.println("Available credit: " mainInterface.moneyIn "\n");
        
mainInterface.getInput();
        
System.out.println("\nYou entered " mainInterface.selection);


        
// menu creation
        
switch (mainInterface.selection) {

        case 
"A1":

            
transaction.itemPrice crispsOne.price;
            
transaction.change transaction.Transaction(mainInterface.moneyIntransaction.itemPrice);

            if (
transaction.enoughCredit() == true) {
                
mainInterface.displayChoice(crispsOne.itemNamecrispsOne.pricetransaction.change);
                
crispsOne.stockAdjust();
                break;
            } else {

                
mainInterface.catchAnError();
                break;
            }

        case 
"A2":

            
transaction.itemPrice crispsTwo.price;
            
transaction.change transaction.Transaction(mainInterface.moneyIntransaction.itemPrice);

            if (
transaction.enoughCredit() == true) {
                
mainInterface.displayChoice(crispsTwo.itemNamecrispsTwo.pricetransaction.change);
                
crispsTwo.stockAdjust();
                break;
            } else {

                
mainInterface.catchAnError();
                break;
            }

        case 
"A3":

            
transaction.itemPrice crispsThree.price;
            
transaction.change transaction.Transaction(mainInterface.moneyIntransaction.itemPrice);

            if (
transaction.enoughCredit() == true) {
                
mainInterface.displayChoice(crispsThree.itemNamecrispsThree.pricetransaction.change);
                
crispsThree.stockAdjust();
                break;
            } else {

                
mainInterface.catchAnError();
                break;
            }

        case 
"B1":

            
transaction.itemPrice bottleOne.price;
            
transaction.change transaction.Transaction(mainInterface.moneyIntransaction.itemPrice);

            if (
transaction.enoughCredit() == true) {
                
mainInterface.displayChoice(bottleOne.itemNamebottleOne.pricetransaction.change);
                
bottleOne.stockAdjust();
                break;
            } else {

                
mainInterface.catchAnError();
                break;
            }

        case 
"B2":

            
transaction.itemPrice bottleTwo.price;
            
            
// set itemPrice
            
transaction.change transaction.Transaction(mainInterface.moneyIntransaction.itemPrice);
            
            if (
transaction.enoughCredit() == true) {
            
                
// check for enough fund 
                
mainInterface.displayChoice(bottleTwo.itemNamebottleTwo.pricetransaction.change);
                
                
bottleTwo.stockAdjust();
                
                break;
                

            } else {

                
mainInterface.catchAnError();
                break;
                
            }

        case 
"B3":

            
transaction.itemPrice bottleThree.price;
            
transaction.change transaction.Transaction(mainInterface.moneyIntransaction.itemPrice);

            if (
transaction.enoughCredit() == true) {
                
mainInterface.displayChoice(bottleThree.itemNamebottleThree.pricetransaction.change);
                
bottleThree.stockAdjust();
                break;
            } else {

                
mainInterface.catchAnError();
                break;
            }

        case 
"C1":

            
transaction.itemPrice chocOne.price;
            
transaction.change transaction.Transaction(mainInterface.moneyIntransaction.itemPrice);

            if (
transaction.enoughCredit() == true) {
                
mainInterface.displayChoice(chocOne.itemNamechocOne.pricetransaction.change);
                
chocOne.stockAdjust();
                break;
            } else {

                
mainInterface.catchAnError();
                break;
            }

        case 
"C2":

            
transaction.itemPrice chocTwo.price;
            
transaction.change transaction.Transaction(mainInterface.moneyIntransaction.itemPrice);

            if (
transaction.enoughCredit() == true) {
                
mainInterface.displayChoice(chocTwo.itemNamechocTwo.pricetransaction.change);
                
chocTwo.stockAdjust();
                break;
            } else {

                
mainInterface.catchAnError();
                break;
            }

        case 
"C3":

            
transaction.itemPrice chocThree.price;
            
transaction.change transaction.Transaction(mainInterface.moneyIntransaction.itemPrice);

            if (
transaction.enoughCredit() == true) {
                
mainInterface.displayChoice(chocThree.itemNamechocThree.pricetransaction.change);
                
chocThree.stockAdjust();
                break;
            } else {

                
mainInterface.catchAnError();
                break;
            }

        default:

            
System.out.println("You made an invalid selection, please try again \n");
            
System.out.println("Returning money \n" "...\n" "...\n" "Please collect your " mainInterface.moneyIn
                    
" change\n");
            break;

        }

        
// code to avoid screen from closing
        
int doNotClose;
        
Scanner stayOpen = new Scanner(System.in); 
        
doNotClose stayOpen.nextInt();

    }

}

Posted by Paul on April 23rd 2018 13:13 PM

UserInterface.java : - Here options for user to interact will be displayed.

// UserInterface class to allow user to interact with program

import java.util.Scanner;

public class 
UserInterface {

    
double moneyIn;
    
double moneyOut;
    
boolean isAvailable;
    
String selection;

    
// method to accept amount from user
    
void getMoney() {

        
Scanner sc = new Scanner(System.in);
        
System.out.println("Please insert money: ");
        
moneyIn sc.nextDouble();
        
System.out.println(" ");
        
    }

    
// method to get input from user
    
void getInput() {

        
Scanner scan = new Scanner(System.in);

        
System.out.println("Please select from one of the follwing\n");
        
System.out.println("***** Chips *****");
        
System.out.println("[A1] Cream & Onion, [A2] Salted [A3] Tomato \n");
        
System.out.println("***** Bottles of drink *****");
        
System.out.println("[B1] Coke, [B2] Dr Pepsi, [B3] Sprite\n");
        
System.out.println("***** Chocolate Bars *****");
        
System.out.println("[C1] Five Star, [C2] Dairy Milk, [C3] Munch\n");

        
selection scan.nextLine();
        
selection selection.toUpperCase();
        
    }

    
void displayChoice(String aChoicedouble aPricedouble someChange) {

        
// method to display vending machine's process
        
        
System.out.println("You choice " aChoice);
        
System.out.println("This costs " aPrice);
        
System.out.println("You are due " someChange " change\n ");
        
System.out.println("Vending Item \n" "...\n" "...\n" "Please Take your " aChoice "\n");
        
System.out.println("Vending Change \n" "...\n" "...\n" "Please Take your " someChange " change\n");
        
System.out.println("Thank you \n");

    }

    
// method to display that the funds are not sufficient
    
void insufficientFunds() {

        throw new 
UnsupportedOperationException();

    }

    
void catchAnError() {
        try {
            
this.insufficientFunds();

        } catch (
UnsupportedOperationException e) {

            
System.out.println("Sorry Insufficient Funds Please Try Again \n");
            
System.out.println(
                    
"Returning money \n" "...\n" "...\n" "Please collect your " this.moneyIn " change\n");

        }

    }

}

Posted by Paul on April 23rd 2018 13:13 PM

Item.java : - To store Items and stock processing

// Item class to store Items and stock processing
public class Item {

    
String itemName;
    
int stockLevel;
    
String selectionID;
    
double price;

    
// method to set values of new instances
    
void setStock(String aNameint numberOfString sIDdouble aPrice) {

        
itemName aName;
        
stockLevel numberOf;
        
selectionID sID;
        
price aPrice;

    }

    
// Method to decrement stock level after successful process
    
void stockAdjust() {

        
this.stockLevel--;

    }

}

Posted by Paul on April 23rd 2018 13:14 PM

TransactionProcess.java : - To perform transaction and money operations

// TransactionProcess class to perform transaction and money operations

public class TransactionProcess {

    
double itemPrice;
    
double change;

    
double Transaction(double moneyIndouble aPrice) {

        
double change;
        
change moneyIn aPrice
        return 
change;

    }

    
// check for credits enetered by user to be sufficient
    
boolean enoughCredit() {

        if (
this.change 0)
            return 
true
        else
            return 
false
    }

}
Answer Question

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

  • Minimum 20 character
PDRY

related questions

Most liked questions

Most answered questions

Login
PDRY Refresh
Register
PDRY Refresh
Forgot password
Ask a Question
Minimum 20 character
Minimum 20 character
Reply
Minimum 20 character
PDRY Refresh