Results 1 to 6 of 6
Like Tree2Likes
  • 1 Post By JosAH
  • 1 Post By DarrylBurke

Thread: A vending Machine gui

  1. #1
    nmavrommatis is offline Member
    Join Date
    May 2012
    Posts
    1
    Rep Power
    0

    Default A vending Machine gui

    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();
    }
    }

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: A vending Machine gui

    a) please wrap your code in the code tags. b) Do you have a question?

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,404
    Rep Power
    20

    Default Re: A vending Machine gui

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default Re: A vending Machine gui

    When I have to type or copy and paste (almost) the same line more than three times, something in the back of my head starts yelling: "a loop! you silly! use a loop!"

    kind regards,

    Jos
    quad64bit likes this.
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    SnakeDoc is offline Senior Member
    Join Date
    Apr 2012
    Posts
    129
    Rep Power
    0

    Default Re: A vending Machine gui

    ur code looks interesting to learn from. do you mind posting your project3.VMachine import also? thanks!

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,404
    Rep Power
    20

    Default Re: A vending Machine gui

    Quote Originally Posted by SnakeDoc View Post
    ur code ...
    Please spell words in full: your not ur. This is a programming forum, not SMS chat.

    db
    quad64bit likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Vending Machine Change - LOGIC Understanding ISSUE
    By xcaldk74 in forum New To Java
    Replies: 4
    Last Post: 01-24-2014, 03:10 PM
  2. Why JVM varies machine to machine?
    By vivekjan17 in forum New To Java
    Replies: 4
    Last Post: 02-27-2012, 11:56 AM
  3. vending machine
    By Timmy1596 in forum JCreator
    Replies: 2
    Last Post: 09-16-2011, 09:45 PM
  4. Replies: 0
    Last Post: 02-07-2011, 01:27 PM
  5. Getting name/ip of a machine
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-02-2008, 07:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •