This ICSE Computer Applications project contains the implementation of a Vending Machine in Java BlueJ environment. Students can use this as a reference for their own projects.
Complete code and BlueJ output is provided below:
Item.java
/**
* This class represents an item of Vending Machine.
*
* @author knowledgeboat.com
* @version 1.0
*/
public class Item
{
private String name;
private double price;
private int qty;
public Item(String itemName,
double itemPrice,
int itemQty) {
name = itemName;
price = itemPrice;
qty = itemQty;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public int getQty() {
return qty;
}
public void reduceQty() {
if (qty > 0)
qty -= 1;
}
}
VendingMachine.java
/**
* Vending Machine Program
*
* @author knowledgeboat.com
* @version 1.0
*/
import java.util.Scanner;
public class VendingMachine
{
private Item[] items;
public VendingMachine() {
items = new Item[5];
items[0] = new Item("Lays Chips", 20.0, 20);
items[1] = new Item("Coke", 40.0, 10);
items[2] = new Item("Fanta", 40.0, 10);
items[3] = new Item("Sprite", 40.0, 10);
items[4] = new Item("Kinley", 20.0, 20);
}
public void displayInventory() {
for (int i = 0; i < items.length; i++)
{
System.out.print(items[i].getName());
System.out.print('\t');
System.out.print(items[i].getQty());
System.out.println();
}
}
public void dispenseItem(int itemCode) {
Scanner in = new Scanner(System.in);
if (items[itemCode].getQty() <= 0) {
System.out.println("Sorry, out of stock");
}
else
{
System.out.println("MRP: " + items[itemCode].getPrice());
System.out.print("Enter money: ");
double amt = in.nextDouble();
if (amt < items[itemCode].getPrice()) {
System.out.println("Insufficient money paid, can't dispense " +
items[itemCode].getName());
System.out.println("Refunding " + amt);
}
else
{
System.out.println("Dispensing one " + items[itemCode].getName());
double changeAmt = amt - items[itemCode].getPrice();
if (changeAmt > 0)
System.out.println("Here is your change amount of " + changeAmt);
items[itemCode].reduceQty();
}
}
}
Item[] getItems() {
return items;
}
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
VendingMachine vm = new VendingMachine();
Item[] vmItems = vm.getItems();
System.out.println("Vending Machine Menu");
for (int i = 0; i < vmItems.length; i++) {
System.out.println("Enter " + (i+1) + " for " + vmItems[i].getName());
}
System.out.println("Enter 6 to stop the Vending Machine");
int choice;
do {
System.out.print("Enter your choice: ");
choice = in.nextInt();
if (choice < 1 || choice > 6) {
System.out.println("Incorrect choice");
}
else if(choice == 6) {
System.out.println("Stopping Vending Machine...");
}
else {
vm.dispenseItem(choice - 1);
}
} while(choice != 6);
}
}