Designing a Vending Machine in Java involves creating a simple program that simulates the process of selecting products, checking prices, accepting payment, and dispensing the selected item. A basic Vending Machine program typically includes elements such as products, prices, and user interactions.
Below is a basic implementation of a Vending Machine in Java that handles the selection of items, accepts payment, and returns change.
VendingMachine.java (Main Class)
import java.util.Scanner;
public class VendingMachine {
// Defining products and their prices
static String[] products = {"Soda", "Chips", "Candy", "Water"};
static double[] prices = {1.50, 2.00, 1.00, 0.75};
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double userBalance = 0.0;
int userChoice;
// Display the available products and their prices
displayProducts();
// Take input from user for payment
System.out.print("Enter money to insert (in dollars): ");
userBalance = scanner.nextDouble();
// Continue displaying products until user selects a valid item
while(true) {
// Display the products again after payment
displayProducts();
System.out.print("Enter the product number you want to buy (1-" + products.length + "), or 0 to exit: ");
userChoice = scanner.nextInt();
// If user selects 0, exit the program
if (userChoice == 0) {
System.out.println("Thank you for using the Vending Machine!");
break;
}
// Check if the user entered a valid choice
if (userChoice < 1 || userChoice > products.length) {
System.out.println("Invalid choice, please try again.");
continue;
}
// Handle purchase
processPurchase(userChoice - 1, userBalance);
// Ask user if they want to make another purchase
System.out.print("Would you like to make another purchase? (yes/no): ");
String continueShopping = scanner.next();
if (!continueShopping.equalsIgnoreCase("yes")) {
break;
}
}
scanner.close();
}
// Method to display products and their prices
public static void displayProducts() {
System.out.println("\nAvailable Products:");
for (int i = 0; i < products.length; i++) {
System.out.println((i + 1) + ". " + products[i] + " - $" + prices[i]);
}
}
// Method to process purchase
public static void processPurchase(int productIndex, double balance) {
double price = prices[productIndex];
String product = products[productIndex];
if (balance >= price) {
double change = balance - price;
System.out.println("\nYou have purchased " + product + " for $" + price);
System.out.println("Your change is: $" + change);
} else {
System.out.println("\nInsufficient funds! Please insert more money.");
}
}
}
Explanation of the Program
- Products and Prices:
- We define an array
products[]
that holds the names of items in the vending machine. prices[]
holds the corresponding prices of the items.
- We define an array
- User Input:
- The program first asks the user how much money they want to insert.
- It then displays the available products with their prices and prompts the user to choose an item to buy.
- Purchase Processing:
- The user selects an item by entering its corresponding number.
- If the user has enough balance, the program processes the purchase, subtracts the price from the inserted money, and shows the change.
- If the user doesn’t have enough money, it notifies them about the insufficient funds.
- Loop for Multiple Purchases:
- After each purchase, the program asks if the user wants to continue purchasing more items.
- The program continues until the user decides to stop (by entering ‘no’) or exits by selecting 0.
Running the Program
Sample Output:
Available Products:
1. Soda - $1.5
2. Chips - $2.0
3. Candy - $1.0
4. Water - $0.75
Enter money to insert (in dollars): 5.0
Available Products:
1. Soda - $1.5
2. Chips - $2.0
3. Candy - $1.0
4. Water - $0.75
Enter the product number you want to buy (1-4), or 0 to exit: 1
You have purchased Soda for $1.5
Your change is: $3.5
Would you like to make another purchase? (yes/no): yes
Available Products:
1. Soda - $1.5
2. Chips - $2.0
3. Candy - $1.0
4. Water - $0.75
Enter the product number you want to buy (1-4), or 0 to exit: 2
You have purchased Chips for $2.0
Your change is: $1.5
Would you like to make another purchase? (yes/no): no
Thank you for using the Vending Machine!
Enhancements for a More Advanced Vending Machine
- Track Inventory: You can track how many items are left for each product and decrease the count after each purchase.
- Accepting Coins: You can create a method to accept different types of coins (quarters, dimes, nickels, etc.) rather than just a total balance.
- Different Payment Methods: Expand the program to allow payment by credit card, digital wallets, or other payment methods.
- Error Handling: Add error handling for invalid inputs (e.g., user enters non-numeric values).
This basic Vending Machine demonstrates how object-oriented programming can simulate a real-world process in Java. You can further enhance it by adding more complex features such as inventory management, advanced payment processing, and UI elements.