package project3;
import project3.VMachine;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
public class VMachineGui extends JFrame {
/**The public class VMachine is imported in this class in order to create a GUI application
* It is created using the method of Buttons and options and in order to avoid opening a
* second window it uses a void class Hide Buttons, it also uses a public to implement the
* 10 objects and invoke when an action occurs, and a void to show Buttons
* @author nikolaos mavrommatis
*/
private static final long serialVersionUID = 1L;
VMachine VM = new VMachine();
private JLabel x1,x2,x3,x4,x5,x6,x7,x8,x9,x0,x11;
private JButton exitB, Option1, Option2, Option3, Option4;
private JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b0;
private ExitButtonHandler ebHandler;
private Option1ButtonHandler opt1Handler;
private Option2ButtonHandler opt2Handler;
private Option3ButtonHandler opt3Handler;
private Option4ButtonHandler opt4Handler;
private b1ButtonHandler b1Handler;
private b2ButtonHandler b2Handler;
private b3ButtonHandler b3Handler;
private b4ButtonHandler b4Handler;
private b5ButtonHandler b5Handler;
private b6ButtonHandler b6Handler;
private b7ButtonHandler b7Handler;
private b8ButtonHandler b8Handler;
private b9ButtonHandler b9Handler;
private b0ButtonHandler b0Handler;
private static final int WIDTH = 600;
private static final int HEIGHT = 800;
public VMachineGui() {
Option1 = new JButton("1. Display Products");
Option2 = new JButton("2. Buy Products");
Option3 = new JButton("3. Display total products sold.");
Option4 = new JButton("4. Display total amount of money in the machine.");
x11 = new JLabel("", SwingConstants.CENTER);
x1 = new JLabel("", SwingConstants.CENTER);
x2 = new JLabel("", SwingConstants.CENTER);
x3 = new JLabel("", SwingConstants.CENTER);
x4 = new JLabel("", SwingConstants.CENTER);
x5 = new JLabel("", SwingConstants.CENTER);
x6 = new JLabel("", SwingConstants.CENTER);
x7 = new JLabel("", SwingConstants.CENTER);
x8 = new JLabel("", SwingConstants.CENTER);
x9 = new JLabel("", SwingConstants.CENTER);
x0 = new JLabel("", SwingConstants.CENTER);
/*The Class creates an Exit Button, inserts the options and sets the title
* the size and the width for the Frame
* @author nikolaos mavrommatis
*/
exitB = new JButton("Exit");
ebHandler = new ExitButtonHandler();
exitB.addActionListener(ebHandler);
opt1Handler = new Option1ButtonHandler();
Option1.addActionListener(opt1Handler);
opt2Handler = new Option2ButtonHandler();
Option2.addActionListener(opt2Handler);
opt3Handler = new Option3ButtonHandler();
Option3.addActionListener(opt3Handler);
opt4Handler = new Option4ButtonHandler();
Option4.addActionListener(opt4Handler);
b1 = new JButton("1."+VM.Object[0].ProdName);
b1Handler = new b1ButtonHandler();
b1.addActionListener(b1Handler);
b2 = new JButton("2."+VM.Object[1].ProdName);
b2Handler = new b2ButtonHandler();
b2.addActionListener(b2Handler);
b3 = new JButton("3."+VM.Object[2].ProdName);
b3Handler = new b3ButtonHandler();
b3.addActionListener(b3Handler);
b4 = new JButton("4."+VM.Object[3].ProdName);
b4Handler = new b4ButtonHandler();
b4.addActionListener(b4Handler);
b5 = new JButton("5."+VM.Object[4].ProdName);
b5Handler = new b5ButtonHandler();
b5.addActionListener(b5Handler);
b6 = new JButton("6."+VM.Object[5].ProdName);
b6Handler = new b6ButtonHandler();
b6.addActionListener(b6Handler);
b7 = new JButton("7."+VM.Object[6].ProdName);
b7Handler = new b7ButtonHandler();
b7.addActionListener(b7Handler);
b8 = new JButton("8."+VM.Object[7].ProdName);
b8Handler = new b8ButtonHandler();
b8.addActionListener(b8Handler);
b9 = new JButton("9."+VM.Object[8].ProdName);
b9Handler = new b9ButtonHandler();
b9.addActionListener(b9Handler);
b0 = new JButton("10."+VM.Object[9].ProdName);
b0Handler = new b0ButtonHandler();
b0.addActionListener(b0Handler);
setTitle("VendingMachine");
Container pane = getContentPane();
pane.setLayout(new GridLayout(8, 2));
pane.add(Option1);
pane.add(Option2);
pane.add(Option3);
pane.add(Option4);
pane.add(x11);
pane.add(x1);
pane.add(x2);
pane.add(x3);
pane.add(x4);
pane.add(x5);
pane.add(x6);
pane.add(x7);
pane.add(x8);
pane.add(x9);
pane.add(x0);
pane.add(exitB);
HideButtons();
setSize(WIDTH, HEIGHT);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
try {
/*@author nikolaos mavrommatis
* Prints this throwable and its backtrace to the standard error stream.
* This method prints a trace for the object that throws on the error output stream
* that is the value of the field
* @author nikolaos mavrommatis
*/
} catch (Exception e) {
e.printStackTrace();
}
}
public class ExitButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
public class Option1ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (VM.Object[0].ProdQuantity>=1)x1.setText("1."+VM.Object[0].ProdName);
if (VM.Object[1].ProdQuantity>=1)x2.setText("2."+VM.Object[1].ProdName);
if (VM.Object[2].ProdQuantity>=1)x3.setText("3."+VM.Object[2].ProdName);
if (VM.Object[3].ProdQuantity>=1)x4.setText("4."+VM.Object[3].ProdName);
if (VM.Object[4].ProdQuantity>=1)x5.setText("5."+VM.Object[4].ProdName);
if (VM.Object[5].ProdQuantity>=1)x6.setText("6."+VM.Object[5].ProdName);
if (VM.Object[6].ProdQuantity>=1) x7.setText("7."+VM.Object[6].ProdName);
if (VM.Object[7].ProdQuantity>=1)x8.setText("8."+VM.Object[7].ProdName);
if (VM.Object[8].ProdQuantity>=1)x9.setText("9."+VM.Object[8].ProdName);
if (VM.Object[9].ProdQuantity>=1)x0.setText("10."+VM.Object[9].ProdName);
}
}
public class Option3ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
x1.setText("Total Product Sold "+VM.TotalSold);
x2.setText("");
x3.setText("");
x4.setText("");
x5.setText("");
x6.setText("");
x7.setText("");
x8.setText("");
x9.setText("");
x0.setText("");
}
}
private class Option2ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
ShowButtons();
}
}
private class Option4ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
x1.setText("Total Amount is: "+VM.TotalAmountOfMoney);
x2.setText("");
x3.setText("");
x4.setText("");
x5.setText("");
x6.setText("");
x7.setText("");
x8.setText("");
x9.setText("");
x0.setText("");
}
}
// ----------------------------------------------------------------------------------------
public void ShowButtons (){
Container pane = getContentPane();
pane.setLayout(new GridLayout(14, 2));
pane.add(b1);
pane.add(b2);
pane.add(b3);
pane.add(b4);
pane.add(b5);
pane.add(b6);
pane.add(b7);
pane.add(b8);
pane.add(b9);
pane.add(b0);
pane.add(exitB);
if (VM.Object[0].ProdQuantity>=1){b1.setVisible(true); }
if (VM.Object[1].ProdQuantity>=1){b2.setVisible(true);}
if (VM.Object[2].ProdQuantity>=1){b3.setVisible(true);}
if (VM.Object[3].ProdQuantity>=1)b4.setVisible(true);
if (VM.Object[4].ProdQuantity>=1)b5.setVisible(true);
if (VM.Object[5].ProdQuantity>=1)b6.setVisible(true);
if (VM.Object[6].ProdQuantity>=1)b7.setVisible(true);
if (VM.Object[7].ProdQuantity>=1)b8.setVisible(true);
if (VM.Object[8].ProdQuantity>=1)b9.setVisible(true);
if (VM.Object[9].ProdQuantity>=1)b0.setVisible(true);
setSize(WIDTH, HEIGHT);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void HideButtons (){
b1.setVisible(false);
b2.setVisible(false);
b3.setVisible(false);
b4.setVisible(false);
b5.setVisible(false);
b6.setVisible(false);
b7.setVisible(false);
b8.setVisible(false);
b9.setVisible(false);
b0.setVisible(false);
}
public void buy(int sel,VMachine VM){
if (VM.Object[sel-1].ProdQuantity<=0){
return;
}
VM.Object[sel-1].ProdQuantity=VM.Object[sel-1].ProdQuantity-1;
VM.TotalSold=VM.TotalSold+1;
VM.TotalAmountOfMoney=VM.Object[sel-1].ProdPrice+VM.TotalAmountOfMoney;
HideButtons();
}
public class b1ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
buy(1,VM);
}
}//end b1
public class b2ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
buy(2,VM);}
}// end b2
public class b3ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
buy(3,VM);}
}// end b3
public class b4ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
buy(4,VM);}
}
public class b5ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
buy(5,VM);}
}
public class b6ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
buy(6,VM);}
}
public class b7ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
buy(7,VM);}
}
public class b8ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
buy(8,VM);}
}
public class b9ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
buy(9,VM);}
}
public class b0ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
buy(10,VM);}
}
public static void main(String[] args)
/*
* here stands the main class for the VMachineGui
* @author nikolaos mavrommatis
*/
{
@SuppressWarnings("unused")
project3.VMachineGui rectObject = new VMachineGui();
}
}

