Sample vending machine made in Java


Gain programming experience with Java

Target person

Beginners who learned the basics of Java

Achievement goal

You can program based on the flowchart and functional details.



detail of function

・ Product list initialization The following three products are fixed. Coke 100 yen Orange juice 120 yen Water 80 yen

·payment It is possible to deposit in units of 1 yen. Prompt to deposit until the minimum amount available for purchase is deposited. (In this case, 80 yen or more for water)

・ Product selection Display products within the deposit amount. Select by product name.

・ Sales Offer the selected product.

・ Billing Subtract the purchase price from the deposit amount. Return the change.

Sample code

package vm;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		//Product initialization
		Map<String, Integer> items = new HashMap<String, Integer>();
		items.put("Cola", 100);
		items.put("Orange juice", 120);
		items.put("water", 80);

		//Additional deposit for minimum purchase amount
		int deposit = 0;
		int minSaleAmount = -1;
		Scanner scanner = new Scanner(;
		do {
			//Deposit processing
			System.out.println("Please put in the money.");
			deposit = deposit + scanner.nextInt();

			//Amount check (minimum purchase amount)
			int loopCount = 0;
			for (String itemKey: items.keySet()) {
				if(loopCount == 0 || minSaleAmount > items.get(itemKey)) {
					minSaleAmount = items.get(itemKey);
		} while(deposit < minSaleAmount);

		//Product selection
		System.out.println("Please select a product.");
		Map<String, Integer> availablePurchases = new HashMap<String, Integer>();
		for (String itemKey: items.keySet()) {
			if(deposit >= items.get(itemKey)) {
			System.out.println(itemKey + ":" + items.get(itemKey) + "Circle");
			availablePurchases.put(itemKey, items.get(itemKey));

		String itemName;
		while(true) {
			itemName =;
			if (availablePurchases.containsKey(itemName)){
			System.out.println("The product name is specified incorrectly. Please specify the product name again.");
		System.out.println(itemName + "is!");

		//Billing function
		deposit = deposit - availablePurchases.get(itemName).intValue();
		System.out.println("Change is" + deposit + "It is a circle.");



I have created a classified version.

Recommended Posts

Sample vending machine made in Java
Sample vending machine made in Java (classification)
I made roulette in Java.
I made an annotation in Java.
Age guessing game made in Java
Sample to unzip gz file in Java
Partization in Java
[Java] Generics sample
Java sample code 02
Java sample code 03
Changes in Java 11
Java GUI sample
Rock-paper-scissors in Java
Java sample code 04
Java sample code 01
Pi in Java
FizzBuzz in Java
I made a primality test program in Java
Refactored GUI tools made with Java8 + JavaFX in 2016
java state machine car
Interpreter implementation in Java
Make Blackjack in Java
I made a simple calculation problem game in Java
Rock-paper-scissors app in Java
Constraint programming in Java
Sample code to convert List to List <String> in Java Stream
Put java8 in centos7
NVL-ish guy in Java
"Hello World" in Java
Callable Interface in Java
Comments in Java source
Azure functions in java
[Java] Holiday judgment sample
Format XML in Java
Simple htmlspecialchars in Java
Boyer-Moore implementation in Java
Use OpenCV in Java
webApi memorandum in java
Type determination in Java
Ping commands in Java
Heapsort implementation (in java)
Zabbix API in Java
ASCII art in Java
Compare Lists in Java
Express failure in Java
Create JSON in Java
[Java] logback slf4j sample
What's new in Java 8
Use PreparedStatement in Java
What's new in Java 9,10,11
Parallel execution in Java
Initializing HashMap in Java
[Beginner] I made a program to sell cakes in Java
Try using RocksDB in Java
Read binary files in Java 1
Avoid Yubaba's error in Java
Digital signature sample code (JAVA)
Get EXIF information in Java
Save Java PDF in Excel
[Neta] Sleep Sort in Java