Sample vending machine made in Java (classification)

The vending machine I wrote last time: Was classified into classes.

package vm;
import java.util.HashMap;
import java.util.Map;
public class Items {
    private Map<String, Integer> items; //Product List
    private Map<String, Integer> availablePurchases; //Items that can be purchased
    //Constructor (initialization of product list)
    Items() {
        this.items = new HashMap<String, Integer>();
        items.put("Cola", 100);
        items.put("Orange juice", 120);
        items.put("water", 80);
    //Get the minimum purchase price
    int minPrice() {
        int minPrice = 0;
        int loopCount = 0;
        for (String itemKey : items.keySet()) {
            minPrice = validMinPrice(loopCount, minPrice, itemPrice(itemKey));
        return minPrice;
    //Judgment of minimum purchase amount
    int validMinPrice(int loopCount, int minPrice, int price) {
        if (loopCount == 0 || minPrice > price) {
            minPrice = price;
        return minPrice;
    //Product price
    private int itemPrice(String itemKey) {
        return items.get(itemKey);
    //Amount of items that can be purchased
    private int availablePurchasePrice(String itemKey) {
        return availablePurchases.get(itemKey);
    //Get a list of available products
    void createAvailablePurchases(int deposit) {
        availablePurchases = new HashMap<String, Integer>();
        for (String itemKey : items.keySet()) {
            addAvailablePurchase(deposit, itemKey);
    //Addition of available product list
    private void addAvailablePurchase(int deposit, String itemKey) {
        if (deposit >= items.get(itemKey)) {
            availablePurchases.put(itemKey, items.get(itemKey));
    //Display of available products
    void showAvailablePurchase() {
        for (String itemKey : availablePurchases.keySet()) {
            System.out.println(itemKey + ":" + items.get(itemKey) + "Circle");
    //Judgment as to whether the product is available for purchase
    boolean isAvailablePurchase(String itemName, Deposit deposit) {
        return availablePurchases.containsKey(itemName);
    //Selling price
    int saleAmount(String itemName) {
        return availablePurchasePrice(itemName);

package vm;
public class Deposit {
    private int deposit; //Deposit amount
    //Constructor (initial value 0 yen)
    Deposit() {
        deposit = 0;
    void deposit(int amount) {
        deposit = deposit + amount;
    //Current deposit amount
    int amount() {
        return this.deposit;
    void charge(int amount) {
        int charge = this.deposit - amount;
        this.deposit = charge;

package vm;
public class VendingMachine {
    private Items items; //Product
    private Deposit deposit; //Deposit amount
    VendingMachine() {
        items = new Items();
        deposit = new Deposit();
    void deposit(int amount) {
    //Determine if there is a deposit above the lowest priced item
    boolean hasMinDeposit() {
        if (deposit.amount() < items.minPrice()) {
            return false;
        return true;
    //Show available items
    void showAvailablePurchases() {
    //Determine if the item is available for purchase
    boolean isAvailablePurchases(String itemName) {
        return items.isAvailablePurchase(itemName, deposit);
    //Show purchased items
    void selectItem(String itemName) {
        System.out.println(itemName + "is!");
    void charge(String itemName) {
        int saleAmount = items.saleAmount(itemName);
        System.out.println("Change is" + deposit.amount() + "It is a circle.");

package vm;
import java.util.Scanner;
 *Vending Machine (Level 6) Creating Vending Machine Class
public class Main {
    //Main logic
    public static void main(String[] args) {
        //① Product initialization
        VendingMachine vendingMachine = new VendingMachine();
        //Additional deposit for minimum purchase amount
        Scanner scanner = new Scanner(;
        do {
            //② Deposit
            System.out.println("Please put in the money.");
            int amount = scanner.nextInt();
            //③ Amount check (minimum purchase amount)
        } while (!vendingMachine.hasMinDeposit());
        //④ Display available products
        System.out.println("It is a product that can be purchased.");
        //⑤ Select a product
        String itemName;
        do {
            System.out.println("Please enter the product name.");
            itemName =;
        } while (!vendingMachine.isAvailablePurchases(itemName));
        //⑥ Billing function

Recommended Posts

Sample vending machine made in Java (classification)
Sample vending machine made in Java
Vending machine made with Java (domain driven)
I made roulette in Java.
A simple sample callback in Java
I made an annotation in Java.
Age guessing game made in Java
Sample to unzip gz file in Java
Refactored GUI tools made with Java8 + JavaFX in 2016
Partization in Java
[Java] Generics sample
Java sample code 02
Java sample code 03
Selenium sample (Java)
Changes in Java 11
Java GUI sample
Rock-paper-scissors in Java
Java sample code 04
I made a rock-paper-scissors game in Java (CLI)
Java sample code 01
Pi in Java
FizzBuzz in Java
I made a simple calculation problem game in Java
Sample code to convert List to List <String> in Java Stream
[For beginners] Minimum sample to display RecyclerView in Java
java state machine car
[java] sort in list
Read JSON in Java
Interpreter implementation in Java
Make Blackjack in Java
Rock-paper-scissors app in Java
Constraint programming in Java
Put java8 in centos7
NVL-ish guy in Java
Combine arrays in Java
"Hello World" in Java
Comments in Java source
Azure functions in java
Format XML in Java
Simple htmlspecialchars in Java
Boyer-Moore implementation in Java
Hello World in Java
Use OpenCV in Java
Type determination in Java
Various threads 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
Date manipulation in Java 8
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