•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 397,791 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,454 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 1706 | Replies: 3
![]() |
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.
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.
B GRATEFUL,
LSP
LSP
•
•
Join Date: Dec 2004
Location: London or Slovakia
Posts: 2,183
Reputation:
Rep Power: 10
Solved Threads: 265
nice job you nearly there here is your missing peace :cheesy:
KEEP UP YOUR WORK :p
//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
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
If we helped you to solve your problem, answered your question please mark your post as SOLVED.
Publilius Syrus
(~100 BC)
If we helped you to solve your problem, answered your question please mark your post as SOLVED.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
access activation api blogger blogging blogs code com combo dani daniweb data debugging development dreamweaver dropdownlist gdata google gpl html innovation java key linux microsoft microsoft sdk for java microsystems module net news open openbsd platform product programming reuse rss serial software source sun tags vista web wysiwyg xml
- download jar files using java code (Java)
- I want to know about the java code. Help please!!!! (Java)
- Move out java code from jsp (Java)
- Help with Java code (Java)
- Can N E 1 Help!!!! -- Need sample Java code (Java)
- java code newbie (Java)
Other Threads in the Java Forum
- Previous Thread: Help with jpanel and jframe drawing java
- Next Thread: List View (like in VB) in Java?



Linear Mode