1,105,288 Community Members

Vending (Drinking) Machine Java Code

Member Avatar
LSPUWILLC
Newbie Poster
9 posts since Oct 2006
Reputation Points: 2 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
1
 

This year I started learning Java in class and now we are making a Vending Machne code...
I compleated the task, but now I want to add another buton, but I get stuck a a sertain point...

Here is my original code with the second button added:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ThirstQuencher extends JFrame
implements ActionListener {

        private JButton button1;
        private JButton button2;

        public static void main (String [] args) {
            ThirstQuencher frame = new ThirstQuencher () ;
            frame.setSize (400, 300) ;
            frame.createGUI () ;
            frame.show () ;
        }

        private void createGUI() {
            setDefaultCloseOperation (EXIT_ON_CLOSE) ;
            Container window = getContentPane () ;
            window.setLayout (new FlowLayout () ) ;

            //First button
            button1 = new JButton ("Coca-Cola      1,15 Û") ;
            window.add (button1) ;
            button1.addActionListener (this) ; 

            //Second button
            button2 = new JButton ("Water      0,85 Û") ;
            window.add (button2) ;
            button2.addActionListener (this) ; 
        }

        public void actionPerformed (ActionEvent event) {
            int cents;
            int euros;
            int change;
            int changeCents;
            int changeEuros;

            //Makes windows that ask for money
            String centsString;
            String eurosString;
            eurosString = JOptionPane.showInputDialog ("Enter Û (Euros)") ;
            centsString = JOptionPane.showInputDialog ("Enter c (Cents)") ;
            cents = Integer.parseInt (centsString);
            euros = Integer.parseInt (eurosString) * 100;

            //Calculate the change

            change = (cents + euros) - 115;

            //Splits up the change
            changeCents = change % 100;
            changeEuros = change / 100;
            //Tells the change
            JOptionPane.showMessageDialog (null, 
                "Change breakes down into:" + 
                "   Euros: " + changeEuros + 
                "   Cents: " + changeCents);
            //Tells how the cents come out    
            int CentsDecimale;
            int CentsJednine;
            CentsDecimale = changeCents / 10;
            CentsJednine = changeCents - (CentsDecimale * 10);
            JOptionPane.showMessageDialog (null, 
                "Change in Cents coming out:" + 
                "     " + 
                CentsDecimale + 
                " *10c" + 
                "     " +
                CentsJednine + 
                " *1c");
            //Tells how the euros come out
            int NEuros;
            NEuros = changeEuros / 1;
            JOptionPane.showMessageDialog (null, 
            "Change in Euros comming out:     " + 
            NEuros + 
            " *1Û");

        }
    }

The only part I need to figure out is how to make the program calculate -85 if the person selected water instead of coke which is -115!
That part ih colored in blue!
PLS enyone help me
THANX.

Member Avatar
LSPUWILLC
Newbie Poster
9 posts since Oct 2006
Reputation Points: 2 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Coke is button 1 and water is button 2...

Member Avatar
peter_budo
Code tags enforcer
7,934 posts since Dec 2004
Reputation Points: 2,502 [?]
Q&As Helped to Solve: 1,028 [?]
Skill Endorsements: 66 [?]
Moderator
Featured
 
0
 

nice job you nearly there here is your missing peace :cheesy:

//Calculate the change
String command = event.getActionCommand();
if(command == "Coca-Cola 1,15 Û")
{
change = (cents + euros) - 115;
}
else
{
change = (cents + euros) - 85;
}

KEEP UP YOUR WORK :p

Member Avatar
LSPUWILLC
Newbie Poster
9 posts since Oct 2006
Reputation Points: 2 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

IT WORKS!!!
Thank you very much!

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article