Start New Discussion within our Software Development Community

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.

Comments
Code nearly done just asking for advice in simple matter

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

This article has been dead for over six months. Start a new discussion instead.