import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.util.*;
public class VendingMachineGUI extends JFrame implements ActionListener {
private JPanel entryPanel;
private JTextArea displayArea;
private JLabel lblVend,lblItemID, lblItemName, lblItemPri, lblItemQuant, lblcredit;
private JTextField ItemIDTextField, ItemNameTextField, ItemPriTextField, ItemQuantTextField, creditTextField;
private JButton addItButton, displayItButton, addCreditButton, purchaseItemButton, changePriceButton, changeQuantityButton;
private int counter;
private Dispenser dp1;
public Credit c;
private ArrayList <Dispenser> item;
public static void main(String[] args)
{
new VendingMachineGUI();
}
public VendingMachineGUI()
{
setLayout(new FlowLayout());
entryPanel = new JPanel();
entryPanel.setPreferredSize(new Dimension(400, 210));
entryPanel.setBackground(Color.RED);
lblVend = new JLabel("*****Vending Machine*****");
lblVend.setForeground(Color.BLUE);
lblItemID = new JLabel("Item ID: ");
lblItemID.setForeground(Color.WHITE);
lblItemName = new JLabel("Item Name: ");
lblItemName.setForeground(Color.WHITE);
lblItemPri = new JLabel("Item Price: ");
lblItemPri.setForeground(Color.WHITE);
lblItemQuant = new JLabel("Item Quantity: ");
lblItemQuant.setForeground(Color.WHITE);
lblcredit = new JLabel("Credit Amount ");
lblcredit.setForeground(Color.BLUE);
creditTextField = new JTextField(10);
ItemIDTextField = new JTextField(32);
ItemNameTextField = new JTextField(30);
ItemPriTextField = new JTextField(30);
ItemQuantTextField = new JTextField(30);
entryPanel.add(lblItemID); entryPanel.add(ItemIDTextField); entryPanel.add(lblItemName);
entryPanel.add(ItemNameTextField); entryPanel.add(lblItemPri); entryPanel.add(ItemPriTextField);
entryPanel.add(lblItemQuant); entryPanel.add(ItemQuantTextField);
addItButton = new JButton("Add Item(s)");
addItButton.setForeground(Color.BLUE);
displayItButton = new JButton("Display All Items");
displayItButton.setForeground(Color.BLUE);
addCreditButton = new JButton("Add Credit");
addCreditButton.setForeground(Color.BLUE);
purchaseItemButton = new JButton("Purchase Item");
purchaseItemButton.setForeground(Color.BLUE);
changePriceButton = new JButton("Change the price of an item");
changePriceButton.setForeground(Color.BLUE);
changeQuantityButton = new JButton("Change the quantity of an item");
changeQuantityButton.setForeground(Color.BLUE);
displayArea = new JTextArea();
displayArea.setPreferredSize(new Dimension(400, 200));
displayArea.setBackground(Color.MAGENTA);
add(lblVend); add(addCreditButton); add(lblcredit); add(creditTextField);
add(entryPanel);
add(addItButton); add(displayItButton); add(displayArea);
add(changePriceButton); add(changeQuantityButton); add(purchaseItemButton);
addItButton.addActionListener(this); displayItButton.addActionListener(this);
addCreditButton.addActionListener(this); purchaseItemButton.addActionListener(this);
changePriceButton.addActionListener(this); changeQuantityButton.addActionListener(this);
displayItButton.setEnabled(false); addCreditButton.setEnabled(false);
purchaseItemButton.setEnabled(false); changePriceButton.setEnabled(false);
changeQuantityButton.setEnabled(false);
setTitle("******Vending Machine******");
setSize(450, 610);
setVisible(true);
item = new ArrayList <Dispenser>();
c = new Credit();
counter = 0;
creditTextField.setText("0");
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == addCreditButton)
{
float totalMoney = c.addCredit();
c.credit = c.credit - totalMoney;
creditTextField.setText(Float.toString(totalMoney));
}
if (e.getSource() == addItButton)
{
String iid, itn;
int itp, itq;
if(counter <5)
{
iid = ItemIDTextField.getText();
itn = ItemNameTextField.getText();
itp = Integer.parseInt(ItemPriTextField.getText());
itq = Integer.parseInt (ItemQuantTextField.getText());
dp1 = new Dispenser(iid, itn, itp, itq);
item.add(counter, (Dispenser) dp1);
ItemIDTextField.setText(""); ItemNameTextField.setText("");
ItemPriTextField.setText(""); ItemQuantTextField.setText("");
counter ++;
if(counter > 4)
{
displayItButton.setEnabled(true); addCreditButton.setEnabled(true);
purchaseItemButton.setEnabled(true); changePriceButton.setEnabled(true);
changeQuantityButton.setEnabled(true);
}
}
else
{
JOptionPane.showMessageDialog(null, "The maximum of 5 items have been entered");
}
}
if (e.getSource() == displayItButton)
{
String allItems="";
for (int i=0; i<5; i++)
{
allItems = allItems + item.get(i).getItemValues() +"\n";
}
displayArea.setText(allItems);
}
if (e.getSource() == changePriceButton)
{
int whichitem;
String allItems="";
whichitem = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter the Item ID of the item to be modified"));
int newItemPrice = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter the new price"));
item.get(whichitem).replaceItemPrice(newItemPrice);
for (int i=0; i<5; i++)
{
allItems = allItems + item.get(i).getItemValues() +"\n";
}
displayArea.setText(allItems);
}
if (e.getSource() == changeQuantityButton)
{
int whichitem;
String allItems="";
whichitem = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter the ID of the item to be modified"));
int newItemQuantity = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter the new Quantity"));
item.get(whichitem).replaceItemQuantity(newItemQuantity);
for (int i=0; i<5; i++)
{
allItems = allItems + item.get(i).getItemValues() +"\n";
}
displayArea.setText(allItems);
}
if (e.getSource() == purchaseItemButton){
String Itemname, newidnum,newItm, qtyWanted, allRecords="";
int newPr, qAvailable, rec=0;
boolean found =false;
Itemname = (JOptionPane.showInputDialog(null, "Enter the item name"));
while(rec<5){
if(Itemname.equals(item.get(rec).getItemName()))
{
found = true;
qtyWanted = (JOptionPane.showInputDialog(null, "Enter amount to vend"));
int QtyOrdered = Integer.valueOf(qtyWanted).intValue();
newidnum = item.get(rec).getItemID(); newItm = item.get(rec).getItemName();
newPr = item.get(rec).getItemPrice(); qAvailable = item.get(rec).getItemQuantity();
int CurrentQ = Integer.valueOf(qAvailable).intValue();
int NewQ = CurrentQ - QtyOrdered;
float P = Float.valueOf(newPr).floatValue();
float TotalPr = P * QtyOrdered;
if(TotalPr < c.credit)
{
JOptionPane.showMessageDialog(null,"Your bill is "+ TotalPr);
c.credit = c.credit - TotalPr;
String str = "" + c.credit;
creditTextField.setText(str);
item.get(rec).dispenceItem(newidnum, newItm, newPr, NewQ);
for (int i=0; i<5; i++)
{
allRecords = allRecords + item.get(i).getItemValues() +"\n";
}
displayArea.setText(allRecords);}
else
{
JOptionPane.showMessageDialog(null,"Insufficient Credit");
}
break;
}
rec++;
}
if(found == false){
JOptionPane.showMessageDialog(null, "Item not found, please try again");
}
}
}
}